首页 > 其他 > 详细

九度OJ-1101-计算表达式

时间:2015-03-26 09:08:50      阅读:276      评论:0      收藏:0      [点我收藏+]

这个题目一行输入可能有多个表达式

#include <stack>
#include <stdio.h>
using namespace std;
char str[220]; //保存表达式字符串
/*
优先级矩阵,记人为添加在表达式首位的标记运算符为0号,
+为1号,-为2号,*为3号,/为4号,若mat[i][j]==1
则表示i号运算符优先级大于j号。
*/
int mat[][5]={
    1,0,0,0,0,
    1,0,0,0,0,
    1,0,0,0,0,
    1,1,1,0,0,
    1,1,1,0,0,
};

stack<int> op;  //运算符栈
stack<int> in;//数字栈
/*
获得表达式中下一个元素的函数,若函数结束运行时,引用变量reto为true,则表示该元素
为一个运算符,其编号保存在饮用变量retn中;否则,表示该元素为一个数字,其值保存在
引用变量retn中,引用变量i表示遍历到的字符串的下标。
*/
void getOp(bool &reto,int &retn,int &i){
    if (i==0&&op.empty()==true)//初始时将0号运算符压栈
    {
        reto=true;
        retn=0;
        return;
    }
    if (str[i]==0)//字符串扫描结束时,将0号运算符压栈
    {
        reto=true;
        retn=0;
        return;
    }

    if (str[i]>=‘0‘&&str[i]<=‘9‘)//当为数字时,将reto置为false
    {
        reto = false;
    }
    else{           //其他情况均为运算符
        reto=true;
        if (str[i]==‘+‘)
        {
            retn=1;
        }else if (str[i]==‘-‘)
        {
            retn=2;
        }else if (str[i]==‘*‘)
        {
            retn=3;
        }else if (str[i]==‘/‘)
        {
            retn=4;
        }
        i++;
        return;
    }
    retn=0;
    for (; str[i] !=‘+‘&&str[i] !=‘-‘&&str[i] !=‘*‘&&str[i] !=‘/‘&&str[i]!=0; ++i)
    {
        retn*=10;
        retn+=str[i]-‘0‘;
    }

    return ;
}
int main(){
    //char str[200];
    freopen("input.txt","r",stdin);
    while(scanf("%s",str)!=EOF){
        //puts(str);
        bool retop;
        int retnum;
        int idx=0;
        while(!op.empty()) op.pop();
        while(!in.empty()) in.pop();
        while(true){
            getOp(retop,retnum,idx);
            //printf("idx:%d,retop:%d,retnum:%d\n",idx,retop,retnum);
            if (retop==false) //操作数进栈
            {
                in.push(retnum);
            }else{
                double tmp;
                if (op.empty()==true||mat[retnum][op.top()]==1)//运算符优先级高的要进栈
                {
                    op.push(retnum);
                }else{
                    while(mat[retnum][op.top()]==0){
                        int ret=op.top();
                        op.pop();
                        int  b=in.top();
                        in.pop();
                        int  a = in.top();
                        in.pop();
                        if (ret==1)
                        {
                            tmp=a+b;
                        }
                        else if(ret ==2) tmp=a-b;
                        else if(ret ==3) tmp=a*b;
                        else tmp=a/b;
                        in.push(tmp);
                    }
                    op.push(retnum);
                }
            }
            if (op.size()==2&&op.top()==0)
            {
                break;
            }

        }
        printf("%d\n",in.top());

    }
    return 0;
}

九度OJ-1101-计算表达式

原文:http://blog.csdn.net/li8630/article/details/44632773

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!