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. ASP.NET MVC 4 批量上传文件

    上传文件的经典写法: <form id="uploadform" action="/Home/UploadFile" method="post& ...

  2. JSONP与JSON的关系

    这篇文章<说说JSON和JSONP,也许你会豁然开朗>写得非常棒,推荐一下 http://kb.cnblogs.com/page/139725/ (大神,对不起,我记录一下,因为你写的真得 ...

  3. yii2.0的gii生成代码bug

    自动生成代码真的很好用,能减少很多基础代码的编写,如果这些基础代码一个个手动去敲,即枯燥乏味,还容易出错(话说人类真的不适合做单调重复的工作),yii框架的gii自动生成代码工具就能减少很多工作量.前 ...

  4. hdu 4628 动态规划

    思路:首先就是状态压缩,然后判断哪些状态是回文串.最后就是动态方程:dp[i]=min(dp[i],dp[j]+1).这个方程得前提条件是状态(j-i)为回文串. #include<iostre ...

  5. Commons CLI - Usage

    Usage Scenarios The following sections describe some example scenarios on how to use CLI in applicat ...

  6. HTTP - 首部

    首部类型 首部类型 说明  通用首部   客户端和服务器都可以使用的通用首部.可以在客户端.服务器和其他应用程序之间提供一些有用的通用首部.  请求首部   请求首部时请求报文特有的.它们为服务器提供 ...

  7. Asp.net中基于Forms验证的角色验证授权

    Asp.net的身份验证有有三种,分别是"Windows | Forms | Passport",其中又以Forms验证用的最多,也最灵活. Forms 验证方式对基于用户的验证授 ...

  8. 【Cocos2d入门教程七】三分钟看懂Cocos2d坐标系

    无论是搞2d还是3d开发,最需要搞清楚的就是坐标系,这部分混乱的话就没啥搞头了.所以玩cocos2d,一上来就需要先把各种与坐标有关的东西搞清楚. 1.OpenGL坐标系 Cocos2d-x使用的是O ...

  9. SQL_从星期一到星期六自动打卡SQL代码

    create proc sp_MarkAutoKQ as begin ) ---创建两个变量,接收当前时间和当天是星期几 set @dateA=getdate() ---获取当前时间 set @dat ...

  10. firebug调试js时提示调试器未激活处理办法

    firebug是web开发中最常用的分析调试软件,不过我今天使用在调试百度在线编辑器UEditor时一直提示调试器未激活. 从使用经验来看不应该啊,我都下了断点了为什么会提示调试器未激活呢!多次载入网 ...