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 <map>
#include <stack>
#include <cctype>
#include <iostream>
using namespace std;
map<char,int> m;
string a1,a2,v1,v2;
string bianhouzui(string a) //这里是将公式变成后缀式
{
int i,j=,len;
char c[];
stack<char> z1;
len=a.size();
for(i=; i<len; i++)
{
if(isalnum(a[i])) //isalnum是判断是不是数字或者字母,如果是返回真
c[j++]=a[i];
else
{
switch (a[i])
{
case '(':
z1.push(a[i]);
break;
case ')':
while(z1.top()!='(')
{
c[j++]=z1.top();
z1.pop();
}
z1.pop();
break;
case '+':
case '-':
case '*':
while(!z1.empty()&&m[a[i]]<=m[z1.top()])
{
c[j++]=z1.top();
z1.pop();
}
z1.push(a[i]);
}
}
}
while(!z1.empty())
{
c[j++]=z1.top();
z1.pop();
}
c[j]='\0';
string x=c;
return x;
} int jisuan(string v) //这里是计算
{
int a,b,i,len;
len=v.size();
stack<int> z2;
for(i=; i<len; i++)
{
if(isalnum(v[i]))
{
if(isdigit(v[i])) // 判断是不是数字
z2.push(v[i]-''); //转成ASCII码
else
z2.push(v[i]);
}
else
{
a=z2.top();
z2.pop();
b=z2.top();
z2.pop();
switch (v[i])
{
case '+':
z2.push(b+a);
break;
case '-':
z2.push(b-a);
break;
case '*':
z2.push(b*a);
}
}
}
return z2.top();
} int main()
{
int T;
m['(']=;
m['+']=m['-']=;
m['*']=;
cin>>T;
cin.get(); //没有这个就不行,我也不知道为什么,貌似好像是什么回车问题.....
while(T--)
{
getline(cin,a1);
getline(cin,a2);
v1= bianhouzui(a1);
v2= bianhouzui(a2);
if(jisuan(v1)==jisuan(v2))
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
}
return ;
}

lazy 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. Lazy Math Instructor

      Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 3721   Accepted: 1290 Description A m ...

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

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

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

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

  6. POJ 1686 Lazy Math Instructor(栈)

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

  7. 代码的坏味道(15)——冗余类(Lazy Class)

    坏味道--冗余类(Lazy Class) 特征 理解和维护类总是费时费力的.如果一个类不值得你花费精力,它就应该被删除. 问题原因 也许一个类的初始设计是一个功能完全的类,然而随着代码的变迁,变得没什 ...

  8. Mach-O 的动态链接(Lazy Bind 机制)

    ➠更多技术干货请戳:听云博客 动态链接 要解决空间浪费和更新困难这两个问题最简单的方法就是把程序的模块相互分割开来,形成独立的文件,而不再将它们静态的链接在一起.简单地讲,就是不对那些组成程序的目标文 ...

  9. Lazy Load, 延迟加载图片的 jQuery 插件.

    Lazy Load 是一个用 JavaScript 编写的 jQuery 插件. 它可以延迟加载长页面中的图片. 在浏览器可视区域外的图片不会被载入, 直到用户将页面滚动到它们所在的位置. 这与图片预 ...

随机推荐

  1. css笔记08:id选择器之父子选择器

    1.父子选择器 (1)01.html <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " ...

  2. C++中枚举定义运算符

    由于枚举也是用户定义类型,所以是可以定义运算符, 如: enum Day {sun, mon, tue, wen, thu, fri, sat}; Day& operator++(Day&am ...

  3. win10 IIS10 HTTP 错误 404.2 - Not Found

    环境win10系统IIS10里边发布web应用程序的时候,出现 HTTP 错误 404.2 - Not Found 由于 Web 服务器上的“ISAPI 和 CGI 限制”列表设置,无法提供您请求的页 ...

  4. ListView使用自定义适配器的情况下实现适配器的文本和图标控件点击事件执行Activity界面中的方法

    ListView使用的是自定义适配器,列表项的布局文件中含有文本和图标,实现文本区域和图标区域的点击事件. 实现思路:在自定义适配器MyArrayAdapter 类型中自定义接口和接口方法,分别设置文 ...

  5. Linux系统内核制作和内核模块的基础

    Linux系统内核制作 1.清除原有配置与中间文件 x86:  make distclean arm:  make distclean 2.配置内核 x86:  make menuconfig arm ...

  6. hdu 2004 成绩转换

    成绩转换 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submi ...

  7. JS中的嵌套作用域

    在JS中仅仅区分全局变量和局部变量还不够,实际上,变量作用域可以有任意层级(嵌套).其他函数内部定义的函数可以调用父函数的局部变量,而内部函数里定义的函数则不仅可以调用父函数的局部变量,还可以调用祖父 ...

  8. Forms身份认证

    引言 大家都知道Http是无状态的协议,所以访问一个url,你并不能知道用户在之前是否已经登陆过.但是许多业务上的逻辑又离不开user的信息,这个时候就可以借助身份认证来记录当前user的登录状态.这 ...

  9. java实现的身份证照片脸部识别(头像截图) 以及OCR字体识别

    断断续续地折腾了大半个月,终于把身份证照片脸部识别以及OCR字体识别功能用Java实现了,需求很简单:通过摄像头所照的一张放在黑色底板上的身份证照,识别照片上身份证里面的人名和地址(OCR中文),再截 ...

  10. 搭建私有git代码托管服务就是这么简单(简单5步)

    部署一个git代码托管服务就是这么简单 --基于阿里云ecs以docker容器运行gogs代码托管服务 部署步骤: 1.新建ecs云主机,选定操作系统为ubuntu 12.4tls 2.搭建docke ...