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. Github 的一些基本操作

    1.创建一个新的repository: 先在github上创建并写好相关名字,描述.例如这样一个地址: https://github.com/test/test2.git 回到本地目录如hellowo ...

  2. Spring 简单入门实例

    首先新建一个Web 项目 导入相应Jar 包 <?xml version="1.0" encoding="UTF-8"?> <beans xm ...

  3. poj3264

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 45777   Accepted: 21499 ...

  4. 命令行创建Windows窗体应用程序

    csc:(C Sharp Compiler) 类似于 javac (java Compiler) 命令行的编译工具 位置:C:\Windows\Microsoft.NET\Framework\v4.0 ...

  5. Jedis - hello world

    Maven Dependency: <dependency> <groupId>redis.clients</groupId> <artifactId> ...

  6. php学习-快速开发框架thinkphp-day1

    以下操作针对windows系统. 1.下载thinkphp3.23-all并解压 官方网站: http://www.thinkphp.cn/down.html 2.开发工具使用phpstorm htt ...

  7. .Net 微信开发与微信支付

    .NET     https://github.com/JeffreySu/WeiXinMPSDK JAVA   http://git.oschina.net/pyinjava/fastweixin ...

  8. MVC5----用户登陆及验证码

    随便写写记录一下学习的过程 登陆 Models中添加添加 public class LoginViewModel { [Required(ErrorMessage = "*")] ...

  9. 4月10日学习笔记——jQuery选择器

    概念 jQuery 是一套Javascript脚本库,注意 jQuery 是脚本库,而不是脚本框架."库"不等于"框架".jQuery 并不能帮助我们解决脚本的 ...

  10. 暑假集训(4)第三弹 -----递推(Hdu1799)

    问题描述:还记得正在努力脱团的小A吗? 他曾经最亲密的战友,趁他绘制贤者法阵期间,暗中设下鬼打墙将小A 围困,并准备破坏小A正在绘制的法阵.小A非常着急.想阻止他的行动.而要阻止他,必须先破解鬼打墙. ...