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. grep egrep fgrep命令

    一.grep.egrep.fgrep命令 本文中主要介绍了linux系统下grep egrep fgrep命令和正则表达式的基本参数和使用格式.方法.(注释:文中fg代表例子,) 1.1.基本定义: ...

  2. CreateFile函数使用方法详细介绍

    CreateFileThe CreateFile function creates or opens the following objects and returns a handle that c ...

  3. KVM的虚拟化研究及应用

    引言 虚拟化技术是IBM在20世纪70年代首先应用在IBM/370大型机上,这项技术极大地提高了大型机资源利用率.随着软硬件技术的迅速发展,这项属于大型机及专利的技术开始在普通X86计算机上应用并成为 ...

  4. Adobe Acrobat Ⅺ Pro安装激活

    1.注意一定要断网安装,如果你有防火墙拦截亦可(注意:系统自带那防火墙不行). 2.将AcrobatPro_11_Web_WWMUI.exe解压到一个目录下,找到目录下的setup.exe安装,安装时 ...

  5. UVa 10330 Power Transmission / 最大流

    最大流 这题有很多起点和终点 在取2个点(0和n+1) 作为唯一的起点和终点 此外每个点也有容量限制 建图时每条边上的容量为这条边和2个端的容量的最小值 然后EK就行 #include <cst ...

  6. saltstack:使用教程之一安装及客户端返回写入MySQL

    saltstack使用教程: 1.安装: 需要epel的yum源,没有的话把下面的复制并新建个文件 /etc/yum.repos.d/epel.repo 粘贴即可: [epel] name=Extra ...

  7. mac忘记登陆密码解决

    重新启动苹果电脑,开机时按住“command”键+“S”键,(普通键盘按住win+s)会进入单用户模式, 出现像DOS一样的提示符 #root>依次输入如下三个命令:(注意空格 注意大小写) f ...

  8. Savitzky-Golay滤波器(2)

    前几天写过一篇介绍 Savitzky-Golay滤波器的文章, 没想到最近做项目还真的用上了. 因此就顺便写了个 C 语言的自动计算生成 SG 滤波器系数的程序.利用这里的代码可以生成任意阶数的 SG ...

  9. 简单的方式实现javascript 小数取整

    JS: function truncateNumber(n){ return n|0; } 測试: console.log(truncateNumber(12.345)); 浏览器打印出12

  10. 【linux】arm mm内存管理

    欢迎转载,转载时请保留作者信息,谢谢. 邮箱:tangzhongp@163.com 博客园地址:http://www.cnblogs.com/embedded-tzp Csdn博客地址:http:// ...