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

}

相关推荐
David猪大卫5 小时前
【Linux修炼】环境变量、进程地址空间
linux·c语言·经验分享·笔记·学习·ubuntu·centos
xiaoye-duck7 小时前
《C++ 项目:手写大模型接入 SDK》Gemini接口设计(上):初始化、全量响应实现和测试
开发语言·网络·c++·语言模型
nice_lcj5209 小时前
Java Lambda 表达式入门:怎么写、怎么用、常用方法有哪些
java·开发语言·后端·面试
码农小河669 小时前
基于SSM+JSP+MySQL的在线医疗服务系统的设计与实现
java·开发语言·mysql
Zwawa13 小时前
两根线怎么挂住一堆设备:I2C 协议原理
c语言·单片机·嵌入式
nice_lcj52014 小时前
ThreadLocal 入门:它是怎么隔离数据的、为什么会导致内存泄漏
java·开发语言·后端·面试·并发编程·内存泄漏·threadlocal
紧跟先前的步伐14 小时前
Go AI Agent 实战 28|Agent 的预算、超时与停止条件
开发语言·人工智能·后端·ai·golang
武子康14 小时前
h3.c 的两个权重槽,怎样把内存压力换成 SSD 等待
c语言·人工智能·agent
bksczm15 小时前
InnoDB 行格式详解:DYNAMIC / COMPACT / COMPRESSED / REDUNDANT
java·开发语言·数据库
春涧草茶15 小时前
慢就是快16-1协程
开发语言·前端·python