POJ 1686 Lazy Math Instructor (模似题+栈的运用) 各种坑
You are to write a program to read different formulas and determine whether or not they are arithmetically equivalent.
- 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.
(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)
YES
NO
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<math.h>
using namespace std;
typedef struct nn
{
int pri;//优先级,'(' > '*' > '+' = '-' > ')'
char cc;//运算符
}node;
int main()
{
int t,i,j,k,len,top1,top2;
char ch[2][100];
__int64 anss[1000],tem,ans[2],as[100],tt,n[2];//anss为数字栈,as中间数字栈,tem,ans[]为中间变量,n[]为最终的两个值进行比较
node C[1000];//符号栈
node c,c1;
scanf("%d",&t);
getchar();
while(t--)
{
gets(ch[0]); gets(ch[1]);//一定要注意输入
k=-1;
for(j=0;j<2;j++)
{
top1=top2=0;//分别指向数字栈和符号栈
len=strlen(ch[j]);
for(i=0;i<=len;i++)//注意等号
{
if(ch[j][i]==' ')//为空时,不往下运行
continue;
if(ch[j][i]>='0'&&ch[j][i]<='9'||ch[j][i]>='a'&&ch[j][i]<='z')
{
if(ch[j][i]>='0'&&ch[j][i]<='9')
as[++k]=ch[j][i]-'0';
else
as[++k]=ch[j][i]-'a'+97;//把a变成ASC码数
}
else
{
tt=1;tem=0;
for(;k>=0;k--)//变成一个整数
{
tem=tem+as[k]*tt; tt*=10;
}
if(tt!=1) anss[++top1]=tem;//当有数时存入数字栈printf("%I64d %d \n",tem,i);}
if(i==len) break;
if(ch[j][i]=='(') {c.pri=3;c.cc='(';}
if(ch[j][i]==')') {c.pri=0;c.cc=')';}
if(ch[j][i]=='+') {c.pri=1;c.cc='+';}
if(ch[j][i]=='-') {c.pri=1;c.cc='-';}
if(ch[j][i]=='*') {c.pri=2;c.cc='*';} if(top2!=0&&c.cc!='(')
{
c1=C[top2];
if(c1.cc=='('&&c.cc==')') //可以去掉空括号
{top2--;continue;} if(c1.pri<c.pri||c1.cc=='(') //当前符号的优先级大于栈顶或栈顶为右括号时,当前符号存入符号栈中
{C[++top2]=c;continue;}
while(top2>0&&c1.pri>=c.pri) //符号栈不为空并且当前的运算符的优先级小于等于栈顶
{
top2--; //退一个符号栈顶
ans[1]=anss[top1];top1--;
ans[0]=anss[top1];top1--; //printf("%I64d %c %I64d",ans[0],c1.cc,ans[1]);
if(c1.cc=='+') {ans[0]+=ans[1];anss[++top1]=ans[0];}
if(c1.cc=='-') {ans[0]-=ans[1];anss[++top1]=ans[0];}
if(c1.cc=='*') {ans[0]*=ans[1];anss[++top1]=ans[0];}
c1=C[top2]; //取出,为下一次运算printf("= %I64d\n",ans[0]);
if(c1.cc=='('||top2==0||c.pri>c1.pri) //在一直往前运算时,有三种情况要退出当前运算,
{ //1:一直运算到( ,2:栈为空,3:栈顶符优先级大于当前运算符优先级
if(c.cc==')') top2--;
else C[++top2]=c; //后两种情况时,要把当前的符号放入到符号栈中
break; //跳出循环
}
}
}
else//当符号栈为空或当前符号为( 时,直接放入符号栈中
{
C[++top2]=c;
}
}
}
while(top2>0)//运算还没有算完的运算符
{
c1=C[top2]; top2--;
ans[1]=anss[top1];top1--;
ans[0]=anss[top1];top1--;
if(c1.cc=='+') {ans[0]+=ans[1];anss[++top1]=ans[0];}
if(c1.cc=='-') {ans[0]-=ans[1];anss[++top1]=ans[0];}
if(c1.cc=='*') {ans[0]*=ans[1];anss[++top1]=ans[0];}
}
n[j]=anss[top1];//到这一步时,数字栈一定只有一个 数字(结果)
}
printf("%s\n",n[0]==n[1]?"YES":"NO");
}
}
/*
15
2 * 1-3+( 2 * 5 )
9
8
((2 * 4)-7*2+(5-(6 * 7)+ 4 ) )
(a)+(((b)))*(a-(2*b))*(1)*(b-2+c+a)*(d+e+a-f+b)
a * b * a * b * a * a * 9 * b * a * 8 * b * 7 * c * 9 * a
(((a)*a)*a * b * ( a-b)*( a - b )*(a-c)*(a+b))*(((b-c)))
b*a*(a*(a*(b-c))*((a+b)*(a-c))*(a-b)*(a-b))
(w-a)+(a-b)-(c+a)*4+d
(a-w)+(b-a)-(a+c)*d+4
a-b*c+a+d*a-c*a+e*a*b*9*z*8*w*7
(a*a*a*a*b*(c-b+1)+(a*b*(a*(a*d-c)+e)))*9*z*8*w*7
(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)
(a-b)*(a-b)
(a*a)-(2*a*b)+(b*b)
a*b
b*a
1+a-a
b+1-b
2+1+a+1
1+3+a
(1+a+b)+7 +c+(d)
a+c+b+d +1+1+(1+(2+3))
((((1)+a)+b))+6 +c+(d)
a+c+b+d +1+1+(1+(2+3))
*/
POJ 1686 Lazy Math 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(栈)
原题目网址:http://poj.org/problem?id=1686 题目中文翻译: Description 数学教师懒得在考卷中给一个问题评分,因为这个问题中,学生会为所问的问题提出一个复杂的公 ...
- poj 1684 Lazy Math Instructor(字符串)
题目链接:http://poj.org/problem?id=1686 思路分析:该问题为表达式求值问题,对于字母使用浮点数替换即可,因为输入中的数字只能是单个digit. 代码如下: #includ ...
- Lazy Math Instructor
Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 3721 Accepted: 1290 Description A m ...
- UVALive 2056 Lazy Math Instructor(递归处理嵌套括号)
因为这个题目说明了优先级的规定,所以可以从左到右直接运算,在处理嵌套括号的时候,可以使用递归的方法,给定每一个括号的左右边界,伪代码如下: int Cal(){ if(括号) sum += Cal( ...
- POJ - 2183 Bovine Math Geniuses
“模拟“题,运用哈希,不断地按照一定运算规律对一个结果进行计算,如果重复出现就停止并且输出该数.注意到仔细看题,这种题一定要细心! POJ - 2183 Bovine Math Geniuses Ti ...
- POJ 1061 青蛙的约会 数论水题
http://poj.org/problem?id=1061 傻逼题不多说 (x+km) - (y+kn) = dL 求k 令b = n-m ; a = x - y ; 化成模线性方程一般式 : Lx ...
- UVA.12096 The SetStack Computer ( 好题 栈 STL混合应用)
UVA.12096 The SetStack Computer ( 好题 栈 STL混合应用) 题意分析 绝对的好题. 先说做完此题的收获: 1.对数据结构又有了宏观的上的认识; 2.熟悉了常用STL ...
- POJ 1845-Sumdiv(快速幂取模+整数唯一分解定理+约数和公式+同余模公式)
Sumdiv Time Limit:1000MS Memory Limit:30000KB 64bit IO Format:%I64d & %I64u Submit Statu ...
随机推荐
- 完整的 dataType=text/plain jquery ajax 登录验证
Html: <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <m ...
- SGU 168.Matrix
时间限制:0.5s 空间限制:15M 题意: 给出一个N*M的矩阵A,计算矩阵B,满足B[i][j]=min{ A[x][y]:(y>=j) and ( x>=i+j-y )} Solut ...
- Linux系统、版本、CPU、内存查看、硬盘空间
查看系统版本:lsb_release -a [root@localhost /]# lsb_release -a LSB Version: :core-4.0-amd64:core-4.0-no ...
- Debian 8.0(Jessie) 无线网卡,ATI显卡驱动和输入法等安装记录。
转载请注明作者与出处!谢谢! 最近准备彻底转换到Linux平台,之前一直用的是Red Hat,对Debian不是很熟悉,花了不少时间摸索.下面记录一下安装的过程以便备忘,顺便给他人能做个参考. 我的是 ...
- javascript获取div高度
DOM的: 获得了div的高度值 = document.getElementById("目标id").offsetHeight;
- cocos2dx-html5 实现网页版flappy bird游戏
我也是第一次使用cocos2d_html5,对js和html5也不熟,看引擎自带的例子和引擎源码,边学边做,如果使用过cocos2d-x的话,完成这个游戏还是十分简单的.游戏体验地址: http:// ...
- 关于system(”pause“);的作用和意义
注意:不要再return 的语句之后加,那样就执行不到了. system() 是调用系统命令:pause 暂停命令: 如果加有 system(”pause“): 这样在运行到此处时,会显示“Pres ...
- vs 2015 菜单重复的问题解决方法
打开 “运行” 输入 D:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\devenv.exe /resetuserdata ...
- 【转】JQUERY相关的几个网站
作者:Terry li - GBin1.com 1. John Resig - http://ejohn.org 毫无疑问,jQuery 的缔造者的博客是你首先必须关注的. 2. Filmament ...
- 禁用窗体关闭按钮(使用GetWindowLong修改GWL_STYLE)
一般我们不想让窗体能够关闭, 首先想到的是在OnCloseQuery事件里设置CanClose := False, 不过在某些情况下这个会和程序关闭窗体的业务逻辑产生冲突 所以写了下面这个函数, 可以 ...