自定义C语言变量转换库

一、代码库如下

(一)字符串转int整数

// 把数字字符串转成整数

// 返回整数结果

int parseInt(char msg\[\]){

int offset = 0;

int total = 0;

// 默认是正数

int isPositive = 1;

// 第一个是符号,如果是+代表整数

if(msg0 == '+'){

offset++;

} else if (msg0 == '-'){

isPositive = 0;

offset++;

}

int k;

// 如果存在小数点

int isExistPoint = 0;

for(k = 0; msgk != '\0'; k++){

if(msgk == '.'){

isExistPoint = 1;

break;

}

}

// 如果存在小数点,返回0

if(isExistPoint){

return 0;

}

// 进行求值

for(k = offset; msgk != '\0'; k++){

total = (msgk - 48) + total * 10;

}

// 如果是负数,乘以-1

if(!isPositive){

total = total * (-1);

}

return total;

}

(二)字符串转long类型的整数

// 把数字字符串转成整数

// 返回整数结果

long parseLong(char msg\[\]){

int offset = 0;

long total = 0L;

// 默认是正数

int isPositive = 1;

// 第一个是符号,如果是+代表整数

if(msg0 == '+'){

offset++;

} else if (msg0 == '-'){

isPositive = 0;

offset++;

}

int k;

// 如果存在小数点

int isExistPoint = 0;

for(k = 0; msgk != '\0'; k++){

if(msgk == '.'){

isExistPoint = 1;

break;

}

}

// 如果存在小数点,返回0

if(isExistPoint){

return 0;

}

// 进行求值

for(k = offset; msgk != '\0'; k++){

total = (msgk - 48) + total * 10L;

}

// 如果是负数,乘以-1

if(!isPositive){

total = total * (-1);

}

return total;

}

// 把数字字符串转成无符号的长整数

// 返回整数结果

long parseUnsignedLong(char msg\[\]){

int offset = 0;

unsigned long total = 0L;

int k;

// 进行求值

for(k = offset; msgk != '\0'; k++){

total = (msgk - 48) + total * 10L;

}

return total;

}

(三)字符串转double类型的数

// 把浮点数字字符串转成浮点数

// 返回整数结果

double parseDouble(char msg\[\]){

int offset = 0;

double total = 0;

// 默认是正数

int isPositive = 1;

// 第一个是符号,如果是+代表整数

if(msg0 == '+'){

offset++;

} else if (msg0 == '-'){

isPositive = 0;

offset++;

}

int k;

// 记录小数点位置

int pointIndex = -1;

// 如果存在小数点

int isExistPoint = 0;

for(k = 0; msgk != '\0'; k++){

if(msgk == '.'){

isExistPoint = 1;

pointIndex = k;

break;

}

}

// 如果存在小数点,计算整数

if(isExistPoint){

int h = 0;

// 进行整数求值

for(h = offset; h < pointIndex; h++){

total = (msgh - 48) + total * 10.0;

}

// 进行小数部分求值

double unit = 1.0;

for(h = pointIndex + 1; msgh != '\0'; h++){

// 更新单位

unit = unit * 0.1;

total = total + (msgh - 48) * unit;

}

} else{

// 不存在小数点,全部算作整数

int n = 0;

for(n = offset; msgn != '\0'; n++){

total = (msgn - 48) + total * 10.0;

}

}

// 如果是负数,乘以-1

if(!isPositive){

total = total * (-1);

}

return total;

}

二、使用案例

#include<stdio.h>

int main(){

printf("把-55转成整数:%d", parseInt("-55"));

printf("把1155转成整数:%d", parseInt("1155"));

printf("把-1999888777转成整数:%ld", parseLong("-1999888777"));

printf("把44441225转成无符号整数:%ld", parseUnsignedLong("44441225"));

printf("把-5525.0转成浮点数:%lf", parseDouble("-5525.0"));

printf("把3.14156转成浮点数:%lf", parseDouble("3.14156"));

return 0;

}

相关推荐
yujunl1 小时前
总结一下U9客开的成果
开发语言
独隅1 小时前
KMP 全栈进化:Koog 框架打造纯 Kotlin AI Agent 实战效果
开发语言·人工智能·kotlin
lhldsg1 小时前
社区健身场地规划实战指南:从器材配置到智能化管理经验分享
java·开发语言·经验分享·小程序
SomeB1oody2 小时前
【RustyML入门】5.0. 模型评估
开发语言·后端·机器学习·rust·教程
Evand J2 小时前
【MATLAB例程,PDR11】二维平面下的PDR(行人航位推算)步态检测与EKF融合定位,附代码的下载链接
开发语言·matlab·平面·pdr·行人导航·步态检测
牛油果子哥q3 小时前
C++STL算法超全精讲:排序/查找/去重/遍历/最值/合并全套API、仿函数、Lambda适配、实战避坑
开发语言·c++·算法
Xeon_CC3 小时前
用C++编写屏幕准星
开发语言·c++
是隼人3 小时前
buuctf-pwn bjdctf_2020_babystack2题解 整数溢出(学习过程持续更新)
c语言·学习·安全·pwn入门·ctf入门
孤存5223 小时前
c语言动态内存管理
c语言·开发语言·算法
祖力553 小时前
快速学会数据结构中的链式队列与循环队列
linux·c语言·数据结构·算法