表达式求值 【堆栈】

来源:互联网 发布:股票行情交易软件 编辑:程序博客网 时间:2024/04/28 07:33

表达式求值

时间限制:3000 ms  |  内存限制:65535 KB
难度:4
描述
ACM队的mdd想做一个计算器,但是,他要做的不仅仅是一计算一个A+B的计算器,他想实现随便输入一个表达式都能求出它的值的计算器,现在请你帮助他来实现这个计算器吧。
比如输入:“1+2/4=”,程序就输出1.50(结果保留两位小数)
输入
第一行输入一个整数n,共有n组测试数据(n<10)。
每组测试数据只有一行,是一个长度不超过1000的字符串,表示这个运算式,每个运算式都是以“=”结束。这个表达式里只包含+-*/与小括号这几种符号。其中小括号可以嵌套使用。数据保证输入的操作数中不会出现负数。
数据保证除数不会为0
输出
每组都输出该组运算式的运算结果,输出结果保留两位小数。
样例输入
21.000+2/4=((1+2)*5+1)/4=
样例输出
1.504.00

AC代码:

 # include <stdio.h># include <math.h># include <string.h># include <stack>using namespace std;char s[1005];stack<double> dtack;stack<char> ctack;int main(void){int n;scanf("%d", &n);while (n--){scanf("%s", &s[1]);int len = strlen(&s[1]);s[0] = '(';s[len] = ')';int i;for (i = 0; i <= len; i++){if (s[i] == '('){ctack.push(s[i]);}else if (s[i] >= '0' && s[i] <= '9'){double v = 0.0;int spot = 0;while (s[i] >= '0' && s[i] <= '9' || s[i] == '.'){if (s[i] == '.'){spot = i;}else {v = v * 10 + (s[i] - '0');}i++;}i--;if (spot == 0){dtack.push(v);}else{dtack.push(v / pow(10, (i - spot)));}}else if (s[i] == '+' || s[i] == '-'){while (ctack.top() != '('){double a = dtack.top(); dtack.pop();double b = dtack.top(); dtack.pop();double c = 0.0;switch(ctack.top()){case '+' :c = b + a;break;case '-' :c = b - a;break;case '*' :c = b * a;break;case '/' :c = b / a;break;}dtack.push(c);ctack.pop();}ctack.push(s[i]);}else if (s[i] == '*' || s[i] == '/'){if (ctack.top() == '*'){double a = dtack.top(); dtack.pop();double b = dtack.top(); dtack.pop();dtack.push(b * a);ctack.pop();}else if (ctack.top() == '/'){double a = dtack.top(); dtack.pop();double b = dtack.top(); dtack.pop();dtack.push(b / a);ctack.pop();}ctack.push(s[i]);}else if (s[i] == ')'){while (ctack.top() != '('){double a = dtack.top(); dtack.pop();double b = dtack.top(); dtack.pop();double c = 0.0;switch (ctack.top()){case '+' :c = b + a;break;case '-' :c = b - a;break;case '*' :c = b * a;break;case '/' :c = b / a;break;}dtack.push(c);ctack.pop();}ctack.pop();}}printf("%.2lf\n", dtack.top());dtack.pop();}return 0;}        


表达式求值

时间限制:3000 ms  |  内存限制:65535 KB
难度:3
描述

Dr.Kong设计的机器人卡多掌握了加减法运算以后,最近又学会了一些简单的函数求值,比如,它知道函数min(20,23)的值是20 ,add(10,98) 的值是108等等。经过训练,Dr.Kong设计的机器人卡多甚至会计算一种嵌套的更复杂的表达式。

假设表达式可以简单定义为:

1. 一个正的十进制数 x 是一个表达式。

2. 如果 和  表达式,则 函数min(x,y )也是表达式,其值为x,y 中的最小数。

3. 如果 和  表达式,则 函数max(x,y )也是表达式,其值为x,y 中的最大数。

4.如果 和  表达式,则 函数add(x,y )也是表达式,其值为x,y 之和。

例如, 表达式 max(add(1,2),7) 的值为 7。

请你编写程序,对于给定的一组表达式,帮助 Dr.Kong 算出正确答案,以便校对卡多计算的正误

输入
第一行: N 表示要计算的表达式个数 (1≤ N ≤ 10) 
接下来有N行, 每行是一个字符串,表示待求值的表达式
(表达式中不会有多余的空格,每行不超过300个字符,表达式中出现的十进制数都不
超过1000。)
输出
输出有N行,每一行对应一个表达式的值。
样例输入
3add(1,2) max(1,999) add(min(1,1000),add(100,99)) 
样例输出
3999200

AC代码:

 # include <stdio.h># include <string.h># include <stack>int min(int a, int b);int max(int a, int b);int add(int a, int b);using namespace std;char s[310];stack<int> idsta;stack<char> csta;int main(void){int n;scanf("%d", &n);while (n--){scanf("%s", &s);int len = strlen(s);for (int i = 0; i <= len; i++){if (s[i] == '('){csta.push(s[i]);}else if (s[i] >= 'a' && s[i] <= 'z'){csta.push(s[i]);}else if (s[i] >= '0' && s[i] <= '9'){int v = 0;while (s[i] >= '0' && s[i] <= '9'){v = v * 10 + (s[i] - '0');i++;}//printf("v = %d\n", v);idsta.push(v);if (s[i] == ','){i++;v = 0;while (s[i] >= '0' && s[i] <= '9'){v = v * 10 + (s[i] - '0');i++;}//printf("v = %d\n", v);idsta.push(v);}i--;}else if (s[i] == ')'){if (csta.top() == '('){int a = idsta.top(); idsta.pop();int b = idsta.top(); idsta.pop();int c;csta.pop();//printf("csta.top = %c\n", csta.top());int count = 2;char ch[4];ch[count] = csta.top(); csta.pop();//printf("csta.top = %c\n", csta.top());count --;ch[count] = csta.top(); csta.pop();//printf("csta.top = %c\n", csta.top());count --;ch[count] = csta.top(); csta.pop();//printf("csta.top = %c\n", csta.top());ch[3] = '\0';//puts(ch);if (strcmp(ch, "min") == 0){c = min(b, a);}else if (strcmp(ch, "max") == 0){c = max(b, a);}else if (strcmp(ch, "add") == 0){c = add(b, a);}//printf("c = %d\n", c);idsta.push(c);//printf("csta.top = %c\n", csta.top());}}}printf("%d\n", idsta.top());idsta.pop();}return 0;}int min(int a, int b){if (a > b)return b;else return a;}int max(int a, int b){if (a > b)return a;else return b;}int add(int a, int b){return a + b;}        


表达式求值

时间限制:1000 ms  |  内存限制:65535 KB
难度:3
描述
假设表达式定义为: 1. 一个十进制的正整数 X 是一个表达式。 2. 如果 X 和 Y 是 表达式,则 X+Y, X*Y 也是表达式; *优先级高于+. 3. 如果 X 和 Y 是 表达式,则 函数 Smax(X,Y)也是表达式,其值为:先分别求出 X ,Y 值的各位数字之和,再从中选最大数。 4.如果 X 是 表达式,则 (X)也是表达式。 例如: 表达式 12*(2+3)+Smax(333,220+280) 的值为 69。 请你编程,对给定的表达式,输出其值。  
输入
【标准输入】 第一行: T 表示要计算的表达式个数 (1≤ T ≤ 10) 接下来有 T 行, 每行是一个字符串,表示待求的表达式,长度<=1000
输出
【标准输出】 对于每个表达式,输出一行,表示对应表达式的值。
样例输入
312+2*312*(2+3)12*(2+3)+Smax(333,220+280)
样例输出
186069

AC代码:

# include <stdio.h># include <string.h># include <stack># include <algorithm>using namespace std;stack <int> result;stack <char> ost;char str[1005];int main(void){int t;scanf("%d", &t);while (t--){int i;scanf("%s", &str[1]);str[0] = '(';int len = strlen(&str[1]);str[len + 1] = ')';len ++;for (i = 0; i <= len; i++){if (str[i] >= '0' && str[i] <= '9'){int t = 0;while (str[i] >= '0' && str[i] <= '9'){t = t * 10 + (str[i] - '0');i++;}i--;result.push(t);}else if (str[i] == '('){ost.push(str[i]);}else if (str[i] == 'S'){ost.push('(');ost.push('S');i += 4;}else if (str[i] == '+'){while (ost.top() != '(' && ost.top() != 'S'){int a = result.top();result.pop();int b = result.top();result.pop();int c;switch(ost.top()){case '+' : c = b + a;break;case '*' : c = b * a;break;}result.push(c);ost.pop();}ost.push(str[i]);}else if (str[i] == '*'){if (ost.top() == '*'){int a = result.top();result.pop();int b = result.top();result.pop();int c = b * a;result.push(c);ost.pop();}ost.push(str[i]);}else if (str[i] == ')'){while (ost.top() != '(' && ost.top() != 'S'){int a = result.top();result.pop();int b = result.top();result.pop();int c;switch(ost.top()){case '+' : c = b + a;break;case '*' : c = b * a;break;}result.push(c);ost.pop();}if (ost.top() == 'S'){int c;int a = result.top();result.pop();int b = result.top();result.pop();int ta = 0, tb = 0;while (b){tb += b % 10;b /= 10;}while (a){ta += a % 10;a /= 10;}c = ta > tb ? ta : tb;result.push(c);ost.pop();}ost.pop();}}printf("%d\n", result.top());result.pop();}return 0;}




0 0
原创粉丝点击