Title Description:
By converting the middle order to the post-order type, it is not necessary to deal with the sequence of operators, as long as it is read by the operator.
Topic Analysis:
Operation from the front of the post-processing start reading, encountered the operator first deposited into the stack, if encountered operator, then the stack is taken out of two operators to do the corresponding operation, and then stored back to the stack, if the expression is completed, then the stack top value is the answer, for example, we calculate 12+34+* this expression ( 1+2) * (3+4)):
Read |
Stack |
1 |
1 |
2 |
1 2 |
+ |
3//1+2 after storage back |
3 |
3 3 |
4 |
3 3 4 |
+ |
3 7//3+4 back |
* |
21//3 * 7 after Save back |
Program code:
#include <gtest/gtest.h>using namespacestd;intGetoperatorprior (Charvalue) { intNresult =0; Switch(value) { Case '+': Case '-': {nresult=1; } Break; Case '*': Case '/': {nresult=2; } Break; } returnNresult;}BOOLConverttopostfix (Const string& Infixexp,string&postfixexp) {postfixexp.clear (); int* Pstack =New int[Infixexp.size ()]; intNTop =-1; for(string:: Size_type i=0; I < infixexp.size (); i++) { CharCvalue =Infixexp[i]; Switch(cvalue) { Case '(': {pstack[++ntop] =Cvalue; } Break; Case ')': { while((NTop >=0) && Pstack[ntop]! ='(') {Postfixexp+=Pstack[ntop]; --NTop; } //Not find ' (', Express is invalid. if(NTop <0) { return false; } --NTop; } Break; Case '+': Case '-': Case '*': Case '/': { while((NTop >=0) &&Getoperatorprior (Pstack[ntop])>=Getoperatorprior (Cvalue)) {Postfixexp+=Pstack[ntop]; --NTop; } pstack[++ntop] =Cvalue; } Break; default: Postfixexp+=Cvalue; Break; } } while(NTop >=0) {Postfixexp+ = pstack[ntop--]; } return true;}BOOLCalcvalue (DoubleV1,DoubleV2,Double& Value,CharExpress) { BOOLBresult =true; Switch(Express) { Case '+': Value= V1 +v2; Break; Case '-': Value= V1-v2; Break; Case '*': Value= V1 *v2; Break; Case '/': if(Fabs (v2) < 1e- the) { return false; } Value= V1/v2; Break; default: Bresult=false; Break; } returnBresult;}BOOLExpresscalc (Const string& Express,Double&value) { stringpostfixexpress; if(!Converttopostfix (Express, Postfixexpress)) { return false; } Double* Operandstack =New Double[Postfixexpress.size ()]; intNTop =-1; for(string:: Size_type i =0; I < postfixexpress.size (); ++i) {CharCvalue =Postfixexpress[i]; Switch(cvalue) { Case '+': Case '-': Case '*': Case '/': { //Operand Not enough if(NTop <1) { return false; } DoubleDresult; if(! Calcvalue (operandstack[ntop-1], Operandstack[ntop], Dresult, cvalue)) {return false; } operandstack[ntop-1] =Dresult; --NTop; } Break; default: if(Cvalue <'0'&& cvalue >'9') { return false; } operandstack[++ntop] = cvalue-'0'; Break; } } if(NTop >=1) { return false; } Value= operandstack[0]; return true;} TEST (Algo, Texpresscalc) {// //Postfix Convert// //a+b*d+c/d = abd*+cd/+ stringstrresult; Converttopostfix ("A+B*D+C/D", strresult); Assert_eq ("abd*+cd/+", strresult); //(a+b) *c/d+e = ab+c*d/e+Converttopostfix ("(a+b) *c/d+e", strresult); Assert_eq ("ab+c*d/e+", strresult); //((a) +b* (c-d) +e/f) *g = abcd-*+ef/g*Converttopostfix ("((a) +b* (c-d) +e/f) *g", strresult); Assert_eq ("abcd-*+ef/+g*", strresult); //1+3*4+2/5 = 13.4 DoubleDresult =0.0; Expresscalc ("1+3*4+2/5", Dresult); Assert_double_eq (13.4, Dresult); //(4+6) *1/9+7 = 8.1111111111111111111111111111111Expresscalc ("(4+6) *1/9+7", Dresult); Assert_double_eq (8.1111111111111111111111111111111, Dresult); //(5) +2* (1-7) +3/8) -26.5Expresscalc ("(5) +2* (1-7) +3/8)", Dresult); Assert_double_eq (-26.5, Dresult);}
[Classical algorithm] post-op operation