pipioj 1291 中缀表达式转后缀表达式I

 1 #define bug(x) cout<<#x<<" is "<<x<<endl
 2 #include<bits/stdc++.h>
 3 using namespace std;
 4 const int N=1000;
 5 stack<char>s;
 6 int n;
 7 char t[N];
 8 void solve(){
 9     for(int i=1;i<=n;i++){
10         if(t[i]>='a'&&t[i]<='z')printf("%c",t[i]);
11         else if(t[i]=='('){
12             s.push(t[i]);
13         }
14         else if(t[i]=='^'){
15             while(!s.empty()&&s.top()=='^'){
16                 printf("%c",s.top());
17                 s.pop();
18             }
19             s.push(t[i]);
20         }
21         else if(t[i]=='*'||t[i]=='/'){
22             while(!s.empty()&&(s.top()=='^'||s.top()=='*'||s.top()=='/')){
23                 printf("%c",s.top());
24                 s.pop();
25             }
26             s.push(t[i]);
27         }
28         else if(t[i]=='+'||t[i]=='-'){
29             while(!s.empty()&&s.top()!='('){
30                 printf("%c",s.top());
31                 s.pop();
32             }
33             s.push(t[i]);
34         }
35         else{
36             while(!s.empty()&&s.top()!='('){
37                 printf("%c",s.top());
38                 s.pop();
39             }
40             s.pop();
41         }
42     }
43     while(!s.empty()){
44         printf("%c",s.top());
45         s.pop();
46     }
47 }
48 int main(){
49     scanf("%s",t+1);
50     n=strlen(t+1);
51     solve();
52     printf("
");
53 }
54 /*
55 -a^e+b*c/d
56 a*(b^c)+d*e
57 a^b^c*d
58 */
原文地址:https://www.cnblogs.com/ccsu-kid/p/13493908.html