自定义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;

}

相关推荐
努力的lpp1 小时前
php反序列化一(大白话版)
开发语言·web安全·php·ctf·反序列化
菜鸟~noob2331 小时前
【电子战】第03篇:CFAR(恒虚警)处理【含matlab代码】
开发语言·matlab
泡海椒2 小时前
内置SPI函数库详解:JQuick-Java Builtin工具类实战用法
java·开发语言·python
hqyjzsb2 小时前
零 AI 项目经验,学 Python 转型 AI 的正确顺序是什么?
开发语言·人工智能·python·算法·职场和发展·数据挖掘·数据分析
T1mzhou3 小时前
ARM64 Linux 6.10内核启动流程7-ioremap和readl writel
linux·服务器·c语言
Keven_113 小时前
算法札记:ACM适用的很骚的C++语法(持续更新)
开发语言·c++
脉动数据行情14 小时前
C# 实现德指 DAX 期货 WebSocket 实时行情监听
开发语言·websocket·c#
大圣编蚕4 小时前
Java ByteArrayInputStream 详解:从入门到实战
java·开发语言·算法
菜鸟~noob2334 小时前
【电子战】第02篇:能量检测器 vs. 相关检测器【含matlab代码】
开发语言·matlab
OpenAnolis小助手5 小时前
一句话看透 JVM,SysOM 诊断 Skill 新增 Java 应用诊断能力
java·开发语言·jvm·阿里云·操作系统控制台·sysom