POJ 1686 Lazy Math Instructor (中缀表达式计算)
来源:互联网 发布:dsa数据升级工具 编辑:程序博客网 时间:2024/05/16 15:24
Lazy Math Instructor
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 3819 Accepted: 1320
Description
A math instructor is too lazy to grade a question in the exam papers in which students are supposed to produce a complicated formula for the question asked. Students may write correct answers in different forms which makes grading very hard. So, the instructor needs help from computer programmers and you can help.
You are to write a program to read different formulas and determine whether or not they are arithmetically equivalent.
You are to write a program to read different formulas and determine whether or not they are arithmetically equivalent.
Input
The first line of the input contains an integer N (1 <= N <= 20) that is the number of test cases. Following the first line, there are two lines for each test case. A test case consists of two arithmetic expressions, each on a separate line with at most 80 characters. There is no blank line in the input. An expression contains one or more of the following:
Note: Expressions are syntactically correct and evaluated from left to right with equal precedence (priority) for all operators. The coefficients and exponents of the variables are guaranteed to fit in 16-bit integers.
- Single letter variables (case insensitive).
- Single digit numbers.
- Matched left and right parentheses.
- Binary operators +, - and * which are used for addition, subtraction and multiplication respectively.
- Arbitrary number of blank or tab characters between above tokens.
Note: Expressions are syntactically correct and evaluated from left to right with equal precedence (priority) for all operators. The coefficients and exponents of the variables are guaranteed to fit in 16-bit integers.
Output
Your program must produce one line for each test case. If input expressions for each test data are arithmetically equivalent, "YES", otherwise "NO" must be printed as the output of the program. Output should be all in upper-case characters.
Sample Input
3(a+b-c)*2(a+a)+(b*2)-(3*c)+ca*2-(a+c)+((a+c+e)*2)3*a+c+(2*e)(a-b)*(a-b)(a*a)-(2*a*b)-(b*b)
Sample Output
YESYESNO
Source
大体题意:
每一组数据给你两个表达式,问两个表达式结果是否相同!
思路:
直接用红书的中缀表达式模板就可过了!
虽然是公式最后是否相等,可以随便数据看是否相同!,可以将每个字母弄成指定的ASCII码值!
直接计算即可!
注意:
表达式会有空格 但模板是支持空格的,所以直接gets即可!
保险点可以加个精度eps!
#include<cstdio>#include<cstring>#include<algorithm>#include<stack>#include<vector>#include<string>#include<cstdlib>#include<cctype>#include<cmath>#include<iostream>using namespace std;const double eps = 1e-8;int priv[300];double value[300];double calc(double a,double b,char op){ if (op == '+')return a + b; if (op == '-')return a - b; if (op == '*')return a * b; if (op == '/')return a / b;}double calculate(string str,double val[] = value){ stack<double >num; stack<char > oper; priv['+'] = priv['-'] = 3; priv['*'] = priv['/'] = 2; priv['('] = 10; double x,y,t = 0; int i;char last = 0; for (i = 0; i < str.length(); i++){ if (isalpha(str[i])){ num.push(val[str[i]]); }else if (isdigit(str[i])){ num.push(atof(str.c_str()+i)); for (;i+1 < str.length()&&isdigit(str[i+1]);i++); if (i+1<str.length()&&str[i+1] == '.') for (i++;i+1<str.length()&&isdigit(str[i+1]);i++); }else if (str[i] == '('){ oper.push(str[i]); }else if (str[i] == ')'){ while(oper.top()!='('){ y = num.top();num.pop(); x = num.top();num.pop(); char op=oper.top(); oper.pop(); num.push(calc(x,y,op)); } oper.pop(); }else if (str[i] == '-' && (last == 0||last == '(')){ num.push(0.0); oper.push('-'); }else if (priv[str[i]]>0){ while(oper.size()>0&&priv[str[i]]>=priv[oper.top()]){ y = num.top();num.pop(); x = num.top();num.pop(); char op = oper.top(); oper.pop(); num.push(calc(x,y,op)); } oper.push(str[i]); }else continue; last = str[i]; } while(oper.size()>0){ y=num.top();num.pop(); x=num.top();num.pop(); char op = oper.top(); oper.pop(); num.push(calc(x,y,op)); } return num.top();}int main(){ for (int i = 0; i < 300; ++i)value[i] = i; int n; char u[300],v[300]; scanf("%d",&n); getchar(); while(n--){ gets(u); gets(v); double ans1 = calculate(u); double ans2 = calculate(v); if (fabs(ans1-ans2) < eps)printf("YES\n"); else printf("NO\n"); } return 0;}
0 0
- POJ 1686 Lazy Math Instructor (中缀表达式计算)
- zoj 1418 || poj 1686 Lazy Math Instructor(等价表达式)
- poj 1686 Lazy Math Instructor(表达式求值)
- poj 1686 Lazy Math Instructor (字符串)
- POJ 1686 Lazy Math Instructor
- POJ 1686 Lazy Math Instructor
- POJ 1686 Lazy Math Instructor(数据结构-表达式树)
- zoj 1418||poj 1686 Lazy Math Instructor
- POJ 1686 Lazy Math Instructor 笔记
- poj-1686-Lazy Math Instructor(stack)
- HDU 1686 Lazy Math Instructor(中缀转后缀,栈模拟)
- POJ 1686 Lazy Math Instructor 栈的应用
- E - Lazy Math Instructor
- Lazy Math Instructor
- POJ 1686 Lazy Math Instructor (模似题+栈的运用) 各种坑
- Lazy Math Instructor - POJ 1686 栈+提比的强行AC技能
- ZOJ 1418 Lazy Math Instructor
- 中缀表达式转后缀表达式 (中缀表达式的计算)
- java 中this调用静态变量及实例变量的若干错误
- 继电器的使用
- hdu 2444
- win8下使用vs2003查找时需如下设置
- 慕课网学习笔记02
- POJ 1686 Lazy Math Instructor (中缀表达式计算)
- @date2016-05-05 22:27(软件工程男的日常)
- javaweb邮件的在线发送
- Udacity:python基础
- volatile 关键词的使用
- 关于mysql出错后卸载重装的问题
- Java FileReader读文件
- iOS---archive(归档)的总结
- ZZULIOJ 1832 贪吃的松鼠 (位运算)