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

全部博文(168)

文章存档

2014年(6)

2013年(74)

2012年(71)

2011年(17)

分类: C/C++

2013-08-08 04:22:03

今天熬夜了......
之前错误的代码还在上面,当时给自己一个警醒吧
......


睡 .....

点击(此处)折叠或打开

  1. /*************************************************************************
  2. 简单四则运算
  3. 问题描述:
  4. 输入一个只包含个位数字的简单四则运算表达式字符串,计算该表达式的值
  5. 注:
  6. 3.1、表达式只含 +, -, *, / 四则运算符,不含括号
  7. 3.2、表达式数值只包含个位整数(0-9),且不会出现0作为除数的情况
  8. 3.3、要考虑加减乘除按通常四则运算规定的计算优先级
  9. 3.4、除法用整数除法,即仅保留除法运算结果的整数部分。比如8/3=2。输入表达式保证无0作为除数情况发生
  10. 3.5、输入字符串一定是符合题意合法的表达式,其中只包括数字字符和四则运算符字符,除此之外不含其它任何字符,不会出现计算溢出情况
  11. 要求实现函数:
  12. int calculate(int len,char *expStr)
  13. 【输入】 int len: 字符串长度;
  14. char *expStr: 表达式字符串;
  15. 【输出】 无
  16. 【返回】 计算结果
  17. 示例:
  18. 1)输入:char *expStr = “1+4*5-8/3”
  19. 函数返回:19
  20. 2)输入:char *expStr = “8/3*3”
  21. 函数返回:6
  22. ********************************************************************/

  23. #include<stdio.h>
  24. #include<string.h>
  25. /**********************************************
  26. 编程思路:
  27. .统计运算符
  28. .运算的时候判断运算符
  29.     如果是 乘或除 ,那么将两边的数字转换后运算
  30.     如果是 加或减 , 那么再判断下一个运算符是否是乘除
  31. .通过移位继续算

  32. 出错的地方:
  33. 本来想这么修改字符串的,将后面的覆盖前面,strcpy(symbol,&symbol[1]);不可以这么做,就算自己实现的my_strcpy也不可以
  34.         可见dst, src在内存重叠时候不允许连续赋值
  35. 后来没办法只好自己手动覆盖,正确的应该是i + 2<= len1 ,有等号,若没等号的话,后面还会有很多不应该有的符号

  36. **********************************************/

  37. void my_strcpy(char *dst, char *src)
  38. {
  39.     while(src != '\0')
  40.         *dst++ = *src++;
  41.     *dst = '\0';
  42. }

  43. int calculate(int len,char *expStr)
  44. {
  45.     int len1, len2, i, j, num, num2;
  46.     
  47.     char *tmp;
  48.     char *symbol;//存储符号肯定够了    

  49.     if((tmp = (char *)malloc(sizeof(char) * (len + 1))) == NULL)
  50.         exit(1);
  51.     if((symbol = (char *)malloc(sizeof(char) * (len + 1))) == NULL)
  52.         exit(1);

  53.     strcpy(tmp, expStr);
  54.     tmp[len] = '\0';
  55.     
  56.     for(i = 0, j = 0; i < len; i++)
  57.     {
  58.         if((tmp[i] == '+') || (tmp[i] == '-') || (tmp[i] == '*') || (tmp[i] == '/'))
  59.             symbol[j++] = tmp[i];
  60.     }

  61.     symbol[j] = '\0';
  62.     
  63.     num = tmp[0] - '0';
  64.     
  65.     if(strlen(tmp) == 1)
  66.         return tmp[0] - '0';//
  67.     else
  68.     {    
  69.         while((len1 = strlen(tmp)) != 1)//字符数组长度为一时结束
  70.         {
  71.             len2 = strlen(symbol);
  72.             if(symbol[0] == '*')
  73.             {                        
  74.                 num = num * (tmp[2] - '0');
  75.                 for(i = 1; i + 2<= len1; i++)
  76.                     tmp[i] = tmp[i+2];
  77.                 //my_strcpy(&tmp[1], &tmp[3]);
  78.                 for(i = 0; i + 1 <= len2; i++)
  79.                     symbol[i] = symbol[i+1];
  80.                 //strcpy(symbol,&symbol[1]);
  81.             }//
  82.             
  83.             if(symbol[0] == '/')
  84.             {    
  85.                 num = num / (tmp[2] - '0');
  86.                 
  87.                 for(i = 1; i + 2<= len1; i++)
  88.                     tmp[i] = tmp[i+2];
  89.                         
  90.         //        my_strcpy(&tmp[1], &tmp[3]);
  91.     //            strcpy(symbol,&symbol[1]);
  92.             for(i = 0; i + 1 <= len2; i++)
  93.                     symbol[i] = symbol[i+1];
  94.             }//
  95.             
  96.             if(symbol[0] == '+')
  97.             {    
  98.                 num2 = tmp[2] - '0';
  99.                 while((strlen(symbol) > 1) && ((symbol[1] == '*') || (symbol[1] == '/')))
  100.                 {
  101.                     if(symbol[1] == '*')
  102.                     {
  103.                         num2 = num2 * (tmp[4] - '0');
  104.                         for(i = 3; i + 2<= len1; i++)
  105.                             tmp[i] = tmp[i+2];
  106.                         for(i = 1; i + 1 <= len2; i++)
  107.                             symbol[i] = symbol[i+1];    
  108.                     //    my_strcpy(&tmp[3], &tmp[5]);
  109.                     //    my_strcpy(&symbol[1],&symbol[2]);
  110.                     }
  111.                     if(symbol[1] == '/')
  112.                     {
  113.                         num2 = num2 / (tmp[4] - '0');
  114.                         for(i = 3; i + 2<= len1; i++)
  115.                             tmp[i] = tmp[i+2];
  116.                         for(i = 1; i + 1 <= len2; i++)
  117.                             symbol[i] = symbol[i+1];                        
  118.                     //    my_strcpy(&tmp[3], &tmp[5]);
  119.                     //    my_strcpy(&symbol[1], &symbol[2]);
  120.                     }
  121.                 }
  122.     
  123.                 num = num + num2;
  124.     //            printf("%d",num);
  125.                 
  126.                 for(i = 1; i + 2<= len1; i++)
  127.                     tmp[i] = tmp[i+2];
  128.             //    my_strcpy(&tmp[1], &tmp[3]);
  129.             //    strcpy(symbol,&symbol[1]);
  130.                 for(i = 0; i + 1 <= len2; i++)
  131.                     symbol[i] = symbol[i+1];

  132.             }//
  133.             
  134.             if(symbol[0] == '-')
  135.             {    
  136.                 num2 = tmp[2] - '0';
  137.                 while((strlen(symbol) > 1) && ((symbol[1] == '*') || (symbol[1] == '/')))
  138.                 {
  139.                     if(symbol[1] == '*')
  140.                     {
  141.                         num2 = num2 * (tmp[4] - '0');
  142.                         for(i = 3; i + 2<= len1; i++)
  143.                             tmp[i] = tmp[i+2];
  144.                         for(i = 1; i + 1 <= len2; i++)
  145.                             symbol[i] = symbol[i+1];                        
  146.                     //    my_strcpy(&tmp[3], &tmp[5]);
  147.                     //    my_strcpy(&symbol[1],&symbol[2]);
  148.                     }
  149.                     if(symbol[1] == '/')
  150.                     {
  151.                         num2 = num2 / (tmp[4] - '0');
  152.                         for(i = 3; i + 2<= len1; i++)
  153.                             tmp[i] = tmp[i+2];
  154.                         for(i = 1; i + 1 <= len2; i++)
  155.                             symbol[i] = symbol[i+1];                        
  156.                     //    my_strcpy(&tmp[3], &tmp[5]);
  157.                     //    my_strcpy(&symbol[1],&symbol[2]);
  158.                     }
  159.                 }
  160.     
  161.                 num = num - num2;
  162.                 for(i = 1; i + 2<= len1; i++)
  163.                     tmp[i] = tmp[i+2];
  164.                 //strcpy(symbol, &symbol[1]);
  165.                 for(i = 0; i + 1 <= len2; i++)
  166.                     symbol[i] = symbol[i+1];
  167.             }//
  168.         }
  169.     }    

  170.     return num;    
  171. }

  172. int main(int argc, char **argv)
  173. {    
  174.     int result, n;
  175.     //char *expStr = "1+4*5-8/3";
  176.     char *expStr = "1*5+7/3-2+6";
  177.     n = strlen(expStr);
  178.     
  179.     result = calculate(n, expStr);
  180.     
  181.     printf("reslt = %d" , result);
  182.     
  183.     while(1);
  184. }

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