后缀表达式转中缀表达式

发布时间:2019年08月30日 阅读:260 次

http://acm.sdut.edu.cn/onlinejudge2/index.php/Home/Index/problemdetail/pid/2132.html

#include <iostream>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#include<algorithm>
#include<queue>
#include<deque>
#include<stack>
#include<stdio.h>
#define cmax 100003

using namespace std;
//综上出栈情况分为右括号,加减号和输入结束
int main()
{
  stack<char>p;
  char kk;
  while(kk=getchar())
  {

     if((kk>='a'&&kk<='z')||(kk>='A'&&kk<='Z'))
     {
        cout<<kk;
     }
     else if(kk=='+'||kk=='-')//出现加减号时,根据优先级原则,将高优先级的符号全部出栈
     {
        while(!p.empty()&&((p.top()=='*')||p.top()=='/'))
        {printf("%c",p.top());
         p.pop();
        }
        p.push(kk);//之后将该符号入栈
     }
     else if(kk=='('||kk=='*'||kk=='/')
     {
       p.push(kk);

     }
     else if(kk==')')//当出现一个右括号时,打印出栈中除左括号外的所有符号,并清空栈
     {
        while(p.top()!='(')
        {
            cout<<p.top();
            p.pop();

        }
        p.pop();


     }
     else if(kk=='#')//打印出之前入栈的所有符号
     {
        while(!p.empty())
        {
           cout<<p.top();
           p.pop();
        }
        break;
     }
  }
  return 0;

}





/***************************************************
User name: jk160505徐红博
Result: Accepted
Take time: 0ms
Take Memory: 160KB
Submit time: 2017-01-16 11:04:08
****************************************************/


Tag:
相关文章

发表评论: