C ++ Exercise | based infix arithmetic expression evaluation stack (double type

#include<iostream>
#include<stack>
#include<cmath>
using namespace std;
char ch;

bool f(char t)
{
    if(t=='+'||t=='-'||t=='*'||t=='/'||t=='('||t==')'||t=='=')
        return 1;
    return 0;
}

double cal(double t1,double t2,char c)
{
    switch(c)
    {
        case '+':return t1+t2;
        case '-':return t1-t2;
        case '*':return t1*t2;
        case '/':return t1/t2;
    }
    return 0;
}

int cmp(char t1,char t2)
{
    int q1=0,q2=0;
    switch(t1)
    {
        case '+':q1=1;break;
        case '-':q1=1;break;
        case '*':q1=2;break;
        case '/':q1=2;break;
        case '(':q1=0;break;
        case ')':q1=-1;break;
    }
    switch(t2)
    {
        case '+':q2=1;break;
        case '-':q2=1;break;
        case '*':q2=2;break;
        case '/':q2=2;break;
        case '(':q2=0;break;
        case ')':q2=-1;break;
    }
    if(q1==-1&&q2==0)
        return 0;
    if(q1>q2)
        return 2;
    if(q1<=q2)
        return 1;
    return -1;
}

double z()
{
    double ans=0;
    int a[1000],i=0,flag=0;
    a[++i]=ch-'0';
    while(cin>>ch&&!f(ch))
    {
        if(ch=='.')
        {
            flag=i;
            continue;
        }
        a[++i]=ch-'0';
    }
    if(flag==0)
        flag=i;
    for(int j=1;j<=i;j++)
    {
        years + = a [j] * pow ( 10 , FLAG j);
    }
    return years;
}

int main ()
{
    Stack < Double > opnd; // store digital 
    Stack < char > OPTR; // store operator 
    Double T1, T2, T3;
     the while (CIN >> CH = CH &&! ' = ' )
    {
        if(f(ch))
        {
            OPTR.push(ch);
            cin>>ch;
        }
        else
        {
            OPND.push(z());
        }
        while(ch!='=')
        {
            if(f(ch))
            {
                if(OPTR.empty())
                {
                    OPTR.push(ch);
                    cin>>ch;
                    continue;
                }
                IF (CMP (CH, OPTR.top ()) == . 1 ) // after the priority is lower, store 
                {
                    OPTR.push(ch);
                    cin>>ch;
                    continue;
                }
                the else  IF (CMP (CH, OPTR.top ()) == 2 ) // big priority after calculating 
                {
                     char C1 = CH;
                    cin>>ch;
                    t2=z();
                    t1=OPND.top();
                    OPND.pop();
                    t3=cal(t1,t2,c1);
                    OPND.push(t3);
                }
                else
                {
                    OPTR.pop();
                    cin>>ch;
                }
            }
            else
            {
                OPND.push(z());
            }
        }
        while(!OPTR.empty())
        {
            ch=OPTR.top();
            OPTR.pop();
            t2=OPND.top();
            OPND.pop();
            t1=OPND.top();
            OPND.pop();
            t3=cal(t1,t2,ch);
            OPND.push(t3);
        }
        printf("%.2lf\n",OPND.top());
        OPND.pop();
        getchar();
    }
    return 0;
}

Probably focus on that data reception

Guess you like

Origin www.cnblogs.com/tsj816523/p/11802111.html