A simple arithmetic suffix, and an evaluation of the suffix. has been tested
infix turn suffix:
#include <queue>
#include <iostream>
#include <stdlib.h>
#include <string>
#include <string.h>
#include <sstream>
#include <stack>
Static stack<string> ss_out; the element used to hold the suffix expression when the//suffix is evaluated static stack<string> ss_optr; //infix suffix is used to store operators static queue<string> qs; //The element used to store suffix expression when infix suffix string:: Const_iterator IT;
BOOLIsnum (it it) {if(*it-'0'>=0&& *it-'9'<=0) return true; Else return false;}//The operator between the Operation Ching () is ejected and pressed into the output queue;voidGetparenth (void){ for(; Ss_optr.top ()! ="("; Qs.push (Ss_optr.top ()), Ss_optr.pop ()) {} if(Ss_optr.top () = ="(") Ss_optr.pop ();}voidPush_qs (void) {Qs.push (Ss_optr.top ()); Ss_optr.pop ();}voidPush_optr_top (it&bit) {Ostringstream oss; OSS<< *it; stringtmp=Oss.str (); Ss_optr.push (TMP);}//Priority Processing: +-*/simple arithmetic, directly listed, you can also refer to the encyclopedia of the two-dimensional array to prioritize the methodvoidPush_optr (it&it) { if(*it = ='(') Push_optr_top (IT); if(*it = ='/'){ while(!ss_optr.empty () && ss_optr.top () = ="/") Push_qs (); Push_optr_top (IT); } if(*it = ='*'){ while(!ss_optr.empty () && (ss_optr.top () = ="/"|| Ss_optr.top () = ="*") ) Push_qs (); Push_optr_top (IT); } if(*it = ='-'){ while(!ss_optr.empty () && (ss_optr.top () = ="/"|| Ss_optr.top () = ="*"|| Ss_optr.top () = ="-") ) Push_qs (); Push_optr_top (IT); } if(*it = ='+'){ while(!ss_optr.empty () && ss_optr.top ()! ="(") Push_qs (); Push_optr_top (IT); }}//infix suffixvoidIntopost (string Const&str_in) { for(IT it_in = Str_in.begin (); It_in!=str_in.end (); + +it_in) {Ostringstream oss; while(Isnum (it_in)) {OSS << *it_in; if(! Isnum (it_in+1) || it_in+1==Str_in.end ()) { stringTMP =Oss.str (); Qs.push (TMP); //encountering symbols converts the output stream into a string into the output queue; Break; } ++it_in; } if(!Isnum (it_in)) { if(*it_in==')') Getparenth (); //The operator between the Operation Ching () is ejected and pressed into the output queue; Else{push_optr (it_in); } } } while(!Ss_optr.empty ()) {Qs.push (Ss_optr.top ()); Ss_optr.pop (); }}
Suffix evaluation, only write support int type, did not write the template, will read:
voidclear_s (stack<string>&SS) { while(!Ss.empty ()) {Ss.pop (); }}//Stack top 2 element operation, and the result into the stackvoidCalculate (it& it,stack<string>&ss_out) { stringStr_tmp =Ss_out.top (); intresult = Atoi (Str_tmp.c_str ());//The top element of the stack is converted to int;Ss_out.pop (); Str_tmp=Ss_out.top (); Ss_out.pop (); Switch(*it) { Case '+': result = Atoi (Str_tmp.c_str ()) + result; Break; Case '-': result = Atoi (Str_tmp.c_str ())-result; Break; Case '*': result = Atoi (STR_TMP.C_STR ()) * result; Break; Case '/': result = Atoi (Str_tmp.c_str ())/result; Break; } ostringstream oss; OSS<<result; Str_tmp= Oss.str ();//The result of the calculation is converted to stringSs_out.push (str_tmp);}//Suffix expression evaluationvoidPost_val (string Const&str_post) {clear_s (ss_out); for(IT it_out = Str_post.begin (); It_out!=str_post.end (); + +it_out) {Ostringstream oss; while(Isnum (It_out)) {OSS << *it_out; if(! Isnum (it_out+1)){ stringTMP =Oss.str (); Ss_out.push (TMP); //encountering symbols converts the numbers in the output stream into strings into stacks; } ++it_out; } if(!Isnum (it_out)) { if(*it_out = =',') Continue; ElseCalculate (it_out,ss_out); } }}
Test:
voidShow_post () { while(!Qs.empty ()) {cout<< Qs.front () <<" "<<Flush; Qs.pop (); } cout<<Endl;}voidTest () {stringStr_post; stringstr_in; cout<<"Input infix expression:"<<Endl; CIN>>str_in; Intopost (str_in); Show_post (); cout<<"Input postfix expression:"<<Endl; CIN>>Str_post; Post_val (Str_post); cout<< ss_out.top () <<Endl;}intMainvoid) {test (); return 0;}
For example, input 13+ (30-((600/2/15-2-3*4)/2) *10-50) output is 13 30 600 2/15 2-3 4 *-2/10 *-50-+
Input: 2/15 2-3 4 *-2/10 *--+ output:-37
Improved:
1. Operator precedence processing can be more general and optimized
2. Check the legality of user input
3. Evaluation can be written in the form of a template to more general
Write a lot of C, C + +, feel different, C + + simplifies the pointer used very comfortable, but C is often more simple rough, really troublesome.
Inverse Polish expression