For simple arithmetic, postfix expressions can be used to quickly calculate results by using stacks (stack)
================================== I'm a split line ====================================
Definition of suffix:
e.g. 2 + 3, 2 3 +
2 + 3 * 4-2 3 4 * +
Apply stack to calculate suffix expression:
e.g. suffix expression 6 5 2 3 + 8 * + 3 + *
Traversal: 6 push (6) Stack:6
5 push (5) Stack:6 5
2 push (2) Stack:6 5 2
3 Push (3) Stack:6 5 2 3
+ Pop, Pop 2, 3 out of stack, operation ans = 2 + 3; push (ANS) stack:6 5 5
8 Push (8) Stack:6 5 5 8
* Pop, Pop 5, 8 out of the stack, operation ans = 5 * 8; push (ANS) stack:6 5 40
+ Pop, pop 5, 40 out of stack, operation ans = 5 + +; push (ans) stack:6 45
3 Push (3) Stack:6 45 3
+ Pop, pop 45, 3 out of stack, operation ans = 3; push (ans) Stack:6 48
* Pop, pop 6, 48 out of the stack, operation ans = 6 *, push (ans) stack:288
Converts an infix expression to a suffix expression:
Setting: Priority ' (' > ' * ' = '/' > ' + ' = '-')
① reads the character of the input queue, judging whether it is a number or a symbol +-*/()
② If a number, put it in the output queue
③ if ') ', the symbol one by one in the stack is ejected to the output queue until the first ' (', and ' (' not placed in the output queue ') is encountered
④ if the other symbol is +-*/(, the element in the stack pops up until a lower-priority symbol is found or ' ('). ' (' only when encountering ') ' only pops up
Code Implementation :
1 //2016-03-162 //infix suffix3 4 //Limitations: Enter a range of legal, digital 0~95#include <iostream>6#include <string>7#include <stack>8 9 using namespacestd;Ten One intMain () { A stringinput, output; -stack<Char>operators; - while(Cin >>input) { the if(Input = ="0") Break; - output.clear (); - for(inti =0; I < input.size (); i++) { - CharCH =Input[i]; + if(Ch >='0'&& CH <='9') output.push_back (CH); - Else if(ch = ='+'|| ch = ='-') { + while(!operators.empty () && (operators.top () = ='*'|| Operators.top () = ='/'|| Operators.top () = ='-'|| Operators.top () = ='+')) { A Output.push_back (Operators.top ()); at Operators.pop (); - } - Operators.push (CH); - } - Else if(ch = ='*'|| ch = ='/') { - while(!operators.empty () && (operators.top () = ='*'|| Operators.top () = ='/')) { in Output.push_back (Operators.top ()); - Operators.pop (); to } + Operators.push (CH); - } the Else if(ch = =')') { * while(Operators.top ()! ='('&&!Operators.empty ()) { $ Output.push_back (Operators.top ());Panax Notoginseng Operators.pop (); - } the Operators.pop (); + } A Else if(ch = ='(') { the Operators.push (CH); + } - } $ while(!Operators.empty ()) { $ Output.push_back (Operators.top ()); - Operators.pop (); - } thecout <<"Output:"<< Output <<Endl; - }Wuyi return 0; the}
Test :
Limitations: (not strong enough)
① only implements the digital input of the 0~9, if more than two digits are required to make a character judgement (whether the previous character is a number, if so, then the current character and the previous one is the same integer)
② does not do the final result calculation, because it is still a string, if necessary, to do the character to integer conversion judgment.
③ No anomaly detection (assuming all inputs are valid, illegal input will directly result in incorrect output)
some bugs:
No clear () in the ①stack container;
② judgment Condition while (!operators.empty () && (operators.top () = = ' * ' | | operators.top () = = '/') must first be judged by the!operators.empty ()
Otherwise the first judgment (operators.top () = = ' * ' | | operators.top () = = '/') is actually the top () of the read operation, when the stack is empty, the runtime error
"C + +" is picking up the flower--infix suffix