字符串转数字

来源:pongo的编程挑战题目-字符串转数字。

http://hero.pongo.cn/Question/Details?ID=47&ExamID=45


该题的关键是要注意处理溢出的问题。

个人觉得,溢出主要有两种情况:

1)本次计算出的数值比上次的数值还小,那肯定是溢出了。

2)本次计算出的数值经反算后,与上次的数值不等,那肯定是溢出了。


现附上代码,通过本人能想到的所有测试用例。是否还有未考虑到的情况,望大家指正。

(csdn上显示以下代码时,会漏掉一些空格。复制代码测试时请注意)


#include<stdio.h>

#include<stdlib.h>

#include<string.h>


int StrToInt(const char* str)

{

   int result =0;

   int sign =1;

   int temp;

   if (str !=NULL){

       while(*str ==' ')

            str++;

       if (*str =='-' || *str =='+'){

           if (*str =='-')

                sign = -1;

            str++;

        }

       while(*str){

           if (*str <'0' || *str >'9')

               break;

            

            //越加越少,那肯定是溢出了

           if (result *10 + (*str -'0') < result){

                result =0x80000000;

               break;

            }

            

            temp = result *10 + (*str -'0');

            

            //计算后的结果,再算回去,如果与上次的结果不等,那肯定也是溢出了

           if (((temp - (*str -'0')) /10) != result){

                result =0x80000000;

               break;

            }

                

            result = temp;

            

            str++;

        }

    }

    

   if (sign <0){

       if (result !=0x80000000)

            result = - result;

    }

   else  if (sign >0){

       if (result ==0x80000000)

            result =0x7fffffff;

    }       

    

   return result;

}



//start 提示:自动阅卷起始唯一标识,请勿删除或增加。

int main()

{

   //

   printf("%d\n",StrToInt(""));

   printf("%d\n",StrToInt("1"));

   printf("%d\n",StrToInt("+1"));

   printf("%d\n",StrToInt("-1"));

   printf("%d\n",StrToInt("123"));

    printf("%d\n",StrToInt("-123"));

   printf("%d\n",StrToInt("010"));

    printf("%d\n",StrToInt("+00131204"));

    printf("%d\n",StrToInt("-01324000"));

    printf("%d\n",StrToInt("2147483647"));

    printf("%d\n",StrToInt("-2147483647"));

    printf("%d\n",StrToInt("-2147483648"));

    printf("%d\n",StrToInt("2147483648"));

    printf("%d\n",StrToInt("-2147483649"));

   printf("%d\n",StrToInt("abc"));

    printf("%d\n",StrToInt("-abc"));

   printf("%d\n",StrToInt("1a"));

    printf("%d\n",StrToInt("23a8f"));

    printf("%d\n",StrToInt("-3924x8fc"));

    printf("%d\n",StrToInt("   321"));

    printf("%d\n",StrToInt("   -321"));

    printf("%d\n",StrToInt("123 456"));

    printf("%d\n",StrToInt("123  "));

   printf("%d\n",StrToInt("  - 321"));    

    printf("%d\n",StrToInt("  +4488"));

    printf("%d\n",StrToInt("  + 413"));

    printf("%d\n",StrToInt("  ++c"));

    printf("%d\n",StrToInt("  ++1"));

    printf("%d\n",StrToInt("  --2"));

    printf("%d\n",StrToInt("  -2"));

    printf("%d\n",StrToInt("21474836481"));

    printf("%d\n",StrToInt("-21474836492"));

    printf("%d\n",StrToInt("-9147483649"));

    printf("%d\n",StrToInt("9147483649"));

    printf("%d\n",StrToInt("10522545454999876"));

    printf("%d\n",StrToInt("-1152921504606846976"));

    

}

//end //提示:自动阅卷结束唯一标识,请勿删除或增加。


评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值