The first step is to convert the infix expression to a suffix expression. The transformation of this step can be said to be the core of the subject.
The main means of conversion is the use of stacks, there are several rules:
- Digital Direct output
- "(" directly into the stack
- ") to stack the elements in the stack until you encounter the" ("
- Other operators need to take precedence over the top element of the stack, and if the top element of the stack is less than equal to the operator to be manipulated, it needs to be out of the stack and output. Until the top of the stack has precedence over the element to be processed
- Finally, the elements in the stack need to be emptied, all output
intToInt (string inch){ intrst; StringStream SS; SS<<inch; SS>>rst; returnrst;}intPriorityChara) { Switch(a) { Case '*':return 2; Case '/':return 2; Case '+':return 1; Case '-':return 1; Case '(':return 3; Case ')':return 3; }}BOOLIsdig (Chara) { if(a>='0'&&a<='9')return true; Else return false;}//ensure that each entry of the symbol priority is higher than the current stack top element, if the top of the stack at this point of precedence than the stack element is lower or equal, you need to stack//knowing that there is a higher priority than the current need to enter the stack elementvoidMidtopost (string inch,vector<string>&VEC) {Stack<Char>s; stringrst=""; intI=0; while(true) { if(i>=inch. Length ()) Break; if(Isdig (inch[i])) {stringnum=""; while(Isdig (inch[i])) num+=inch[i++]; Vec.push_back (num); } Else { if(S.empty ()) S.push (inch[i++]); Else { if(inch[i]=='(') {S.push (inch[i]);} Else if(inch[i]==')') { while(S.top ()! ='(') { stringtemp=""; Temp+=S.top (); Vec.push_back (temp); S.pop (); } s.pop (); } Else { if(Priority (inch[i]) >priority (S.top ()) | | S.top () = ='(') S.push (inch[i]); Else { //determine if the null must be written in front, in line with the principle of short circuit while(!s.empty () && (inch[i]) <=Priority (S.top ()))) { stringtemp=""; Temp+=S.top (); Vec.push_back (temp); S.pop (); } s.push (inch[i]); } } ++i; } } } //Empty Stack while(!S.empty ()) { stringtemp=""; Temp+=S.top (); Vec.push_back (temp); S.pop (); }}//the calculation of the suffix expression, the number into the stack, the symbol will stack top two elements out of the stack, the operation of the backward stackintCalc (vector<string>&VEC) {Stack<int>s; for(intI=0; I<vec.size (); + +i) {if(!vec[i].compare ("*")) { intx=S.top (); S.pop (); inty=S.top (); S.pop (); S.push (x*y); } Else if(!vec[i].compare ("-")) { intx=S.top (); S.pop (); inty=S.top (); S.pop (); S.push (y-x); } Else if(!vec[i].compare ("+")) { intx=S.top (); S.pop (); inty=S.top (); S.pop (); S.push (x+y); } Else if(!vec[i].compare ("/")) { intx=S.top (); S.pop (); inty=S.top (); S.pop (); S.push (y/x); } Else{s.push (ToInt (Vec[i])); } } returns.top ();}intMain () {string inch="+ (3-1) *3+10/2"; //string s= "9 3 1-3 * + 2/+";vector<string>VEC; Midtopost (inch, VEC); cout<<calc (VEC) <<Endl; return 0; }
C + + leverages stacks to solve computational problems