⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 1237.cpp

📁 杭电 acm部分代码 有兴趣的可以下载 谢谢
💻 CPP
字号:
#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<stack>
using namespace std;
int cmp(char a,char b){
    if(a=='*'||a=='/')
        return 1;
    else if(a=='+'||a=='-'){
        if(b=='*'||b=='/')
            return -1;
        else
            return 1;
    }
    else if(a=='#')
        return -1;
}
void main()
{
    char s[1001],c;
    int i,len,m;
    double a,b,r;
    while(gets(s)&&strcmp(s,"0")){
        vector<int> num;
        vector<char> chars;
        stack<char,vector<char> > opt;
        stack<char,vector<double> > intg;
        len=strlen(s);
        for(i=0;i<len;i++)
            if(s[i]=='+'||s[i]=='-'||s[i]=='/'||s[i]=='*')
                chars.push_back(s[i]);
            else if(s[i]<='9'&&s[i]>='0'){
                m=s[i]-'0';
                while(s[i+1]<='9'&&s[i+1]>='0'){
                    i++;
                    m=10*m+s[i]-'0';
                }
                num.push_back(m);
            }
        chars.push_back('#');
        opt.push('#');
        int flag=0,k=0;
        while(opt.top()!='#'||chars[0]!='#'){
            if(flag==0){
                intg.push(num[0]);
                num.erase(num.begin());
                flag=1;
            }
            else{
                if(cmp(opt.top(),chars[0])<0){
                    opt.push(chars[0]);
                    chars.erase(chars.begin());
                    flag=0;
                }
                else if(cmp(opt.top(),chars[0])>0){
                    c=opt.top();
                    opt.pop();
                    a=intg.top();
                    intg.pop();
                    b=intg.top();
                    intg.pop();
                    if(c=='+')
                        r=b+a;
                    else if(c=='-')
                        r=b-a;
                    else if(c=='*')
                        r=b*a;
                    else if(c=='/')
                        r=b/a;
                    intg.push(r);
                    flag=1;
                }
            }
        }
        printf("%.2lf\n",intg.top());
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -