c ++ Handwriting Calculator

Teacher class data structure arranged jobs so that they write a calculator, fractional arithmetic, and requires support brackets, conceived when brushing your teeth in the morning and found two stacks can be achieved.

Wherein a stack is used to keep the operator, a stack number for the deposit operation by the input stack infix expressions into postfix computer it is easy to handle, when the operator kept out stack pop operators We calculate the first two deposit the operand stack, the operation result onto the stack.

Specific codes are as follows

 

#include<iostream>
#include<cstring>
#include<stack>
using namespace std;

stack<double> num;
stack<char> sign;

void calc(char c){
    double a,b;
    a=num.top();
    num.pop();
    b=num.top();
    num.pop();
    if(c=='+')
        num.push(a+b);
    else if(c=='-')
        num.push(b-a);
    else if(c=='*')
        num.push(a*b);
    if(c=='/')
        num.push(b/a);
}

double solve(string s){

    char top;
    double ans=0;
    char c;
    double n=0;
    int len=s.length();
    double m=1,d=0;
    for(int i=0;i<len;i++){
        c=s[i]; 
        if(c>=48&&c<=57){
            if(d==0.0){
                n=n*m+int(c-48);
                m*=10;
            }
            else{
                n=n+d*int(c-48);
                d*=0.1;
            }
        }
        else if(c=='.'){
            d=0.1;
        }
        else if(c=='+'||c=='-'){
            if(s[i-1]!=')')
                num.push(n);
            n=0;
            m=1;
            d=0;
            if(!sign.empty()){
                top=sign.top();
                while((top=='*'||top=='/'||(top=='-'&&c=='-'))&&!sign.empty()){
                    calc(top);
                    sign.pop();
                    if(!sign.empty())
                        top=sign.top();
                }
            }
            sign.push(c);
        }
        else  if(c=='*'||c=='/'){
            if(s[i-1]!=')')
                num.push(n);
            n=0;
            m=1;
            d=0;
            if(!sign.empty()){
                top=sign.top();
                while((top=='/'&&c=='/')&&!sign.empty()){
                    calc(top);
                    sign.pop();
                    if(!sign.empty())
                        top=sign.top();
                }
            }
            sign.push(c);
        }
        else if(c=='('){
            sign.push(c);
        }
        else if(c==')'){
            num.push(n);
            if(!sign.empty())
                top=sign.top();
            while(top!='('){
                calc(top);
                sign.pop();
                top=sign.top();
            }
            sign.pop();
        }
        else{
            printf("Error! Illgal input!EXIT_");
            return 0;
        }
    }
    if(s[len-1]!=')')
        num.push(n);
    while(!sign.empty()){
        top=sign.top();
        calc(top);
        sign.pop();
    };
    ans=num.top();
    return ans;
}

int main(){
    cout<<"Please input what u want to calculate:";
    string s;
    cin>>s;
    //s="(5.3-9)*2.6/1.3+4";
    cout<<solve(s);
    return 0;
}

 

Basically simple operation

 

 

 

 Proven results are correct

Guess you like

Origin www.cnblogs.com/r3t7rn/p/11717492.html