C + + implementation Inverse Polish expression calculation problem

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.