BUAA OJ 722 Gzh之表达式求值
2017-07-26 21:37
218 查看
BUAA OJ 722 Gzh之表达式求值
题目描述
时间限制: 1000 ms 内存限制: 65536 kb如题,给你个数学表达式,求值。
输入
多组输入数据,每组数据为一行字符串(长度小于100)。输入只包含整数(32位int范围内) , ‘+’ ,’-’ ,’‘, ‘/’ , ‘(’ , ‘)’ , ‘%’, ‘*‘。(两个乘号为乘方,具有最高优先级)输入数据保证运算合法, 保证不会出现除乘方外两个运算符连续出现的情况(比如”++1”),不会出现小数。
输出
对于每组数据,输出一行,表达式的值。输入样例
1+1
输出样例
2
解题思路
表达式求值代码
#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'; } }
相关文章推荐
- 栈的应用-四则运算表达式求值
- nyoj 1272 表达式求值
- 使用传统算法进行表达式求值(数字支持科学表示法,操作符支持加减乘除乘方, 支持括号, 不支持函数)
- 数据结构上机题目4--后缀表达式求值
- 【足迹C++primer】表达式求值
- 【算法】表达式求值--逆波兰算法介绍
- 学习笔记------数据结构(C语言版)栈应用 表达式求值
- C++Primer 练习9.52(续) 中缀表达式转后缀表达式求值
- zzuli 1530 表达式求值
- 表达式求值
- 表达式求值 数据结构 C/C++ 栈的应用
- 算法-表达式求值(Dijkstra双栈)
- (学习日记)裘宗燕:C/C++ 语言中的表达式求值
- (精)河南省第4届acm(表达式求值)
- 【蓝桥杯-基础训练】*+表达式求值
- 最简单的表达式求值
- 栈的应用--四则运算表达式求值(java语言)
- 第九届省赛题 表达式求值
- 表达式求值(C/C++版)
- 四则运算表达式解析和求值(支持括号、小数)