PRoblem Description 對于一個基于二元運算符的算術表達式,轉換為對應的后綴式,并輸出之。 Input 輸入一個算術表達式,以‘#’字符作為結束標志。 Output
輸出該表達式轉換所得到的后綴式。 Example Input
a*b+(c-d/e)*f#Example Output
ab*cde/-f*+Hint
Author
#include <stdio.h>#include<math.h>#include <stack>#include <iostream>#include <algorithm>#include <bits/stdc++.h>using namespace std;int main(){ stack <char> p; char kk; while(kk=getchar()) { if(kk>='a'&&kk<='z'||kk>='A'&&kk<='Z') { printf("%c", 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=='/') { p.push(kk); } else if(kk=='(') { p.push(kk); } else if(kk==')') { while(p.top()!='(') { printf("%c", p.top()); p.pop(); } p.pop(); } else if(kk=='#') { while(!p.empty()) { printf("%c", p.top()); p.pop(); } break; } } return 0;}新聞熱點
疑難解答