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. 【LIC】O(nlogn)解法

    [LIC--最长递增子序列问题] 在一列数中寻找一些数,这些数满足:任意两个数a[i]和a[j],若i<j,必有a[i]<a[j],这样最长的子序列称为最长递增子序列. O(nlogn)算 ...

  2. hdu 1892 树状数组

    思路:就是一个很普通的二维树状数组,注意的是x1,y1不一定在x2,y2的左下方. #include<iostream> #include<cstring> #include& ...

  3. nginx实现域名重定向

    一般网站默认的访问端口为80,当多个域名指向同一个服务器IP时,可以nginx进行重定向,分别指向不同的目的地址或其他主机. 在nginx目录下的conf/vhost子目录下建两个conf文件,hos ...

  4. Core Bluetooth Programming Guide

    https://developer.apple.com/library/ios/documentation/NetworkingInternetWeb/Conceptual/CoreBluetooth ...

  5. error LNK2005: DDX_Control 已经在 uafxcwd.lib(wincore2.obj) 中定义

    编译错误提示: 1>afxnmcdd.lib(wincore2.obj) : error LNK2005: "void __stdcall DDX_Control(classCData ...

  6. MVC3、如何应用EntityFramework 连接MySql 数据库

    原文:MVC3.如何应用EntityFramework 连接MySql 数据库 新的一年,新的开始. 今天总结的主题是在MySql中应用EntityFramework 的Code First模式. 开 ...

  7. 【Unix环境高级编程】编写变长参数函数

    文件的格式输入输出函数都支持变长参数.定义时,变长参数列表通过省略号'...'表示, 因此函数定义格式为: type 函数名(parm1, parm2,parmN,...); Unix的变长参数通过v ...

  8. WF4.0 基础篇 (十八) Flowchar

    本节主要介绍WF4 中 Flowchart的使用 本文例子下载: http://files.cnblogs.com/foundation/FlowcharSample.rar 本文例子说明 Flowc ...

  9. 前端性能优化工具--DOM Monster

    当我们开发web应用的时候,性能是一个永远不能回避的问题.其实对于DOM的性能调试也是一个不可或缺的过程.使用DOM monster你只需要添加到你的”书签中“,在任何需要调试的页面点击这个书签,它就 ...

  10. Js 对象三

    一.screen对象 Width:屏幕的宽度 Height:屏幕的高度 availWidth:屏幕的有效宽度 availhHeight:屏幕的有效高度 (不包含任务栏) colorDepth:色深 二 ...