剑指offer--面试题49:把字符串转换成整数

感谢CSDN的htyurencaotang,来自http://blog.csdn.net/htyurencaotang/article/details/9883879

C语言的库函数atoi()的作用是将一个字符串转换为整数。写一个函数StrToInt,实现这一功能。

本题的实现中需要注意多种边界问题,非数字字符问题等细节问题。

  1. // 写一个函数StrToInt实现将字符串转换为整数的功能.cpp : 定义控制台应用程序的入口点。  
  2.   
  3. #include "stdafx.h"  
  4. #include <iostream>  
  5. using namespace std;  
  6.   
  7. //特殊情况:1.字符串为空指针或空串  
  8. //2.字符串中有非0到9的字符(负号,#,@,等等)  
  9. //特别需要注意的是:还要考虑溢出的问题。正整数最大值0x7FFFFFFF,负整数最小值0x80000000  
  10. //函数功能:将字符串str转换为整数, 由参数nInt接收整数值,若转换成功返回true,否则返回false  
  11. bool StrToInt(char *str, int &nInt)  
  12. {  
  13.     if (str == "")  
  14.     {  
  15.         cout << "空串!" << endl;    
  16.         return false;  
  17.     }  
  18.     else if (str == NULL)  
  19.     {  
  20.         cout << "空指针!" << endl;  
  21.         return false;  
  22.     }  
  23.     else if ((strcmp(str, "+")==0) || (strcmp(str, "-")==0))  
  24.     {  
  25.         cout << "字符串输入无效!" << endl;  
  26.         return false;  
  27.     }  
  28.     else  
  29.     {  
  30.         char *p = str;  
  31.         bool isFirst = true;//标记是否为字符串的第一个字符  
  32.         bool hasMinus = false;//标记字符串的第一个字符是否为负号  
  33.         nInt = 0;  
  34.   
  35.         while (*p != '\0')  
  36.         {  
  37.             if (isFirst && (*p)=='-')//有负号  
  38.             {  
  39.                 hasMinus = true;  
  40.                 p++;  
  41.                 continue;  
  42.             }  
  43.             else if (isFirst && (*p)=='+')  
  44.             {  
  45.                 p++;  
  46.                 continue;  
  47.             }  
  48.   
  49.             if ((*p) >='0' && (*p) <= '9')//当前字符为数字字符  
  50.             {  
  51.                 nInt = nInt * 10 + (*p) - '0';    
  52.                 if ((!hasMinus && nInt>0x7FFFFFFF) || (hasMinus && nInt<(signed int)0x80000000))//注意此处(signed int)  
  53.                 {  
  54.                     cout << "字符串数值溢出,输入无效!" << endl;  
  55.                     return false;  
  56.                 }  
  57.                 p++;  
  58.             }  
  59.             else//当前字符为非数字字符  
  60.             {  
  61.                 cout << "字符串中包含有非数字的字符,不能转换为数字!" << endl;  
  62.                 return false;  
  63.             }       
  64.         }  
  65.   
  66.         if (hasMinus)//字符串有负号  
  67.         {  
  68.             nInt = (-1) * nInt;  
  69.         }  
  70.   
  71.         return true;    
  72.     }     
  73. }  
  74.   
  75. int _tmain(int argc, _TCHAR* argv[])  
  76. {  
  77.   
  78. int nTest1 = 100000000000000;  
  79. int nTest2 = -1000000000000000;  
  80.      if(nTest1 > 0x7FFFFFFF)  
  81.      {  
  82.          cout << "上溢!" << endl;  
  83.      }  
  84.   
  85.      if (nTest2 < (signed int)0x80000000)  
  86.      {  
  87.          cout << "下溢!" << endl;  
  88.      }  
  89.           
  90.   
  91.     int nInt = 0;  
  92.     char *str = NULL;  
  93.     if (StrToInt("123", nInt))  
  94.     {  
  95.         cout << nInt << endl;  
  96.     }  
  97.   
  98.     if (StrToInt("", nInt))//空串  
  99.     {  
  100.         cout << nInt << endl;  
  101.     }  
  102.   
  103.     if (StrToInt(str, nInt))//空指针  
  104.     {  
  105.         cout << nInt << endl;  
  106.     }  
  107.   
  108.     if (StrToInt("-123", nInt))  
  109.     {  
  110.         cout << nInt << endl;  
  111.     }  
  112.   
  113.     if (StrToInt("+123", nInt))  
  114.     {  
  115.         cout << nInt << endl;  
  116.     }  
  117.   
  118.     if (StrToInt("-12#3@", nInt))  
  119.     {  
  120.         cout << nInt << endl;  
  121.     }  
  122.   
  123.     if (StrToInt("0", nInt))  
  124.     {  
  125.         cout << nInt << endl;  
  126.     }  
  127.   
  128.     if (StrToInt("+", nInt))  
  129.     {  
  130.         cout << nInt << endl;  
  131.     }  
  132.   
  133.     if (StrToInt("100000000000000000000000000000000000000", nInt))  
  134.     {  
  135.         cout << nInt << endl;  
  136.     }  
  137.   
  138.     if (StrToInt("-100000000000000000000000000000000000000", nInt))  
  139.     {  
  140.         cout << nInt << endl;  
  141.     }  
  142.   
  143.     system("pause");  
  144.     return 0;  
  145. }  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值