• 表达式求值


    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <stack>
    using namespace std;
    const int maxn=1000+5;
    string s1,s2;
    stack<char> s;
    stack<double> c;
    
    void init()
    {
        while(!s.empty())
            s.pop();
        while(!c.empty())
            c.pop();
    }
    
    int pro(char ch)
    {
        switch(ch)
        {
            case '+':
            case '-':return 1;
            case '*':
            case '/':return 2;
            default :return 0;
        }
    }
    
    void deal()
    {
        init();
        int i=0,len=s1.length();
        s.push('#');
        while(i<len-1)
        {
            if(s1[i]=='(')
                s.push(s1[i++]);
            else if(s1[i]==')')
            {
                while(s.top()!='(')
                {
                    s2+=s.top();
                    s2+=' ';
                    s.pop();
                }
                s.pop();
                i++;
            }
            else if(s1[i]=='+'||s1[i]=='-'||s1[i]=='*'||s1[i]=='/')
            {
                while(pro(s.top())>=pro(s1[i]))
                {
                    s2+=s.top();
                    s2+=' ';
                    s.pop();
                }
                s.push(s1[i]);
                i++;
            }
            else
            {
                while(s1[i]<='9'&&s1[i]>='0'||s1[i]=='.')
                    s2+=s1[i++];
                s2+=' ';
            }
        }
        while(s.top()!='#')
        {
            s2+=s.top();
            s.pop();
            s2+=' ';
        }
    }
    
    double countt()
    {
    
        int len=s2.length(),i=0;
        double y,x;
        while(i<len)
        {
            if(s2[i]==' ')
                i++;
            else
            {
                switch(s2[i])
                {
                    case '+':x=c.top();c.pop();x+=c.top();c.pop();i++;break;
                    case '-':x=c.top();c.pop();x=c.top()-x;c.pop();i++;break;
                    case '*':x=c.top();c.pop();x*=c.top();c.pop();i++;break;
                    case '/':x=c.top();c.pop();x=c.top()/x;c.pop();i++;break;
                    default :
                    {
                        x=0.0;
                        while(s2[i]<='9'&&s2[i]>='0')
                            x=x*10+(s2[i++]-'0');
                        if(s2[i]=='.')
                        {
                            i++;
                            double k=10.0;y=0.0;
                            while(s2[i]<='9'&&s2[i]>='0')
                            {
                                y+=(s2[i++]-'0')/k;
                                k*=10;
                            }
                            x+=y;
                        }
                    }
                }
                c.push(x);
            }
        }
        return c.top();
    }
    void solve(){
        cin>>s1;
        s2="";
        deal();
        printf("%.2lf
    ",countt());
    }
    int main() {
        int t=1;
        while(t--) {
            solve();
        }
        return 0;
    }

    #include <iostream>#include <cstdio>#include <cstring>#include <stack>using namespace std;const int maxn=1000+5;string s1,s2;stack<char> s;stack<double> c;
    void init(){    while(!s.empty())        s.pop();    while(!c.empty())        c.pop();}
    int pro(char ch){    switch(ch)    {        case '+':        case '-':return 1;        case '*':        case '/':return 2;        default :return 0;    }}
    void deal(){    init();    int i=0,len=s1.length();    s.push('#');    while(i<len-1)    {        if(s1[i]=='(')            s.push(s1[i++]);        else if(s1[i]==')')        {            while(s.top()!='(')            {                s2+=s.top();                s2+=' ';                s.pop();            }            s.pop();            i++;        }        else if(s1[i]=='+'||s1[i]=='-'||s1[i]=='*'||s1[i]=='/')        {            while(pro(s.top())>=pro(s1[i]))            {                s2+=s.top();                s2+=' ';                s.pop();            }            s.push(s1[i]);            i++;        }        else        {            while(s1[i]<='9'&&s1[i]>='0'||s1[i]=='.')                s2+=s1[i++];            s2+=' ';        }    }    while(s.top()!='#')    {        s2+=s.top();        s.pop();        s2+=' ';    }}
    double countt(){
        int len=s2.length(),i=0;    double y,x;    while(i<len)    {        if(s2[i]==' ')            i++;        else        {            switch(s2[i])            {                case '+':x=c.top();c.pop();x+=c.top();c.pop();i++;break;                case '-':x=c.top();c.pop();x=c.top()-x;c.pop();i++;break;                case '*':x=c.top();c.pop();x*=c.top();c.pop();i++;break;                case '/':x=c.top();c.pop();x=c.top()/x;c.pop();i++;break;                default :                {                    x=0.0;                    while(s2[i]<='9'&&s2[i]>='0')                        x=x*10+(s2[i++]-'0');                    if(s2[i]=='.')                    {                        i++;                        double k=10.0;y=0.0;                        while(s2[i]<='9'&&s2[i]>='0')                        {                            y+=(s2[i++]-'0')/k;                            k*=10;                        }                        x+=y;                    }                }            }            c.push(x);        }    }    return c.top();}void solve(){    cin>>s1;    s2="";    deal();    printf("%.2lf ",countt());}int main() {    int t=1;    while(t--) {        solve();    }    return 0;}

  • 相关阅读:
    iOS11自定义tabBar重影问题
    iOS打包时遇到的has one iOS Distribution certificate but its private key is not installed的问题
    MAC本地生成SSH KEY的方法
    Mac版Sourcetree的安装使用
    Xcode报错:could not attach to pid:"1764"
    解决Xcode10 Library not loaded: /usr/lib/libstdc++.6造成的crash及报错
    socket调试工具(Mac版)
    iOS-基于TCP连接<Scoket-服务端>
    iOS自定义TabBar使用popToRootViewControllerAnimated返回后tabbar重叠
    [UIApplication sharedApplication].keyWindow.rootViewController
  • 原文地址:https://www.cnblogs.com/gggyt/p/7181831.html
Copyright © 2020-2023  润新知