Lazy Math Instructor
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 2087 | Accepted: 712 |
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)+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
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<stack>
#include<cctype>
#include<cstdlib>
using namespace std;
const int N=1000;
char _stack[N];//运算符的栈
int top;//初始化为-1
void _pre(string &str)
{
string res;
for(int i=0,len=str.size();i<len;i++)
{
if(isalpha(str[i]))
{
int n=int(str[i]);
int a[50],pl=0;
while(n) a[pl++]=n%10,n/=10;
for(int j=pl-1;j>=0;j--) res+=a[j]+'0';
}
else res+=str[i];
}
str=res;
}
int _pow(int x,int y)
{
if(y==0) return 1;
if(y==1) return x;
int cnt;
cnt=_pow(x,y/2);
cnt=cnt*cnt;
if(y&1) cnt*=x;
return cnt;
}
void _change(string &str,string &res)//第一个参数为中缀表达式,第二个为后缀表达式,初始为空
{
top=-1;
int len=str.size();
for(int i=0;i<len;i++)
{
switch(str[i])
{
case '(':_stack[++top]='(';break;
case '+':
case '-':while(top>=0&&_stack[top]!='(')
res+=_stack[top--];
_stack[++top]=' ';//空格很重要 分隔数字的
_stack[++top]=str[i];
break;
case '*':
case '/':while(top>=0&&_stack[top]!='('&&_stack[top]!='+'&&_stack[top]!='-')
res+=_stack[top--];
_stack[++top]=' ';
_stack[++top]=str[i];
break;
case '^':while(top>=0&&_stack[top]!='('&&_stack[top]!='+'&&_stack[top]!='-'&&_stack[top]!='*'&&_stack[top]!='/')
res+=_stack[top--];
_stack[++top]=' ';
_stack[++top]=str[i];
break;
case ')':while(_stack[top]!='(')
res+=_stack[top--];
top--;
break;
default:res+=str[i];
if(i==len-1) res+=' ';
else if(!isdigit(str[i+1])) res+=' ';
}
}
while(top>=0)
{
res+=_stack[top--];
}
}
int _value(string str)//参数为后缀表达式
{
int cnt,len=str.size();
top=-1;
int _digit[N];
for(int i=0;i<len;i++)
{
switch(str[i])
{
case ' ':break;//空格很重要 分隔数字的
case '+':cnt=_digit[top-1]+_digit[top];
_digit[--top]=cnt;
break;
case '-':cnt=_digit[top-1]-_digit[top];
_digit[--top]=cnt;
break;
case '*':cnt=_digit[top-1]*_digit[top];
_digit[--top]=cnt;
break;
case '/':cnt=_digit[top-1]/_digit[top];//此题为整除,因题而异
_digit[--top]=cnt;
break;
case '^':cnt=_pow(_digit[top-1],_digit[top]);
_digit[--top]=cnt;
break;
default:string temp;
while(i<len&&isdigit(str[i])) temp+=str[i],i++;i--;
_digit[++top]=atoi(temp.c_str());
}
}
return _digit[0];
}
int main()
{
int ci;scanf("%d",&ci);getchar();
while(ci--)
{
char h[111],k[111];
gets(h);gets(k);//需用gets
string a,b,res;
a=h,b=k;//再赋值
_pre(a);
_pre(b);//将字母转换成对应的ASCII码
// cout<<a<<"..."<<b<<endl;
_change(a,res);
int sa=_value(res);
res.clear();
_change(b,res);
int sb=_value(res);
if(sa==sb) printf("YES/n");
else printf("NO/n");
}
return 0;
}