【数据结构】中缀表达式求值

来源:互联网 发布:苏州关键词优化 编辑:程序博客网 时间:2024/05/17 14:21

04:中缀表达式的值

  • 查看
  • 提交
  • 统计
  • 提问
总时间限制: 
200ms 
内存限制: 
1024kB
描述
人们熟悉的四则运算表达式称为中缀表达式,例如(23+34*45/(5+6+7))。在程序设计语言中,可以利用堆栈的方法把中缀表达式转换成保值的后缀表达式(又称逆波兰表示法),并最终变为计算机可以直接执行的指令,得到表达式的值。

给定一个中缀表达式,编写程序,利用堆栈的方法,计算表达式的值。
输入
第一行为测试数据的组数N
接下来的N行,每行是一个中缀表达式。表达式中只含数字、四则运算符和圆括号,操作数都是正整数,数和运算符、括号之间没有空格。中缀表达式的字符串长度不超过600。
输出
对每一组测试数据输出一行,为表达式的值
样例输入
33+5*8(3+5)*8(23+34*45/(5+6+7))
样例输出
4364108
提示
注意:运算过程均为整数运算(除法运算'/'即按照C++定义的int除以int的结果,测试数据不会出现除数为0的情况),输出结果也为整数(可能为负)。

中间计算结果可能为负。


#include <iostream>  #include<stack>  #include<string>  using namespace std;  stack <char> p;  stack <int> q;  string s;  void add()  {      int a=0,b=0;      char c;      c=p.top();      p.pop();      a=q.top();      q.pop();      b=q.top();      q.pop();      if(c=='+')      {          a=b+a;          q.push(a);      }      else if(c=='-')      {          a=b-a;          q.push(a);      }      else if(c=='*')      {          a=a*b;          q.push(a);      }      else if(c=='/')      {          a=b/a;          q.push(a);      }  }  int main()  {      int n,i,len,m;      cin>>n;      while(n--)      {          cin>>s;          len=s.length();          for(i=0;i<len;i++)          {              if(s[i]<48&&s[i]!=')')              {                  if(s[i]=='('||p.empty())                  {                      p.push(s[i]);                  }                  else                  {                      if(s[i]=='*'||s[i]=='/')                      {                          if(p.top()=='+'||p.top()=='-'||p.top()=='(')                              p.push(s[i]);                          else                          {                              if(!p.empty())                              {                                  while(p.top()!='+'||p.top()!='-'||p.top()!='(')                                  {                                      add();                                      if(p.empty())                                          break;                                      else if(p.top()=='+'||p.top()=='-'||p.top()=='(')                                          break;                                  }                                  p.push(s[i]);                              }                          }                      }                      else if(s[i]=='+'||s[i]=='-')                      {                          if(p.top()=='(')                          {                              p.push(s[i]);                          }                          else                          {                              if(!p.empty())                              {                                  while(p.top()!='('||!p.empty())                                  {                                      add();                                      if(p.empty())                                          break;                                      else if(p.top()=='(')                                          break;                                  }                                  p.push(s[i]);                              }                          }                      }                  }              }              else if(s[i]==')')              {                  while(p.top()!='(')                  {                      add();                      if(p.top()=='(')                          break;                  }                  p.pop();              }              else              {                  m=0;                  while((s[i]>=48&&s[i]<=57))                  {                      m=m*10+s[i]-'0';                      i++;                  }                  q.push(m);                  i--;              }          }          while(!p.empty())          {              add();          }          cout<<q.top()<<endl;      }      return 0;  }  


原创粉丝点击