自定义C语言变量转换库

一、代码库如下

(一)字符串转int整数

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

// 返回整数结果

int parseInt(char msg[]){

int offset = 0;

int total = 0;

// 默认是正数

int isPositive = 1;

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

if(msg[0] == '+'){

offset++;

} else if (msg[0] == '-'){

isPositive = 0;

offset++;

}

int k;

// 如果存在小数点

int isExistPoint = 0;

for(k = 0; msg[k] != '\0'; k++){

if(msg[k] == '.'){

isExistPoint = 1;

break;

}

}

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

if(isExistPoint){

return 0;

}

// 进行求值

for(k = offset; msg[k] != '\0'; k++){

total = (msg[k] - 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(msg[0] == '+'){

offset++;

} else if (msg[0] == '-'){

isPositive = 0;

offset++;

}

int k;

// 如果存在小数点

int isExistPoint = 0;

for(k = 0; msg[k] != '\0'; k++){

if(msg[k] == '.'){

isExistPoint = 1;

break;

}

}

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

if(isExistPoint){

return 0;

}

// 进行求值

for(k = offset; msg[k] != '\0'; k++){

total = (msg[k] - 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; msg[k] != '\0'; k++){

total = (msg[k] - 48) + total * 10L;

}

return total;

}

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

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

// 返回整数结果

double parseDouble(char msg[]){

int offset = 0;

double total = 0;

// 默认是正数

int isPositive = 1;

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

if(msg[0] == '+'){

offset++;

} else if (msg[0] == '-'){

isPositive = 0;

offset++;

}

int k;

// 记录小数点位置

int pointIndex = -1;

// 如果存在小数点

int isExistPoint = 0;

for(k = 0; msg[k] != '\0'; k++){

if(msg[k] == '.'){

isExistPoint = 1;

pointIndex = k;

break;

}

}

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

if(isExistPoint){

int h = 0;

// 进行整数求值

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

total = (msg[h] - 48) + total * 10.0;

}

// 进行小数部分求值

double unit = 1.0;

for(h = pointIndex + 1; msg[h] != '\0'; h++){

// 更新单位

unit = unit * 0.1;

total = total + (msg[h] - 48) * unit;

}

} else{

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

int n = 0;

for(n = offset; msg[n] != '\0'; n++){

total = (msg[n] - 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;

}

相关推荐
来来走走14 分钟前
Flutter开发 了解Scaffold
android·开发语言·flutter
xiangweiqiang1 小时前
用phpstudy安装php8.2后报错:意思是找不到php_redis.dll拓展时
开发语言·php
mitt_1 小时前
go语言变量
开发语言·后端·golang
sakabu1 小时前
cJSON库应用
c语言·笔记·学习
TravisBytes2 小时前
gRPC C++ 从 0 到 1 → 到线上:**超详细** 环境搭建、编码范式、性能调优与 DevOps 全攻略
开发语言·c++·devops
kngines2 小时前
【Node.js从 0 到 1:入门实战与项目驱动】1.1 什么是 Node.js?(定义、运行环境、与浏览器 JavaScript 的区别)
开发语言·javascript·node.js
大阳1233 小时前
数据结构2.(双向链表,循环链表及内核链表)
c语言·开发语言·数据结构·学习·算法·链表·嵌入式
ChipCamp3 小时前
Chisel芯片开发入门系列 -- 18. CPU芯片开发和解释8(流水线架构的代码级理解)
开发语言·青少年编程·fpga开发·scala·dsp开发·risc-v·chisel
越来越无动于衷3 小时前
智慧社区(八)——社区人脸识别出入管理系统设计与实现
java·开发语言·spring boot·python·mysql
CUC-MenG4 小时前
2025牛客多校第六场 D.漂亮矩阵 K.最大gcd C.栈 L.最小括号串 个人题解
c语言·c++·算法·矩阵