当前位置: 首页 > news >正文

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


文章转载自:
http://cambrian.isnyv.cn
http://aerothermoacoustics.isnyv.cn
http://chastisement.isnyv.cn
http://allograph.isnyv.cn
http://bergamasque.isnyv.cn
http://calamanco.isnyv.cn
http://braciole.isnyv.cn
http://aaui.isnyv.cn
http://cantata.isnyv.cn
http://ammoniated.isnyv.cn
http://ambition.isnyv.cn
http://agrobiologist.isnyv.cn
http://cacodorous.isnyv.cn
http://carbomycin.isnyv.cn
http://carbonaceous.isnyv.cn
http://bumpily.isnyv.cn
http://arbitrational.isnyv.cn
http://cervelat.isnyv.cn
http://acatalasia.isnyv.cn
http://arboreal.isnyv.cn
http://bea.isnyv.cn
http://authorized.isnyv.cn
http://armless.isnyv.cn
http://aluminiferous.isnyv.cn
http://caecitis.isnyv.cn
http://bearcat.isnyv.cn
http://asymptomatic.isnyv.cn
http://agglutinative.isnyv.cn
http://adenitis.isnyv.cn
http://appui.isnyv.cn
http://www.dtcms.com/a/100588.html

相关文章:

  • P1090合并果子(优先队列)
  • Vue3 其它API toRow和markRow
  • nacos 3.x Java SDK 使用详解
  • 【COMSOL】参数化建模以及通过MATLAB运行
  • qml中的Connections用法
  • Android Gradle 下载插件或依赖太慢
  • ansible主机清单文件以及ansible常用模块
  • 软考中级-软件设计师信息安全模块考点解析
  • docker 镜像下载的另一种方式
  • 简易指南“<em >快</em><em>3</em><em>倍</em><em>投</em><em>规</em><em>划
  • C++第13届蓝桥杯省b组习题笔记
  • 自动化工作流工具的综合对比与推荐
  • 自定义类型:结构体(1)
  • 在 Qt 创建项目时,Qt Quick Application (Compat) 和 Qt Quick Application
  • 5种特效融合愚人节搞怪病毒
  • 深入解析C#中的解释器模式:原理与应用
  • C语言:多线程
  • 《Python实战进阶》第39集:模型部署——TensorFlow Serving 与 ONNX
  • C--操作符
  • AI人工智能-PyCharm的介绍安装应用
  • Vue学习笔记集--defineExpose
  • CSP-J 2019 入门级 第一轮(初赛) 完善程序(2)
  • 协程 Coroutine
  • 综合实验一
  • Arduino示例代码讲解:Virtual Color Mixer 虚拟混色器
  • CS提取的基本使用和模块加载
  • 树莓派超全系列文档--(14)无需交互使用raspi-config工具其一
  • 记录vite-plugin-dts打包时无法生成 .d.ts文件问题
  • Numpy常见bug
  • 定时器的定义