lazy instructor

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码的值计算。最后判断.......



  1 #include <map>
  2 #include <stack>
  3 #include <cctype>
  4 #include <iostream>
  5 using namespace std;
  6 map<char,int> m;
  7 string a1,a2,v1,v2;
  8 string bianhouzui(string a)   //这里是将公式变成后缀式
  9 {
 10     int i,j=0,len;
 11     char c[81];
 12     stack<char> z1;
 13     len=a.size();
 14     for(i=0; i<len; i++)
 15     {
 16         if(isalnum(a[i]))   //isalnum是判断是不是数字或者字母,如果是返回真
 17             c[j++]=a[i];
 18         else
 19         {
 20             switch (a[i])
 21             {
 22             case '(':
 23                 z1.push(a[i]);
 24                 break;
 25             case ')':
 26                 while(z1.top()!='(')
 27                 {
 28                     c[j++]=z1.top();
 29                     z1.pop();
 30                 }
 31                 z1.pop();
 32                 break;
 33             case '+':
 34             case '-':
 35             case '*':
 36                 while(!z1.empty()&&m[a[i]]<=m[z1.top()])
 37                 {
 38                     c[j++]=z1.top();
 39                     z1.pop();
 40                 }
 41                 z1.push(a[i]);
 42             }
 43         }
 44     }
 45     while(!z1.empty())
 46     {
 47         c[j++]=z1.top();
 48         z1.pop();
 49     }
 50     c[j]='\0';
 51     string x=c;
 52     return x;
 53 }
 54 
 55 
 56 int jisuan(string v)   //这里是计算
 57 {
 58     int a,b,i,len;
 59     len=v.size();
 60     stack<int> z2;
 61     for(i=0; i<len; i++)
 62     {
 63         if(isalnum(v[i]))
 64         {
 65             if(isdigit(v[i]))  //  判断是不是数字
 66                 z2.push(v[i]-'0');   //转成ASCII码
 67             else
 68                 z2.push(v[i]);
 69         }
 70         else
 71         {
 72             a=z2.top();
 73             z2.pop();
 74             b=z2.top();
 75             z2.pop();
 76             switch (v[i])
 77             {
 78             case '+':
 79                 z2.push(b+a);
 80                 break;
 81             case '-':
 82                 z2.push(b-a);
 83                 break;
 84             case '*':
 85                 z2.push(b*a);
 86             }
 87         }
 88     }
 89     return z2.top();
 90 }
 91 
 92 
 93 int main()
 94 {
 95     int T;
 96     m['(']=0;
 97     m['+']=m['-']=1;
 98     m['*']=2;
 99     cin>>T;
100     cin.get();     //没有这个就不行,我也不知道为什么,貌似好像是什么回车问题.....
101     while(T--)
102     {
103         getline(cin,a1);
104         getline(cin,a2);
105         v1= bianhouzui(a1);
106         v2=  bianhouzui(a2);
107         if(jisuan(v1)==jisuan(v2))
108             cout<<"YES"<<endl;
109         else
110             cout<<"NO"<<endl;
111     }
112     return 0;
113 }

 




你可能感兴趣的:(struct)