C 字符串转数字

问题:给定一个字符串输入,将其转换为相应的数字


涉及知识点:字符串处理

分析:  1.输入字符串的合法性检查

          2.对于顺序读入的字符串,如何确定相应的权重

          3.处理非法字符串

          4.对于整数,小数,负数的处理

首先放上 C库函数中atoi函数的原型:

int ascii_to_int(char const *string)
{
    int value;
    value = 0;
    
    while(*string <= '0'&&*string >= '9')
    {
        value *= 10;
        value += *string - '0';
        ++string;
    }
    if(*string != '0')
        value = 0;
    
    return value;
}


这个原函数只对输入为数字字符的字符串进行处理,如果输入为非数字字符则不予处理,返回0


自己写的:

#include <stdio.h>
#include <math.h>
#include <stdbool.h>

#define SIZE 500

long double strToInt(char *str);
char* dealWithStr(char *str);

bool isNagetiveNumber(char *str);
bool isPureDecimalNumber(char *str);
bool isNagetive_PureDecimalNumber(char *str);


int main(int argc, const char * argv[])
{
    
    char *orginal_Str;
    char ch[SIZE];
    printf("Please input the orginal string( Only numbers):\n");
    scanf("%s",ch);
    orginal_Str = ch;
    char *checked_Str = dealWithStr(orginal_Str);
    
    long double rs;
    if (checked_Str != NULL) {
        rs = strToInt(checked_Str);
        
        printf("The int number of the string is %Lf\n",rs);
    }else{
    
        printf("Invalid input!\n");
    }

    return 0;
}

/***
    将字符串转换为Int型整数
 ***/
long double strToInt(char *str){
    
	long double result = 0;
    long double resultForIntegerPart = 0;
    long double resultForDecimalPart = 0;
    
    int count = 0;
    char *tempStr = str;
    
    bool isNagetive = false;
    
    bool isIntegerPart = true;
    int countFordecimalPart = 0;

    if (isNagetive_PureDecimalNumber(str) || isNagetiveNumber(str)) {
        isNagetive = true;
    }
    
    /***
        分析字符串,将其划分为整数和小数两个部分,分别计算
     ***/
	while(*tempStr != '\0'){
        
        switch (*tempStr) {
            case '-':
                tempStr++;
                break;
                
            case '.':{
                
                isIntegerPart = false;
                count = 0;
                tempStr++;
            }
                break;
            default:{
            
                int number = (int)(*tempStr - '0');
                
                /***
                    整数部分
                 ***/
                if (isIntegerPart) {
                    if(0 == count)
                        
                        resultForIntegerPart += number;
                    else{
                        
                        resultForIntegerPart = resultForIntegerPart * 10 + number;
                    }
                    count++;
                    tempStr++;
                }else{
                    /***
                        小数部分
                     ***/
                    if(0 == count)
                        
                        resultForDecimalPart += number;
                    else{
                        
                        resultForDecimalPart = resultForDecimalPart * 10 + number;
                    }
                    count++;
                    countFordecimalPart++;
                    tempStr++;
                    
                }

            }
                break;
        }

        
	}
    /***
        如果是整数部分
     ***/
    if (isIntegerPart) {
        result = resultForIntegerPart;
    }else{
        result = resultForIntegerPart + resultForDecimalPart * pow(10,-countFordecimalPart);
    }
    
    if (isNagetive) {
        result = result * -1;
    }
    
	return result;
}

/***
    处理字符串,只允许输入数字字符串
 ***/
char* dealWithStr(char *str){

    char *temp = str;
    int countForMinus = 0;
    int countForDecimalPoint = 0;
    
    
    if (isNagetive_PureDecimalNumber(str)) {
        
        countForDecimalPoint++;
        countForMinus++;
        temp += 2;
    }else if (isNagetiveNumber(str)){
    
            countForMinus++;;
            temp++;
    }else if(isPureDecimalNumber(str)){
    
            countForDecimalPoint++;
            temp++;

    }
    
/***
    允许输入数字,负号,小数点
 ***/
    while (*temp != '\0') {
        
        if((*temp >= '0' && *temp<= '9') || *temp == '.') {
            switch (*temp) {
                case '.':
                    countForDecimalPoint++;
                    break;
                default:
                    break;
            }
            temp++;
        }else{
            break;
        }
    }
    
    if ((countForDecimalPoint == 0 || countForDecimalPoint == 1) &&
        (countForMinus == 0 || countForMinus == 1 ) && *temp == '\0')
        return str;
    else
        return NULL;
}


/***
    判断是否为负数
 ***/
bool isNagetiveNumber(char *str){

    bool result = false;
    char *temp = str;
    
    if (*temp == '-') {
        result = true;
    }
    
    return result;
}

/***
    判断是否为纯小数
 ***/
bool isPureDecimalNumber(char *str){

    bool result = false;
    char *temp = str;
    
    if (*temp == '.') {
        result = true;
    }
    
    
    return result;
}

/***
    判断是否为负纯小数
 ***/
bool isNagetive_PureDecimalNumber(char *str){

    bool result = false;
    char position_0 = *str;
    str++;
    char position_1 = *str;
    if (position_0 == '-' && position_1 == '.') {
        result = true;
    }
    
    return result;
}






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值