把字符串转换为整数

来源:互联网 发布:小班亲子美工纸盘教案 编辑:程序博客网 时间:2024/05/22 00:28

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

[cpp] view plain copy
  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. isFirst=false;
  40.                 hasMinus = true;  
  41.                 p++;  
  42.                 continue;  
  43.             }  
  44.             else if (isFirst && (*p)=='+')  
  45.             {  
  46.                 p++;  
  47. isFirst=false;
  48.                 continue;  
  49.             }  
  50.   
  51.             if ((*p) >='0' && (*p) <= '9')//当前字符为数字字符  
  52.             {  
  53.                 nInt = nInt * 10 + (*p) - '0';    
  54.                 if ((!hasMinus && nInt>0x7FFFFFFF) || (hasMinus && nInt<(signed int)0x80000000))//注意此处(signed int)  
  55.                 {  
  56.                     cout << "字符串数值溢出,输入无效!" << endl;  
  57.                     return false;  
  58.                 }  
  59.                 p++;  
  60.             }  
  61.             else//当前字符为非数字字符  
  62.             {  
  63.                 cout << "字符串中包含有非数字的字符,不能转换为数字!" << endl;  
  64.                 return false;  
  65.             }       
  66.         }  
  67.   
  68.         if (hasMinus)//字符串有负号  
  69.         {  
  70.             nInt = (-1) * nInt;  
  71.         }  
  72.   
  73.         return true;    
  74.     }     
  75. }  
  76.   
  77. int _tmain(int argc, _TCHAR* argv[])  
  78. {  
  79.   
  80. int nTest1 = 100000000000000;  
  81. int nTest2 = -1000000000000000;  
  82.      if(nTest1 > 0x7FFFFFFF)  
  83.      {  
  84.          cout << "上溢!" << endl;  
  85.      }  
  86.   
  87.      if (nTest2 < (signed int)0x80000000)  
  88.      {  
  89.          cout << "下溢!" << endl;  
  90.      }  
  91.           
  92.   
  93.     int nInt = 0;  
  94.     char *str = NULL;  
  95.     if (StrToInt("123", nInt))  
  96.     {  
  97.         cout << nInt << endl;  
  98.     }  
  99.   
  100.     if (StrToInt("", nInt))//空串  
  101.     {  
  102.         cout << nInt << endl;  
  103.     }  
  104.   
  105.     if (StrToInt(str, nInt))//空指针  
  106.     {  
  107.         cout << nInt << endl;  
  108.     }  
  109.   
  110.     if (StrToInt("-123", nInt))  
  111.     {  
  112.         cout << nInt << endl;  
  113.     }  
  114.   
  115.     if (StrToInt("+123", nInt))  
  116.     {  
  117.         cout << nInt << endl;  
  118.     }  
  119.   
  120.     if (StrToInt("-12#3@", nInt))  
  121.     {  
  122.         cout << nInt << endl;  
  123.     }  
  124.   
  125.     if (StrToInt("0", nInt))  
  126.     {  
  127.         cout << nInt << endl;  
  128.     }  
  129.   
  130.     if (StrToInt("+", nInt))  
  131.     {  
  132.         cout << nInt << endl;  
  133.     }  
  134.   
  135.     if (StrToInt("100000000000000000000000000000000000000", nInt))  
  136.     {  
  137.         cout << nInt << endl;  
  138.     }  
  139.   
  140.     if (StrToInt("-100000000000000000000000000000000000000", nInt))  
  141.     {  
  142.         cout << nInt << endl;  
  143.     }  
  144.   
  145.     system("pause");  
  146.     return 0;  
  147. }  

运行结果:


 说明:在判断上溢、下溢时出现错误!不知缘由,请大牛指正!



转载:http://blog.csdn.net/htyurencaotang/article/details/9883879



原创粉丝点击