利用栈实现字符串表达式

上次写的关于字符串表达式计算的方法,逻辑上太过混乱,只是简单的实现!而且还有不少的BUG.写完之后仔细想想计算表达式的方法,才发现一般关于表达式的计算都是利用栈来实现的,这种方法思路很清晰,逻辑很强,而且不容易出错,下面是一个简单的实现:
  1. //设置运算等级
  2. int SetLevel(char op)
  3. {
  4.     int level = 0;
  5.     if ((int)op == 43 || (int)op == 45)
  6.         level = 1;
  7.     if ((int)op == 42 || (int)op == 47)
  8.         level = 2;
  9.     return level;
  10. }
  11. //比较优先级
  12. int Priority(char op_1,char op_2)
  13. {
  14.     int result = 0;
  15.     if (SetLevel(op_1)-SetLevel(op_2) > 0)
  16.         result = 1;
  17.     if (SetLevel(op_1)-SetLevel(op_2) == 0)
  18.         result = 0;
  19.     if (SetLevel(op_1)-SetLevel(op_2) < 0)
  20.         result = -1;
  21.     return result;
  22. }
  23. int calculate(char* str)
  24. {
  25.     stack<int>  stack_Number;
  26.     stack<char> stack_Opreate;
  27.     int result = 0;
  28.     while (*str != '?')
  29.     {
  30.         char temp = 0;
  31.         int i = 0;
  32.         int j = 0;
  33.         //非空栈,将栈内的运算符取出,如果优先级比上一个运算级别高
  34.         //则将数栈连续出栈2次,将2个数用该运算符所计算的值再次压入
  35.         //数栈;如果运算级比上一个级别低,则将级别高的运算符出栈,同
  36.         //样数栈出栈2次参与该运算符号计算,并将值压入数栈,再把该级
  37.         //别低的运算符压入运算符栈;最后当遇到==号时,在按顺序出栈
  38.         //计算.
  39.         char str_Opr = *str;
  40.         switch (str_Opr)
  41.         {
  42.         case '9':case '8':case '7':case '6':case '5':case '4':case '3':case '2':case '1':case '0':
  43.             stack_Number.push(*str-'0');
  44.             break;
  45.         case '+':case '-':case '*':case '/':
  46.             if(stack_Opreate.empty())
  47.             {
  48.                 stack_Opreate.push(*str);//如果是空栈,运算符直接入栈,无需比较优先级
  49.             }
  50.             else
  51.             {
  52.                 temp = stack_Opreate.top();
  53.                 if ( Priority(str_Opr,temp) < 0 )
  54.                 {
  55.                     stack_Opreate.pop();//将级高的出栈,在把级低的入栈
  56.                     stack_Opreate.push('+');
  57.                     i = stack_Number.top();
  58.                     stack_Number.pop();
  59.                     j = stack_Number.top();
  60.                     stack_Number.pop();
  61.                     if (temp == '*')
  62.                         stack_Number.push(i*j);
  63.                     if (temp == '/')
  64.                         stack_Number.push(j/i);
  65.                 }
  66.                 if ( Priority(*str,temp) > 0 )
  67.                 {
  68.                     char new_Opr = *str;//暂存运算符
  69.                     ++str;
  70.                     i = (*str)-'0';
  71.                     j = stack_Number.top();
  72.                     stack_Number.pop();
  73.                     if (new_Opr == '*')
  74.                         stack_Number.push(i*j);
  75.                     if (new_Opr == '/')
  76.                         stack_Number.push(j/i);
  77.                 }
  78.                 if ( Priority(*str,temp) == 0 )
  79.                 {
  80.                     stack_Opreate.push(str_Opr);
  81.                 }
  82.             }
  83.             break;
  84.         case '=':
  85.             while (!stack_Opreate.empty())
  86.             {
  87.                 i = stack_Number.top();
  88.                 stack_Number.pop();
  89.                 j = stack_Number.top();
  90.                 stack_Number.pop();
  91.                 temp = stack_Opreate.top();
  92.                 stack_Opreate.pop();
  93.                 if (temp == '+')
  94.                     stack_Number.push(i+j);
  95.                 if (temp == '-')
  96.                     stack_Number.push(j-i);
  97.             }
  98.             break;
  99.         }
  100.         str++;
  101.     }
  102.     result = stack_Number.top();
  103.     stack_Number.pop();
  104.     return result;
  105. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值