Did the problem after the expression was evaluated.
infix-type variable suffix
Time limit: ms | Memory limit:65535 KB
Difficulty:3
Describe
People's Daily habits are written in the arithmetic expression infix, but for the machine is more "accustomed to" suffix, about the arithmetic expression of the infix and suffix of the general data structure of the book has relevant content to see, here no longer repeat, now your task is to change infix type suffix.
Input
The first line enters an integer n, and there are n sets of test data (N<10).
Each set of test data has only one row, is a string of not more than 1000, indicating the infix of the expression, each expression is "=" end. This expression contains only +-*/and parentheses. Where parentheses can be used in a nested set. The data guarantees that no negative numbers appear in the operands entered.
Data guarantee Divisor is not 0
Output
Each group outputs the corresponding suffix of the group infix, requiring the adjacent operand operators to be separated by a space.
Sample input
21.000+2/4= ((1+2) *5+1)/4=
Sample output
1.000 2 4/+ = 1 2 + 5 * 1 + 4/=
#include <iostream> #include <cstring> #include <string> #include <stack >using namespace std;int prio (char x) {if (' + ' ==x | | '-' ==x ' return 1;else if (' * ' ==x | | '/' ==x) return 2;return 0;} Int main () {int n,i;string s1,s2;stack<char> s;cin>>n;while (n--) {cin>>s1; S2= ""; for (i=0,s.push (' = '), I<s1.length () -1;i++) {if (' (' ==s1[i]) S.push (S1[i]); Else if (') ' ==s1[i]) {while ( S.top ()! = ' (') {s2+=s.top (); s2+= ' '; S.pop ();} S.pop ();} Else if (' + ' ==s1[i] | | '-' ==s1[i] | | ' * ' ==s1[i] | | '/' ==s1[i]) {while (Prio (S.top ()) >=prio (S1[i]))//!!! This place is easy to prio (S1[i]) written s1[i]{s2+=s.top (); s2+= ' '; S.pop ();} S.push (S1[i]);} Else{while (IsDigit (s1[i]) | | '. ' == s1[i]) s2+=s1[i++];i--;s2+= ' ';}} while (S.top ()!= ' = ') {s2+=s.top (); s2+= ' '; S.pop ();} S2+=s.top (); S.pop (); Cout<<s2<<endl;} return 0;}
Nyoj 467 infix-type variable suffix