BUAA OJ 722 Gzh之表达式求值

来源:互联网 发布:网络教育和函授 编辑:程序博客网 时间:2024/05/16 19:50

题目描述

时间限制: 1000 ms 内存限制: 65536 kb
如题,给你个数学表达式,求值。

输入

多组输入数据,每组数据为一行字符串(长度小于100)。输入只包含整数(32位int范围内) , ‘+’ ,’-’ ,’‘, ‘/’ , ‘(’ , ‘)’ , ‘%’, ‘*‘。(两个乘号为乘方,具有最高优先级)

输入数据保证运算合法, 保证不会出现除乘方外两个运算符连续出现的情况(比如”++1”),不会出现小数。

输出

对于每组数据,输出一行,表达式的值。

输入样例

1+1

输出样例

2

AC代码

#include <cstdio>#include <string>#include <cctype>#include <cmath>#include <iostream>using namespace std;const int N_OPTR=9;typedef long long ll;enum thePri{lessThen=1,Equal,moreThen};//运算符优先等级 [栈顶] [当前]const int pri[N_OPTR][N_OPTR]={        /*           |-------------- 当 前 运 算 符 -----------| */        /*            +    -   *     /  (      )   %    **   \0 */        /* --  + */   3,   3,   1,   1,   1,   3,  1,   1,    3,        /* |   - */   3,   3,   1,   1,   1,   3,  1,   1,    3,        /* 栈  * */   3,   3,   3,   3,   1,   3,  3,   1,    3,        /* 顶  / */   3,   3,   3,   3,   1,   3,  3,   1,    3,        /* 符  ( */   1,   1,   1,   1,   1,   2,  1,   1,    4,        /* |   ) */   4,   4,   4,   4,   4,   4,  4,   4,    4,        /* |   % */   3,   3,   3,   3,   1,   3,  3,   1,    4,        /* |  ** */   3,   3,   3,   3,   1,   3,  3,   3,    4,        /* -- \0 */   1,   1,   1,   1,   1,   4,  1,   1,    2};struct stack{    ll data[110];    int size=0;    bool empty(){        return !size;    }    void pop(){        if(size)            size--;    }    ll top(){        return data[size-1];    }    void clear(){        size=0;    }    void push(ll num){        data[size++]=num;    }};string con;stack nums,ops;int pos;//                加    减  乘   除  左  右  取余 乘方 终止enum theOperater {ADD=0,SUB,MUL,DIV,LBR,RBR,DEL,POW,EOE};ll readNum(){    ll tem;    bool addSymbol;    if(isdigit(con[pos])) {        tem = con[pos] - '0';        nums.push(tem);        addSymbol=false;        pos++;    }    else{        addSymbol=true;        nums.push(con[++pos] - '0');        pos++;    }    while(isdigit(con[pos])){        tem=nums.top();nums.pop();        nums.push(tem*10+(con[pos]-'0'));        pos++;    }    if(addSymbol){        tem=nums.top();nums.pop();        tem*=(-1);        nums.push(tem);    }    pos--;    return tem;}theOperater readOp(){    switch(con[pos]){        case '+': return ADD;        case '-': return SUB;        case '*':            if(con[pos+1]=='*') {                ++pos;                return POW;            }            else                return MUL;        case '/': return DIV;        case '(': return LBR;        case ')': return RBR;        case '%': return DEL;        default: exit(-1);    }}ll calcu (ll a,theOperater op,ll b ) {    switch ( op ) {        case ADD : return a + b;        case SUB : return a - b;        case MUL : return a * b;        case DIV : return a/b;        case DEL : return a%b;        case POW : return (ll)pow(a,b);        default  : exit ( -1 );    }}int main(){    while(cin>>con){        //init        nums.clear();        ops.clear();        ops.push(EOE);        int size=(int)con.size();        for(pos=0;!ops.empty();pos++){            if(pos<size&&                    (isdigit(con[pos])                     ||(pos<size-1&&pos>0&&con[pos-1]=='('&&con[pos]=='-'&&isdigit(con[pos+1])))){                readNum();            } else {                if(pos<size){                    theOperater op=readOp();                    theOperater onTop=(theOperater)ops.top();                    switch(pri[onTop][op]){                        case lessThen :                            ops.push(op);                            break;                        case Equal :                            ops.pop();                            break;                        case moreThen:{                            --pos;ops.pop();                            ll num2=nums.top();nums.pop();                            ll num1=nums.top();nums.pop();                            nums.push(calcu(num1,onTop,num2));                        }                            break;                        default : exit ( -1 );                    }                }else{                    theOperater onTop=(theOperater)ops.top();ops.pop();                    if(onTop!=EOE){                        ll num2=nums.top();nums.pop();                        ll num1=nums.top();nums.pop();                        nums.push(calcu(num1, onTop, num2));                    }                }            }        }        cout<<nums.top()<<'\n';    }}
原创粉丝点击