洛谷 P1175 表達式的轉換

https://www.luogu.com.cn/problem/P1175

  1 #define bug(x) cout<<#x<<" is "<<x<<endl
  2 #include<bits/stdc++.h>
  3 using namespace std;
  4 #define pa pair<int,char>
  5 #define mk make_pair
  6 #define se second
  7 #define fi first
  8 const int N=2e6+10;
  9 struct node{
 10     int x;
 11     char c;
 12 };
 13 stack<char>s;
 14 stack<pa>s1,s2;
 15 queue<char>q;
 16 queue<pa>ans1;
 17 int n;
 18 char t[N];
 19 char ans[N];
 20 int b[N];
 21 int cal(int x,int y,char c){
 22     if(c=='+')return x+y;
 23     else if(c=='-')return x-y;
 24     else if(c=='*')return x*y;
 25     else if(c=='/')return x/y;
 26     else{
 27         int res=1;
 28         while(y--){
 29             res*=x;
 30         }
 31         return res;
 32     }
 33 } 
 34 
 35 void solve(){
 36     for(int i=1;i<=n;i++){
 37         if(t[i]>='0'&&t[i]<='9')q.push(t[i]);
 38         else if(t[i]=='(')s.push(t[i]);
 39         else if(t[i]=='^'){
 40             while(!s.empty()&&s.top()=='^'){
 41                 q.push(s.top());
 42                 s.pop();
 43             }
 44             s.push(t[i]);
 45         }
 46         else if(t[i]=='*'||t[i]=='/'){
 47             while(!s.empty()&&(s.top()=='^'||s.top()=='*'||s.top()=='/')){
 48                 q.push(s.top());
 49                 s.pop();
 50             }
 51             s.push(t[i]);
 52         }
 53         else if(t[i]=='+'||t[i]=='-'){
 54             while(!s.empty()&&s.top()!='('){
 55                 q.push(s.top());
 56                 s.pop();
 57             }
 58             s.push(t[i]);
 59         }
 60         else{
 61             while(!s.empty()&&s.top()!='('){
 62                 q.push(s.top());
 63                 s.pop();
 64             }
 65             if(s.top()=='(')s.pop();
 66         }
 67     }
 68     while(!s.empty()){
 69         q.push(s.top());
 70         s.pop();
 71     }
 72     int cnt=0,res=0,tot=0,f=0,c1=0;
 73     while(!q.empty()){
 74         if(q.front()<'0'||q.front()>'9')tot++;
 75         ans[++cnt]=q.front();
 76         ans1.push(mk(123321,ans[cnt]));
 77 
 78         printf("%c ",q.front());
 79         q.pop();
 80     }
 81     printf("\n");
 82     while(tot--){
 83         f=0;
 84         while(!ans1.empty()){
 85             if(!f&&ans1.front().se!='A'&&(ans1.front().se<'0'||ans1.front().se>'9')){
 86                 f=1;
 87                 pa p1=s1.top();
 88                 s1.pop();
 89                 pa p2=s1.top();
 90                 s1.pop();
 91                 int x=0,y=0;
 92                 if(p2.fi==123321)x=p2.se-'0';
 93                 else x=p2.fi;
 94                 if(p1.fi==123321)y=p1.se-'0';
 95                 else y=p1.fi;
 96                 res=cal(x,y,ans1.front().se);
 97                 pa p3=mk(res,'A');
 98                 s1.push(p3);
 99             }
100             else{
101                 s1.push(ans1.front());
102             }
103             ans1.pop();
104         }
105         while(!s1.empty()){
106                 s2.push(s1.top());
107                 s1.pop();
108             }
109         while(!s2.empty()){
110             pa p1=s2.top();
111             s2.pop();
112             ans1.push(p1);
113             if(p1.fi!=123321)printf("%d ",p1.fi);
114             else printf("%c ",p1.se);
115         }
116         printf("\n");
117     }
118 }
119 int main(){
120     scanf("%s",t+1);
121     n=strlen(t+1);
122     solve();
123 }
124 /*
125 8-(3+2*6)/5+4
126 1+2*(3+4)/6-5
127 3*4^(1+2)/3/8(1-4+2*5)-2^(1+2*3)
128 
129 */

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章