Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1291749
  • 博文数量: 168
  • 博客积分: 2124
  • 博客等级: 大尉
  • 技术积分: 2590
  • 用 户 组: 普通用户
  • 注册时间: 2011-09-16 23:51
文章分类

全部博文(168)

文章存档

2014年(6)

2013年(74)

2012年(71)

2011年(17)

分类: C/C++

2013-08-08 11:05:53

早上快十点,宿管阿姨带着几个大叔,破门而入!  --查水表
        要不估计还起不来呢,起来以后做了这样一道题目

点击(此处)折叠或打开

  1. /****************************************************
  2. 1. 字符串提取数字
  3. 完成函数
  4. void take_num(const char *strIn, int *n, unsigned int *outArray)
  5. 输入
  6. strIn="ab00cd+123fght456-25 3.005fgh"
  7. 输出
  8. n=6
  9. outArray={ 0, 123, 456, 25, 3, 5 }
  10. (不考虑小数:如3.005输出3和5)
  11. **********************************************************/

  12. #include<stdio.h>
  13. #include<string.h>
  14. /****************************************************************
  15. 编程思路:
  16. .从开始历遍,直到遇到数字
  17. .遇到数字后开始进行字符串转换为整数,知道遇到下一个字符不是数字
  18. ***************************************************************/

  19. void take_num(const char *strIn, int *n, unsigned int *outArray)
  20. {
  21.     int i, j, sum, len;
  22.     
  23.     j = 0;
  24.     len = strlen(strIn);
  25.     
  26.     for(i = 0; i < len; )
  27.     {    
  28. //        sum = 0;
  29.         if((strIn[i] <= '9') && ((strIn[i]) >= '0'))
  30.         {    
  31.             outArray[j] = 0;
  32.             while((strIn[i] <= '9') && ((strIn[i]) >= '0'))
  33.             {
  34.                 outArray[j] = outArray[j] * 10 + strIn[i] - '0';
  35.                 i++;
  36.             }
  37.             j++;
  38.         }
  39.         else
  40.             i++;
  41.     }
  42.     
  43.     *n = j;
  44. }

  45. int main(int argc, char **argv)
  46. {
  47.     int n, i;
  48.     unsigned int outArray[100] = {0};
  49.     char *strIn="ab00cd+123fght456-25 3.005fgh";
  50.     
  51.     take_num(strIn, &n, outArray);
  52.     
  53.     printf("n = %d \n", n);

  54.     for(i = 0; i < n; i++)
  55.         printf("outArray[%d] = %d\n", i, outArray[i]);
  56.     
  57.     while(1);
  58.         
  59. }

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