zoukankan      html  css  js  c++  java
  • 字符串转换为数字

    int 型数据的范围:-2^31~2^31-1(-2147483648~2147483647)

    unsigned int:  0~2^32

    1.自己写的函数,注意特殊情况的考虑(c代码,基于char*类型)

    #include <iostream>
        using namespace std;
    #include <climits>     //包含整型数的范围限制
    
        int myAtoi(char* str) {
            unsigned long int number=0;
            bool ifneg=false;
            if(str=='')               //需要对空字符串进行处理
            {
                return 0;
            }
            else
            {
                while(*str==' '||*str=='
    '||*str=='
    '||*str=='	'||*str=='f'||*str=='')   //扫描前面的空格,跳过空格
                {++str;}
                if(*str==45)
                {ifneg=true;
                ++str;}          //首字母为负号
                else if(*str==43)
                {++str;}
                while(*str=='0') str++;    //在符号之后的首字母为0的,略过前面的0
                while(*str!=0)        //c风格字符串以空字符()结尾,空字符的ASCII码为0。cout打印时看到空字符才结束
                {
                    if(*str<'0'||*str>'0'+9)     //如果首字符为字母,则返回0.否则返回字母前面的数
                    {
                        if(number==0)
                            return 0;
                        if(ifneg==true)
                            return (int)-number;
                        else
                            return (int)number;
                    }
                    number=number*10+*str-'0';
                    ++str;
                    if(ifneg==false&&number>INT_MAX)             //对溢出进行处理
                    {
                        return INT_MAX;
                    }
                    else if(ifneg==true&&number-1>INT_MAX)
                    {
                        return INT_MIN;
                    }
                }
                if(ifneg==true)
                {
                    return (int)-number;}
                else
                {
                    return (int)number;}
            }
        }
    
        void main()
        {
            char *str="   f454769868898";
            int number=myAtoi(str);
            printf("%d",number);
        }

    c++版(string的应用)

    class Solution {
        public:
            int myAtoi(string str) {
                if(str.empty())
                    return 0;
                int i=0;
                short symbol=1;         //注意要初始化
                unsigned long int number=0;           //注意要初始化,另外要注意类型要大于10*int的最大值
                while(str[i]==' '||str[i]=='
    '||str[i]=='	'||str[i]=='f'||str[i]=='
    '||str[i]=='') i++;
                if(str[i]=='-')
                {symbol=-1;i++;}
                else if(str[i]=='+')    //这句话不能少
                    i++;
                while(str[i]=='0') i++;
                while(i<str.length())
                {
                    if(str[i]<'0'||str[i]>'9')
                        return symbol*number;
                    if(symbol==1&&number*10+str[i]-'0'>INT_MAX)
                        return INT_MAX;
                    else if(symbol==-1&&number*10+str[i]-'0'-1>INT_MAX)
                        return INT_MIN;
                    else
                        number=number*10+str[i]-'0';
                    i++;
                }
                return symbol*number;
            }
        };

    2.c语言中的sscanf

    #include<iostream>
    using namespace std;
    #include<stdio.h>
    void main()
    {
        char s[100];
        cin>>s;
        int i;
        float f;
        sscanf(s,"%d",&i);    //溢出的时候会用负值来表示
        sscanf(s,"%f",&f);
        cout<<"i:"<<i<<endl;
        cout<<"f:"<<f;
    }

    3.c标准库中的atoi

    #include<iostream>
    using namespace std;
    #include<stdio.h>
    void main()
    {
        char s[100];
        cin>>s;
        int i;
        float f;
        i=atoi(s);    //存在非数字的字符时也会输出0,溢出时会得到INT_MAX
        f=atof(s);    //科学计数法来表示结果
        cout<<"i:"<<i<<endl;
        cout<<"f:"<<f;
    }
    #include<iostream>
    using namespace std;
    #include<string>
    void main()
    {
        string s;
        cin>>s;
        int i;
        float f;
        i=atoi(s.c_str());    //存在非数字的字符时也会输出0,溢出时会得到INT_MAX
        f=atof(s.c_str());    //科学计数法来表示结果
        cout<<"i:"<<i<<endl;
        cout<<"f:"<<f;
    }
  • 相关阅读:
    由高度场求法线
    unity中的透视投影矩阵
    bindpose定义
    blinn-phong高光反向穿透问题
    fft ocean注解
    理顺FFT
    unity, 在image effect shader中用_CameraDepthTexture重建世界坐标
    unity, ComputeScreenPos 作用
    Lambert漫反射的BRDF
    VC++ MFC获取对话框上控件的位置
  • 原文地址:https://www.cnblogs.com/wy1290939507/p/4476932.html
Copyright © 2011-2022 走看看