Chinaunix首页 | 论坛 | 博客
  • 博客访问: 505817
  • 博文数量: 184
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1172
  • 用 户 组: 普通用户
  • 注册时间: 2016-06-21 13:40
个人简介

技术改变命运

文章分类

全部博文(184)

文章存档

2020年(16)

2017年(12)

2016年(156)

我的朋友

分类: C/C++

2016-07-02 16:17:27

题目描述:
输入一个由数字组成的字符串,请把它转化成整数并输出.eg:输入字符串“123”,输出123
1分代码:

点击(此处)折叠或打开

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4. int fun(int a,int b)
  5. {
  6.     if (b == 0) return 1;
  7.     else
  8.         return a*fun(a,b-1);
  9. }
  10. int strToInt(char *str)
  11. {
  12.     if (str == NULL)
  13.         return -1;
  14.     int i = 0,j = 0;
  15.     int result = 0;
  16.     int length = strlen(str);
  17.     for (i = length-1;i>=0;i--)
  18.     {
  19.         int c = str[i] - '0';//刚开始还忽略了这一步
  20.         result += c*fun(10,length-i-1);
  21.         printf("result = %d\n",result);
  22.         
  23.     
  24.         
  25.     }
  26.     return result;
  27. }
  28. int main()
  29. {
  30.     char str[1024];
  31.     gets(str);
  32.     int result = strToInt(str);
  33.     printf("转化的整数是%d\n",result);
  34.     return 0;
  35. }
3‘程序:

点击(此处)折叠或打开

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4. /*int fun(int a,int b)
  5. {
  6.     if (b == 0) return 1;
  7.     else
  8.         return a*fun(a,b-1);
  9. }*/
  10. int strToInt(char *str)
  11. {
  12.     if (str == NULL)
  13.         return -1;
  14.     int i = 0,j = 0;
  15.     int result = 0;
  16.     int length = strlen(str);
  17.     for (i = 0;str[i] != 0;i++)
  18.     {
  19.         int c = str[i] - '0';
  20.         result = result * 10+c;
  21.     }
  22.     return result;
  23. }
  24. int main()
  25. {
  26.     char str[1024];
  27.     gets(str);
  28.     int result = strToInt(str);
  29.     printf("转化的整数是%d\n",result);
  30.     return 0;
  31. }
10’程序:(处理溢出(最难的部分),空格,符号(‘+’,‘-’),空)

点击(此处)折叠或打开

  1. int strToInt(const char *str)
  2. {
  3.     static const int MAX_INT = (int)((unsigned)~0 >> 1);//~的优先级高于>>
  4.     static const int MIN_INT = -(int)((unsigned)~0 >> 1);//
  5.     unsigned int n = 0;
  6.     if ( str == NULL)
  7.     {
  8.             return 0;
  9.     }
  10.     if (isspace(*str))//空格
  11.     {
  12.         ++str;
  13.     }
  14.     int sign = 1;
  15.     if (*str == '-')
  16.     {
  17.         sign = -1;
  18.         ++str;
  19.     }
  20.     char c;
  21.     while (isdigit(*str))
  22.     {
  23.         c = *str - '0';
  24.         if (sign > 0 && (n > MAX_INT/10 || (n == MAX_INT/10 && c > MAX_INT%10)))
  25.         {
  26.             n = MAX_INT;
  27.             break;
  28.         }
  29.         if (sign < 0 && (n > (unsigned)MIN_INT/10 || (n == (unsigned)MIN_INT/10 && c > (unsigned)MIN_INT %10)))//这里 (unsigned)的使用
  30.         {
  31.             n = MIN_INT;
  32.             break;
  33.         }
  34.         n = n*10+c;
  35.         ++str;
  36.     }
  37.     return sign > 0?n:-n;
  38. }




阅读(628) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~