Lazy Math Instructor

时间:2023-03-09 01:01:49
Lazy Math Instructor
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 3721   Accepted: 1290

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.

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:
  • 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)+c
a*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

YES
YES
NO
题目意思:输入两行公式,判断这两行公式相不相等,如果相等,输出YES,否则输出NO

解题思路:先将方式变成后缀式,后缀式通过栈实现。(不晓得后缀式是什么,就百度后缀式吧,我也是百度的(⊙﹏⊙)b)  
变成后缀式之后,再通过栈计算他们的值,这里需要将字母转为ASCII码的值计算。最后判断.......
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <stack>
using namespace std;
const int maxn = ;
int priority(char c)
{
if(c=='(')
return ;
else if(c=='*')
return ;
else
return ;
}
void convert(char *str,char *temp)
{
int len = strlen(str),t = ;
char c;
stack<char> st;
for(int i=;i<len;i++)
{
if(str[i]!=' ')
{
c = str[i];
if((c<='z'&&c>='a')||(c>=''&&c<=''))
temp[t++]=c;
else
{
if(st.empty()||c=='(')
st.push(c);
else if(c==')')
{
while(!st.empty()&&st.top()!='(')
{
//push_seq(pn[i],top_seq(p[i]));
temp[t++]=st.top();
st.pop();
}
st.pop();
}
else
{
while(!st.empty()&&priority(c)<=priority(st.top()))
{
temp[t++]=st.top();
st.pop();
}
st.push(c);
}
}
}
}
while(!st.empty())
{
temp[t++]=st.top();
st.pop();
}
temp[t]=;
}
int calculate(char *temp)
{
int len = strlen(temp),x,y,z;
char c;
stack<int> st;
for(int i=;i<len;i++)
{
c=temp[i];
if(c>=''&&c<='')
st.push(c-'');
else if(c<='z'&&c>='a')
st.push(int(c));
else
{
x=st.top();
st.pop();
y=st.top();
st.pop();
switch(c)
{
case '*': z = x*y; break;
case '+': z = x+y; break;
case '-': z = y-x; break;
}
st.push(z);
}
}
return st.top();
}
int main()
{
freopen("in.txt","r",stdin);
char str[maxn],temp[maxn];
int n;
scanf("%d",&n);
getchar();//此处不能忘记getchar(),否则会出错
while(n--)
{
gets(str);
convert(str,temp);
int ans1=calculate(temp);
gets(str);
convert(str,temp);
int ans2=calculate(temp);
if(ans1==ans2)
printf("YES\n");
else
printf("NO\n");
}
}