poj1686Lazy Math Instructor
来源:互联网 发布:excel 编程 示例 编辑:程序博客网 时间:2024/05/17 07:58
http://poj.org/problem?id=1686
构造栈,表达式求值。我只会C语言。。
#include <stdio.h>#include <string.h>char opp[8] = {'+','-','*','/','(',')','#'}; int priority[7][7] = {{1,1,-1,-1,-1,1,1},{1,1,-1,-1,-1,1,1},{1,1,1,1,-1,1,1},{1,1,1,1,-1,1,1},{-1,-1,-1,-1,-1,0,-2},{1,1,1,1,-2,1,1},{-1,-1,-1,-1,-1,-2,0}};//算符之间的优先关系int calculate(int x,int y, char op) { switch(op) { case '+':return x+y; case '-':return x-y; case '*':return x*y; case '/':return x/y; }}int Compare(char a,char b)//比较优先级{ int row,col,i; for(i=0; i<7; i++) { if( a == opp[i] ) row = i; if( b == opp[i] ) col = i; } return priority[row][col];}int result(char str[],int plus)//求表达式的结果{ char ch, op, opetator[100]; int x,y,i=0,count1=0,count2=0,operand[100]; opetator[count2++] = '#'; while(str[i]!='#' || opetator[count2-1]!='#') { ch = str[i]; if(ch>='0'&&ch<='9')//阿拉伯数字‘0’~‘9’ { operand[count1++] = ch-'0'; i++; continue; } if((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z'))//英文字母 { operand[count1++] = ch-'a'+plus; i++; continue; } if(ch=='+' || ch=='-' || ch=='*' || ch=='(' || ch==')' || ch=='#')//操作符 { switch(Compare(opetator[count2-1],ch)) { case -1: opetator[count2++] = ch; i++; break;//当前元素大于栈顶元素 case 0 : count2--; i++; break;//当前元素等于栈顶元素 case 1 : op = opetator[--count2]; y = operand[--count1]; x = operand[--count1]; operand[count1++] = calculate(x,y,op); break;//当前元素小于栈顶元素 } } else i++; } return operand[count1-1];}int main(){ char fir[90], sec[90]; int N; scanf("%d",&N); getchar(); while(N--) { gets(fir); gets(sec); fir[strlen( fir )] = '#'; sec[strlen( sec )] = '#'; if(result(fir,10) != result(sec,10)) printf("NO\n"); else printf("YES\n"); } return 0;}
0 0
- poj1686Lazy Math Instructor
- E - Lazy Math Instructor
- Lazy Math Instructor
- POJ 1686 Lazy Math Instructor
- ZOJ 1418 Lazy Math Instructor
- POJ 1686 Lazy Math Instructor
- poj 1686 Lazy Math Instructor (字符串)
- zoj 1418||poj 1686 Lazy Math Instructor
- POJ 1686 Lazy Math Instructor 笔记
- poj-1686-Lazy Math Instructor(stack)
- zoj 1418 || poj 1686 Lazy Math Instructor(等价表达式)
- poj 1686 Lazy Math Instructor(表达式求值)
- POJ 1686 Lazy Math Instructor 栈的应用
- POJ 1686 Lazy Math Instructor (中缀表达式计算)
- POJ 1686 Lazy Math Instructor(数据结构-表达式树)
- POJ 1686 Lazy Math Instructor (模似题+栈的运用) 各种坑
- Lazy Math Instructor - POJ 1686 栈+提比的强行AC技能
- HDU 1686 Lazy Math Instructor(中缀转后缀,栈模拟)
- Cocos2d-3.x Android环境搭建
- 判断一个链表是否为回文结构(java实现)
- leetcode 2. Add Two Numbers
- http header mark to memory
- Unity3D KeyCode 值
- poj1686Lazy Math Instructor
- 全栈工程师笔记/记一个完整的项目流程
- Android -- WifiMonitor分析
- RStdio 常用快捷键
- DOMReady的实现
- RPM打包详细说明
- Centos 6.5 Tomcat 7 部署
- 在notepad++中直接运行python代码
- ubuntu16.04LTS安装Qt Creator