lazy instructor
Description
You are to write a program to read different formulas and determine whether or not they are arithmetically equivalent.
Input
- 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
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的更多相关文章
- 数据结构——POJ 1686 Lazy Math Instructor 栈的应用
Description A math instructor is too lazy to grade a question in the exam papers in which students a ...
- POJ 1686 Lazy Math Instructor (模似题+栈的运用) 各种坑
Problem Description A math instructor is too lazy to grade a question in the exam papers in which st ...
- Lazy Math Instructor
Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 3721 Accepted: 1290 Description A m ...
- poj 1684 Lazy Math Instructor(字符串)
题目链接:http://poj.org/problem?id=1686 思路分析:该问题为表达式求值问题,对于字母使用浮点数替换即可,因为输入中的数字只能是单个digit. 代码如下: #includ ...
- UVALive 2056 Lazy Math Instructor(递归处理嵌套括号)
因为这个题目说明了优先级的规定,所以可以从左到右直接运算,在处理嵌套括号的时候,可以使用递归的方法,给定每一个括号的左右边界,伪代码如下: int Cal(){ if(括号) sum += Cal( ...
- POJ 1686 Lazy Math Instructor(栈)
原题目网址:http://poj.org/problem?id=1686 题目中文翻译: Description 数学教师懒得在考卷中给一个问题评分,因为这个问题中,学生会为所问的问题提出一个复杂的公 ...
- 代码的坏味道(15)——冗余类(Lazy Class)
坏味道--冗余类(Lazy Class) 特征 理解和维护类总是费时费力的.如果一个类不值得你花费精力,它就应该被删除. 问题原因 也许一个类的初始设计是一个功能完全的类,然而随着代码的变迁,变得没什 ...
- Mach-O 的动态链接(Lazy Bind 机制)
➠更多技术干货请戳:听云博客 动态链接 要解决空间浪费和更新困难这两个问题最简单的方法就是把程序的模块相互分割开来,形成独立的文件,而不再将它们静态的链接在一起.简单地讲,就是不对那些组成程序的目标文 ...
- Lazy Load, 延迟加载图片的 jQuery 插件.
Lazy Load 是一个用 JavaScript 编写的 jQuery 插件. 它可以延迟加载长页面中的图片. 在浏览器可视区域外的图片不会被载入, 直到用户将页面滚动到它们所在的位置. 这与图片预 ...
随机推荐
- linux - 输入输出重定向 及 管道
> 正确结果重定向 2> 错误结果重定向 &> 正确和错误全部重定向 >> 追加,其它同> 标准输出实际上就是显示器,比如我们使用cat命令打开一个文件,文 ...
- CF Vitaly and Strings
Vitaly and Strings time limit per test 1 second memory limit per test 256 megabytes input standard i ...
- Spring 简单入门实例
首先新建一个Web 项目 导入相应Jar 包 <?xml version="1.0" encoding="UTF-8"?> <beans xm ...
- Mirco2440核心板设计思考
1.核心板架构 注意的是:此处的RAM和ROM都是外置的 硬件框架 S3C2440+ SDRAM + NAND Flash + NOR Flash 也就是 CPU + RAM + ROM 2.S3C2 ...
- CSS行高--line-height
遇到的问题:在css中,不理解line-height:1与line-height:1px的区别 发现的过程:最近在学做一个网站的过程中,设置两行文字之间的行高时需要用到line-height,发现了这 ...
- Lombok(1.14.8) - @NonNull
@NonNull @NonNull,生成一个非空检查. package com.huey.lombok; import lombok.Getter; import lombok.NonNull; im ...
- boost.ASIO-可能是下一代C++标准的网络库
曾几何时,Boost中有一个Socket库,但后来没有了下文,C++社区一直在翘首盼望一个标准网络库的出现,网络上开源的网络库也有不少,例如Apache Portable Runtime就是比较著名的 ...
- python学习day4--python基础--购物小程序
'''购物小程序:用户启动时先输入工资用户启动程序后打印商品列表允许用户选择购买商品允许用户不断购买各种商品购买时检测余额是否够,如果够直接扣款,否则打印余额不足允许用户主动退出程序,退出时打印已购商 ...
- 过程式编程 drawShapes
// // main.m // 3.2.1 过程式编程 #import <Foundation/Foundation.h> typedef enum { kCircle, kRectang ...
- 生成 网站“面包屑” XML
using System; using System.Collections.Generic; using System.IO; using System.Threading; using Syste ...