四则运算(未解决)

1086阅读 0评论2012-10-21 时间看来
分类:C/C++


点击(此处)折叠或打开

  1. //3、输入一个表达式,没有括号,数字在0-9之间,输出计算结果,所有的中间结果化为整形。
  2. //例如: 输入:3+8×2/9-2
  3. //输出:2
  4. //函数原型
  5. //public int getMyRet(String str)

  6. #include<stdio.h>
  7. #include<stdlib.h>

  8. //#include <iostream>
  9. //#include <string>
  10. //#include <cstring>
  11. //using namespace std;

  12. int get_my_ret(char *, int);

  13. int
  14. main( int argc, char *argv[] )
  15. {
  16.     //输入字符串
  17.     char my_chars[] = "3+8*2/9-2";
  18.     int my_ret, my_chars_cnt;
  19.     my_chars_cnt = sizeof(my_chars)/sizeof(char);

  20.     //显示需计算的字符串
  21.     int i;
  22.     char *one_char_p = my_chars;
  23.     printf("Need to calculate :\n");
  24.     for(i=0; i<my_chars_cnt; one_char_p++, i++){
  25.         printf("%c", *one_char_p);
  26.     }
  27.     printf("\n");

  28.     //调用计算函数
  29.     my_ret = get_my_ret(my_chars, my_chars_cnt);

  30.     //显示计算结果
  31.     printf("\n my_ret is %d\n", my_ret);
  32. }

  33. int
  34. get_my_ret(char *my_chars_p, int cnt)
  35. {
  36.     int my_ret;
  37.     //获取操作符号个数
  38.     int operations_cnt = 0;
  39.     int i;
  40.     char *cpy_my_chars_p = my_chars_p;
  41.     for(i=0; i<cnt; i++){
  42.         if(*cpy_my_chars_p == '+' || \
  43.                 *cpy_my_chars_p == '-' || \
  44.                 *cpy_my_chars_p == '*' || \
  45.                 *cpy_my_chars_p == '/'){
  46.             operations_cnt++;
  47.         }
  48.         cpy_my_chars_p++;
  49.     }

  50.     //申请数字个数
  51.     int *nums_p_first = (int *) calloc(operations_cnt+1, sizeof(int));
  52.     char *operations_p_first = (char *) calloc(operations_cnt, sizeof(char));

  53.     //将字符串转化为数字存在数组里
  54.     int *nums_p = nums_p_first;
  55.     char *operations_p = operations_p_first;
  56.     cpy_my_chars_p = my_chars_p;

  57.     for(i=0; i<cnt; i++){
  58.         if(*cpy_my_chars_p >= '0' && *cpy_my_chars_p <= '9'){
  59.             *nums_p = *cpy_my_chars_p - '0';
  60.             nums_p++;
  61.         }else{
  62.             *operations_p = *cpy_my_chars_p;
  63.             operations_p++;
  64.         }
  65.         cpy_my_chars_p++;
  66.     }

  67.     //显示获取的操作数
  68.     nums_p = nums_p_first;
  69.     printf("the nums are:\n");
  70.     for(i =0; i<(operations_cnt+1); i++){
  71.         printf("%d, ", *nums_p);
  72.         nums_p++;
  73.     }
  74.     printf("\n");

  75.     //显示获取的操作符号
  76.     operations_p = operations_p_first;
  77.     printf("the oprations are:\n");
  78.     for(i =0; i<(operations_cnt); i++){
  79.         printf("%c, ", *operations_p);
  80.         operations_p++;
  81.     }
  82.     printf("\n");

  83.     //根据两个操作符号,而进行计算
  84.     char *now_opration_p = operations_p_first;
  85.     char *next_opration_p = ++operations_p_first;
  86.     for(i=0; i<operations_cnt; i++){
  87.         //bad beaf
  88.         //bad beaf
  89.         //bad beaf
  90.         //bad beaf
  91.         //bad beaf
  92.         //bad beaf
  93.         //bad beaf
  94.         //bad beaf
  95.     }

  96.     free(nums_p_first);
  97.     free(operations_p_first);
  98.     return my_ret;
  99. }


//强大的网友的答案……难以理解的递归!!!

点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>

  4. //
  5. // 封装常用的判断是否为数字的判断函数,如果是返回1否则0
  6. //
  7. int IsNum(char ch)
  8. {
  9.     return (ch >= '0' && ch <= '9');
  10. }

  11. //
  12. // 把指向Calc的INDEX放于全局变量的位置,因为在递归的过程中需要递增
  13. //
  14. int INDEX = 0;


  15. int Calc(char sub_str[],int first)
  16. {
  17.     //
  18.     // 1.记录下读取到的符号
  19.     //
  20.     char item = sub_str[INDEX];

  21.     char temp[10] = {0};
  22.     char temp_index = 0;

  23.     //
  24.     // 2.如果为终结符,返回first
  25.     //
  26.     if (item == '=' || item == '\0' || item == ')')
  27.     {
  28.         printf("2.return %d\n",first);
  29.         INDEX++;
  30.         return first;
  31.     }

  32.     //
  33.     // 3.1如果为前括号,
  34.     //
  35.     int sub_first = 0;
  36.     if (item == '(')
  37.     {
  38.         INDEX++;
  39.         printf("3.1sub_first = Calc(%s,%d)\n",sub_str,0);
  40.         sub_first = Calc(sub_str,0);
  41.         return Calc(sub_str,sub_first);
  42.     }

  43.     //
  44.     // 3.2如果为数字,读取数字作为first送入
  45.     //
  46.     else if (IsNum(item))
  47.     {
  48.         while (IsNum(sub_str[INDEX]))
  49.         {
  50.             temp[temp_index++] = sub_str[INDEX++];
  51.         }
  52.         sub_first = atoi(temp);

  53.         printf("3.2Calc(%s,%d)\n",sub_str,sub_first);
  54.         return Calc(sub_str,sub_first);
  55.     }

  56.     INDEX++;

  57.     //
  58.     // 4.如果为前括号,把后面的内容作为second
  59.     //
  60.     int second = 0;
  61.     if (sub_str[INDEX] == '(')
  62.     {
  63.         INDEX++;
  64.         printf("4.second = Calc(%s,0);\n",sub_str);
  65.         second = Calc(sub_str,0);
  66.     }

  67.     //
  68.     // 5.如果为数字,把后面的数字作为second
  69.     //
  70.     else
  71.     {
  72.         while (IsNum(sub_str[INDEX]))
  73.         {
  74.             temp[temp_index++] = sub_str[INDEX];
  75.             INDEX++;
  76.         }
  77.         second = atoi(temp);
  78.     }

  79.     //
  80.     // 判断加减乘除的函数,是程序的精华所在
  81.     //
  82.     printf("5.first = %d\titem = %c\tsecond = %d\n", first,item,second);
  83.     if (item == '+' )
  84.     {
  85.         return first + Calc(sub_str, second);
  86.     }
  87.     else if (item == '-')
  88.     {
  89.         return first - Calc(sub_str, second);
  90.     }
  91.     else if (item == '*')
  92.     {
  93.         return Calc(sub_str, first * second);
  94.     }
  95.     else if (item == '/')
  96.     {
  97.         return Calc(sub_str, first / second);
  98.     }
  99. }

  100. //
  101. // 使用递归的思路解决代括号的四则运算的问题
  102. //

  103. int mainn(int argc, char *argv[])
  104. {
  105.     char str[100] = {"((1+((12+50)/2+10))-20*10)+9*(10*(18-11)-20)"};
  106. // char str[100] = {"8+9*5-2/2+6"};
  107.     while (1)
  108.     {
  109.         //
  110.         // 循环输入函数,请输入规范的四则运算式子
  111.         //
  112.         printf("str = %s\n", str);
  113.         int answer = Calc(str,0);

  114.         if(INDEX = strlen(str))
  115.         {
  116.             printf("final_answer = %d\n", answer);
  117.         }
  118.         else
  119.         {
  120.             printf("算式错误,请检查语法\n");
  121.         }

  122.         scanf("%s", str);
  123.         INDEX = 0;
  124.     }
  125.     return 0;
  126. }


上一篇:LDD3 推荐学习笔记——Tekkaman的
下一篇:很美的container_of