C + + implementation Inverse Polish expression calculation problem
#include <iostream>#include<string>using namespacestd;classstack{Private: intsize; inttop; float*Listarray; Public: Stack (intsz= -); ~Stack (); BOOLPushfloatIT);//into the stack BOOLPopfloat& it);//out of the stack BOOLIsEmpty ();//determine if the stack is empty BOOLIsone ();//determine if an element is in the stack}; Stack::stack (intSz//Stack Constructors{size=sz; Top=0; Listarray=New float[size];}BOOLStack::p Ush (floatit) { if(top==size)return false; Listarray[top++]=it; return true;}BOOLStack::p op (float&it) { if(top==0) return false; It=listarray[--top]; return true;}BOOLStack::isempty ()//determine if the station is empty{ if(top==0) return true; return false;}BOOLStack::isone () {if(top==1) return true; return false;} Stack::~Stack () {DeleteListarray;}//This function passes in the input string, and the string into the//the results (function sound) are scanned and processed accordingly.//Ming)voidComputeChar*str); intMain () {Charstr[ -]; Cin.getline (str, -,'#'); Compute (STR); return 0;}//This function passes in the input string, and the string into the//The results (function body) are scanned and processed accordingly .voidComputeChar*str) {Stack astack; floatx=0, y=0, S1,s2,temp; inti; I=0; while(Str[i]) {Switch(Str[i]) { Case '+'://addition Operation if(Astack.isone () | |Astack.isempty ()) {cout<<"expression does not meet the requirements"; return; } astack.pop (S1); Astack.pop (S2); X=s2+S1; Astack.push (x); X=0; i++; Break; Case '-'://Subtraction Operations if(Astack.isone () | |Astack.isempty ()) {cout<<"expression does not meet the requirements"; return; } astack.pop (S1); Astack.pop (S2); X=s2-S1; Astack.push (x); X=0; I++; Break; Case '*'://Multiplication Operations if(Astack.isone () | |Astack.isempty ()) {cout<<"expression does not meet the requirements"; return; } astack.pop (S1); Astack.pop (S2); X=s2*S1; Astack.push (x); X=0; I++; Break; Case '/'://Division Operation if(Astack.isone () | |Astack.isempty ()) {cout<<"expression does not meet the requirements"; return; } astack.pop (S1); Astack.pop (S2); if(s1==0) {cout<<"the denominator is 0! "<<Endl; return; } x=s2/S1; Astack.push (x); X=0; I++; Break; Case ' '://if it is a space, put the data x into the stack if(str[i-1]>= -&&str[i-1]<= $) Astack.push (x); X=0; I++; Y=0; Break; Case '.'://get a decimal parttemp=10.0; while(str[++i]!=' ') { if(str[i]>= -&&str[i]<= $) y=y+ (str[i]- -)/temp; Temp*=Ten; } x+=y; Break; default://converts a character number to a floating-point number if(str[i]>= -&&str[i]<= $) {x=x*Ten+str[i]- -; I++; } } }//if the stack is only tangent to one element, it will lose//the resultsif(Astack.isone ()) {astack.pop (x); cout<< Str <<'='<< x <<Endl; }}
C + + implementation Inverse Polish expression calculation problem