栈的运用! 表达式求值。
2015-05-20 20:15
295 查看
初级版本
http://acm.hdu.edu.cn/showproblem.php?pid=1237
拿杭电的这个初级版本练练手!
下面是南阳理工的一个题目属于加强版,第一个写出来后第二个写起来就轻松多了
http://acm.hdu.edu.cn/showproblem.php?pid=1237
拿杭电的这个初级版本练练手!
#include<iostream> #include<algorithm> #include<stdio.h> #include<math.h> #include<string.h> #include<queue> #include<stack> using namespace std; #define maxn 17 stack <char> opS;//符号栈 stack <double> numS;//数字栈 char str[300]; int pos[5] = {'+','-', '*', '/', '#' }; int cmps[5][5] = { {1,1,2,2,1}, {1,1,2,2,1}, {1,1,1,1,1}, {1,1,1,1,1}, {2,2,2,2,3} }; char* getNum(char *p,double &num) { num = 0; while(*p != ' ' && *p) { num = num*10 + *p - '0'; p ++; } if(*p == ' ') p ++; return p; } int getOp(char ch) { for(int i=0; i<5; i++) { if(ch == pos[i]) return i; } } int cmp(char op2,char op) { int a, b; a = getOp(op2); b = getOp(op); return cmps[a][b]; } double cal(double a, double b,char op) { switch(op) { case '+':return a+b; case '-':return b-a; case '*':return a*b; case '/':return b/a; } } double solve() { double num1, num2; char op, *p = str, op2; opS.push('#'); while(*p) { p = getNum(p,num1); if(*p) { op = *p; p += 2; } else op = '#'; numS.push(num1); while(1) { op2 = opS.top(); if(cmp(op2,op) == 2)///如果栈内运算符的优先级 比栈外的优先级要低 操作符都入栈 { opS.push(op); break; } else if(cmp(op2, op) == 3) { return numS.top(); } else { num1 = numS.top(); numS.pop(); num2 = numS.top(); numS.pop(); opS.pop(); num1 = cal(num1, num2, op2); numS.push(num1); } } } } int main() { while(gets(str), strcmp(str, "0") ) { while(opS.size() ) opS.pop(); while(numS.size() ) numS.pop(); double ans = solve(); printf("%.2lf\n", ans); } return 0; }
下面是南阳理工的一个题目属于加强版,第一个写出来后第二个写起来就轻松多了
#include<iostream> #include<algorithm> #include<stdio.h> #include<math.h> #include<string.h> #include<queue> #include<stack> using namespace std; #define maxn 17 char pos[7] = { '+', '-', '*', '/', '(', ')', '=' }; ///运算符的优先级比较 cmp[栈中符号][栈外符号比较] 1 >, 2<, 3 =, 4 不可比 int cmp[7][7] = { {1,1,2,2,2,1,1}, {1,1,2,2,2,1,1}, {1,1,1,1,2,1,1}, {1,1,1,1,2,1,1}, {2,2,2,2,2,3,4}, {4,4,4,4,4,4,4}, {2,2,2,2,2,4,3} }; double getNum(char* &p) { double num; sscanf(p, "%lf", &num); while( (*p >='0' && *p <= '9') || *p == '.' ) p ++; return num; } char getOp(char* &p) { char ch; ch = *p; p ++; return ch; } double cal(double a, double b,char op) { switch(op) { case '+': return a + b; case '-': return a - b; case '*': return a * b; case '/': return a / b; } } int cmps(char op1,char op2) { int a, b; for(int i=0; i<7; i++) { if(pos[i] == op1) a = i; if(pos[i] == op2) b = i; } return cmp[a][b]; } double solve(char *p) { stack <char> opS;///符号栈 stack <double> numS;///数字栈 double num1, num2; char op1, op2; opS.push('='); while(1) { if(*p >= '0' && *p <= '9') numS.push( getNum(p) ); op2 = getOp(p); while(1) { op1 = opS.top(); if( cmps(op1,op2) == 2)///栈中元素的优先级小,将栈外符号入栈 { opS.push(op2); break; } else if( cmps(op1,op2) == 1)///栈中元素的优先级大,将栈中符号拿出来运算 { opS.pop(); num1 = numS.top(); numS.pop(); num2 = numS.top(); numS.pop(); numS.push( cal(num2, num1, op1) ); } else if( cmps(op1, op2) == 3 ) { opS.pop(); if(op1 == '(' ) break; if(op1 == '=') return numS.top(); } } } } int main() { int T; char str[1350]; scanf("%d ", &T); while(T--) { scanf("%s", str); double ans = solve(str); printf("%.2lf\n", ans); } return 0; }
相关文章推荐
- 运用栈对算数表达式求值
- 栈的运用(算数表达式求值的算符优先算法 十以内简单版)
- POJ 2106-Boolean Expressions,双栈运用类似表达式求值!
- 栈的运用(中缀表达式转换为后缀表达式,并对后缀表达式求值。)
- 表达式求值(C实现,实现多括号,浮点数)---栈的实现以及运用。
- 表达式求值(栈的运用)
- 表达式求值之栈的灵活运用
- 栈的运用-四则运算表达式求值
- NYOJ305_表达式求值
- SDIBT2666——逆波兰表达式求值
- Java 字符串算术表达式求值
- 表达式求值
- iOS系统自带正则表达式简单运用
- 简单算术表达式求值
- 第六周--数据结构之自建算法库之表达式求值(用栈结构)
- 栈的应用 后缀表达式求值 后缀表达式与中缀表达式的转换
- C中运用正则表达式
- poj 2106 Boolean Expressions(特殊的表达式求值)
- 接受用户输入的表达式,利用数据结构中栈完成多项式(+ - * / ( ))求值
- 洛谷p1981 表达式求值