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");
}
}
												

Lazy Math Instructor的更多相关文章

  1. 数据结构——POJ 1686 Lazy Math Instructor 栈的应用

    Description A math instructor is too lazy to grade a question in the exam papers in which students a ...

  2. POJ 1686 Lazy Math Instructor (模似题+栈的运用) 各种坑

    Problem Description A math instructor is too lazy to grade a question in the exam papers in which st ...

  3. poj 1684 Lazy Math Instructor(字符串)

    题目链接:http://poj.org/problem?id=1686 思路分析:该问题为表达式求值问题,对于字母使用浮点数替换即可,因为输入中的数字只能是单个digit. 代码如下: #includ ...

  4. UVALive 2056 Lazy Math Instructor(递归处理嵌套括号)

    因为这个题目说明了优先级的规定,所以可以从左到右直接运算,在处理嵌套括号的时候,可以使用递归的方法,给定每一个括号的左右边界,伪代码如下: int Cal(){ if(括号)  sum += Cal( ...

  5. POJ 1686 Lazy Math Instructor(栈)

    原题目网址:http://poj.org/problem?id=1686 题目中文翻译: Description 数学教师懒得在考卷中给一个问题评分,因为这个问题中,学生会为所问的问题提出一个复杂的公 ...

  6. Soj题目分类

    -----------------------------最优化问题------------------------------------- ----------------------常规动态规划 ...

  7. lazy instructor

    Description A math instructor is too lazy to grade a question in the exam papers in which students a ...

  8. Problem K 栈

    Description A math instructor is too lazy to grade a question in the exam papers in which students a ...

  9. PQJ 1686(栈栈栈)

    PQJ  1686(栈栈栈) 用栈解决问题 Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I6 ...

随机推荐

  1. POJ 2187 旋转卡壳 + 水平序 Graham 扫描算法 + 运算符重载

    水平序 Graham 扫描算法: 计算二维凸包的时候可以用到,Graham 扫描算法有水平序和极角序两种. 极角序算法能一次确定整个凸包, 但是计算极角需要用到三角函数,速度较慢,精度较差,特殊情况较 ...

  2. Qt如何读取ico文件中的image(使用QImageReader和QIcon)

    ico文件是一个容器,内部可以装载许多个image,我们可以通过QIcon的pixmap方法来获取需要的image QPixmap pixmap ( const QSize & size, M ...

  3. linux c coding style

    Linux kernel coding style This is a short document describing the preferred coding style for the lin ...

  4. 测试DOM0级事件和DOM2级事件的堆叠

    1. 问题 如果大家看过北风网CJ讲师的Javascript视频教程,就可以看到其封装了一个很强的事件添加和删除函数,如下所示 function addEvent(obj, evtype, fn) { ...

  5. Android 电话自己主动接听和挂断具体解释

    1.通过aidl及反射实现挂断电话 详细分三步: (1)ITelephony.aidl ,必须新建com.android.internal.telephony包并放入ITelephony.aidl文件 ...

  6. asp.net上传控件使用

    protected void Button1_Click(object sender, EventArgs e) { string str = ""; if (FileUpload ...

  7. C# 多媒体播放器

    //停止播放 public void stopFile() { axWindowsMediaPlayer1.Ctlcontrols.stop(); } //暂停文件 public void pause ...

  8. C# 窗体在线2,8,16进制转换以及,在线更新时间

    class Program { static void Main(string[] args) { //十进制转二进制 Console.WriteLine(, )); //十进制转八进制 Consol ...

  9. pop,墨刀,快现、justinmind 、Axure

    原型设计软件 墨刀:https://modao.cc Justinmind: http://www.zhihu.com/question/26662368/answer/33586218 http:/ ...

  10. ArrayList集合-[习题]--C#

    :向集合中添加10个元素,计算平均值,求最大.最小值. ; list.AddRange(, , , , , , , , }); int Max, Min; Max = Min = (]; ; i &l ...