c++提取string里的数字

直接上代码:

#include <iostream>
#include <string>

using namespace std;

float to_float(string s)  
{  
    int i = 0,n = 0;  
    int point_index = s.find('.');  
    float result = 0,under_0 = 0;//under_0存储小数部分  
    if (count(s.begin(), s.end(), '.') > 1)  
    {
        return 0;//字符串里只能有1个或0个小数点,不然出错退出  
    }
    if (s.find('.') != -1)//字符串里有小数点  
    {
        if ((point_index == 0) || (point_index == s.size()-1))//小数点位置合理,不能在字符串第1位,且不能在最后一位  
        {
            return 0;
        }
        for (i = 0; i <= point_index - 1; i++)//计算整数部分  
        {  
            if (s[i] >= '0'&&s[i] <= '9')  
            {  
                result = result * 10 + s[i] - 48;  
            }  
        }  
        for (i = s.size() - 1; i >= point_index-1 ; i--)//计算小数部分  
        {  
            if (s[i] >= '0'&&s[i] <= '9')  
            {  
                if (i == point_index-1)  
                {  
                    under_0 = under_0 * 0.1 + 0;//i=小数点前一位,under_0+0  
                }  
                else  
                {  
                    under_0 = under_0* 0.1 + s[i] - 48;  
                }  
            }  
        }  
        result = result + under_0;//把整数部分和小数部分相加  
    }  
    else//字符串只有整数部分  
    {  
        for (i = 0; i <= s.size() - 1;i++)  
        {  
            if (s[i] >= '0'&&s[i] <= '9')  
            {  
                result= result * 10 + s[i] - 48;  
            }  
        }  
    }  

    return result;  

} https://blog.csdn.net/qq_39660930/article/details/78125237

猜你喜欢

转载自blog.csdn.net/walker_m/article/details/80149377
今日推荐