栈的应用 表达式求值

支持 + - * / ( )符号,每个数必须是0~9,输入时以#符号结尾

#include <iostream>
#include <stdio.h> 
#include <string> 
#include <stack> 
using namespace std; 

int main()
{
    string ss; 
    while(cin>>ss)
    {
                  stack<char> s1;
                  stack<char> s2;
                  stack<int> s3;
                  stack<char> temp; 
                  temp.push('#');
                  int j, i;
                  j = ss.length();
                  for (i = 0; i < j; i++)
                  {
                      char c;
                      c = ss[i];
                      if ((c >= '0') && (c <= '9'))
                      s1.push(c);
                      else
                      {
                          if ((temp.top() == '#') || (c == '(') || (temp.top() == '('))
                          {
                                         temp.push(c);
                                         continue;
                          }
                          if (((temp.top() == '+') || (temp.top() == '-')) && ((c == '*') || (c == '/')))
                          {
                                           temp.push(c);
                                           continue;
                          }
                           if (((temp.top() == '+') || (temp.top() == '-')) && ((c == '+') || (c == '-')))
                           {
                                            s1.push(temp.top());
                                            temp.pop();
                                            temp.push(c);
                                            continue;
                           }
                           if (((temp.top() == '*') || (temp.top() == '/')) && ((c == '+') || (c == '-')))
                           {
                                            s1.push(temp.top());
                                            temp.pop();
                                            temp.push(c);
                                            continue;
                           }
                           if (((temp.top() == '*') || (temp.top() == '/')) && ((c == '*') || (c == '/')))
                           {
                                            s1.push(temp.top());
                                            temp.pop();
                                            temp.push(c);
                                            continue;
                           }
                           if (c == ')')
                           {
                                 while (1)
                                 {
                                       s1.push(temp.top());
                                       temp.pop();
                                       if (temp.top() == '(')
                                       {
                                                      temp.pop();
                                                      break;
                                       }
                                 }
                                 continue;
                           }
                           if (c == '#')
                           {
                                 while (1)
                                 {
                                       //printf("hello");
                                       s1.push(temp.top());
                                       temp.pop();
                                       if (temp.top() == '#')
                                       {
                                                      break;
                                       }
                                 }
                                 break;
                           }
                      }
                  }
                  j = s1.size(); 
                  for (i = 0; i < j; i++)
                  {
                      char cc;
                      cc = s1.top();
                      s2.push(cc);
                      s1.pop();
                  }
                  j = s2.size(); 
                  for (i = 0; i < j; i++)
                  {
                      char cc;
                      cc = s2.top();
                      s2.pop(); 
                      if ((cc >= '0') && (cc <= '9')) 
                      {
                             int a;
                             a = cc - 48;
                             s3.push(a);
                      }
                      else
                      {
                          int b, c, d;
                          c = s3.top();
                          s3.pop();
                          b = s3.top();                          
                          s3.pop();
                          if (cc == '+')
                          d = b + c;
                          if (cc == '-')
                          d = b - c;
                          if (cc == '*')
                          d = b * c;
                          if (cc == '/')
                          d = b / c;
                          s3.push(d);

                      }
                  }
                  printf("%d\n", s3.top());
    }
    return 0; 
} 


思路:先把中缀表达式转为后缀,再利用后缀表达式求值

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值