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. Xcode插件(一)-规范注释生成器VVDocumenter

    原文来自:http://blog.csdn.net/hitwhylz/article/details/27813315 分享几个常用的Xcode插件. 第一个, 规范注释生成器VVDocumenter ...

  2. Chapter 3.单一职责原则

    单一职责原则:就一个类而言,应该仅有一个引起它变化的原因. 如果一个类承担的职责过多,就等于把这些职责耦合在一起,一个职责的变化可能会削弱或者抑制这个类完成其他职责的能力,就等于把这些职责耦合在一起, ...

  3. Chapter 9 原型模式

    原型模式:用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象. 简单的说就是clone一个对象实例.使得clone出来的copy和原有的对象一模一样. 插一个简单使用clone的例子,如果 ...

  4. 以正确的方式开源 Python 项目

    以正确的方式开源 Python 项目 大多数Python开发者至少都写过一个像工具.脚本.库或框架等对其他人也有用的工具.我写这篇文章的目的是让现有Python代码的开源过程尽可能清 晰和无痛.我不是 ...

  5. Java进阶03 IO基础

    链接地址:http://www.cnblogs.com/vamei/archive/2013/04/11/3000905.html 作者:Vamei 出处:http://www.cnblogs.com ...

  6. python中eval, exec, execfile,和compile [转载]

    eval(str [,globals [,locals ]])函数将字符串str当成有效Python表达式来求值,并返回计算结果. 同样地, exec语句将字符串str当成有效Python代码来执行. ...

  7. Qt实现QQ好友下拉列表(用QListView实现,所以还得定义它的Model)

    偶然发现Qt有个控件可以实现下拉列表,所以就试着实现一下类似QQ面板的下拉列表,这里主要实现几个功能: 1.可以删除列表中图标 2.可以像qq一样的,把某个分组下的图标转移到另外的分组 3.添加分组 ...

  8. WPF常用转换

    原文 WPF常用转换 以下是代码中常常用到的一些转换,整理如下,后续再不断完善: 1.string和Color的转换 //string转Color (Color)ColorConverter.Conv ...

  9. Android_简单笔记一

    入门学习Android的简单笔记(已经安装好了开发环境ADT) 一.关于 AndroidManifest.xml文件 1. android:icon和android:label定义了应用程序安装后显示 ...

  10. 浅尝key-value数据库(三)——MongoDB的分布式

    浅尝key-value数据库(三)——MongoDB的分布式 测试了单机MongoDB的随机读和写入性能,这一节来讲一讲MongoDB的分布式. MongoDB的分布式分成两种,一种是Replicat ...