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码的值计算。最后判断.......
  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <fstream>
  5 #include <stack>
  6 using namespace std;
  7 const int maxn = 90;
  8 int priority(char c)
  9 {
 10     if(c=='(')
 11         return 0;
 12     else if(c=='*')
 13         return 2;
 14     else
 15         return 1;
 16 }
 17 void convert(char *str,char *temp)
 18 {
 19     int len = strlen(str),t = 0;
 20     char c;
 21     stack<char> st;
 22     for(int i=0;i<len;i++)
 23     {
 24         if(str[i]!=' ')
 25         {
 26             c = str[i];
 27             if((c<='z'&&c>='a')||(c>='0'&&c<='9'))
 28                 temp[t++]=c;
 29             else
 30             {
 31                 if(st.empty()||c=='(')
 32                     st.push(c);
 33                 else if(c==')')
 34                 {
 35                     while(!st.empty()&&st.top()!='(')
 36                     {
 37                         //push_seq(pn[i],top_seq(p[i]));
 38                         temp[t++]=st.top();
 39                         st.pop();
 40                     }
 41                     st.pop();
 42                 }
 43                 else
 44                 {
 45                     while(!st.empty()&&priority(c)<=priority(st.top()))
 46                     {
 47                         temp[t++]=st.top();
 48                         st.pop();
 49                     }
 50                     st.push(c);
 51                 }
 52             }
 53         }
 54     }
 55     while(!st.empty())
 56     {
 57         temp[t++]=st.top();
 58         st.pop();
 59     }
 60     temp[t]=0;
 61 }
 62 int calculate(char *temp)
 63 {
 64     int len = strlen(temp),x,y,z;
 65     char c;
 66     stack<int> st;
 67     for(int i=0;i<len;i++)
 68     {
 69         c=temp[i];
 70         if(c>='0'&&c<='9')
 71             st.push(c-'0');
 72         else if(c<='z'&&c>='a')
 73             st.push(int(c));
 74         else
 75         {
 76             x=st.top();
 77             st.pop();
 78             y=st.top();
 79             st.pop();
 80             switch(c)
 81             {
 82                 case '*':  z = x*y; break;
 83                 case '+':  z = x+y; break;
 84                 case '-':  z = y-x; break;
 85             }
 86             st.push(z);
 87         }
 88     }
 89     return st.top();
 90 }
 91 int main()
 92 {
 93     freopen("in.txt","r",stdin);
 94     char str[maxn],temp[maxn];
 95     int n;
 96     scanf("%d",&n);
 97     getchar();//此处不能忘记getchar(),否则会出错
 98     while(n--)
 99     {
100         gets(str);
101         convert(str,temp);
102         int ans1=calculate(temp);
103         gets(str);
104         convert(str,temp);
105         int ans2=calculate(temp);
106         if(ans1==ans2)
107             printf("YES\n");
108         else
109             printf("NO\n");
110     }
111 }
View Code

 



转载于:https://www.cnblogs.com/demodemo/p/4678400.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值