poj2106 http://poj.org/problem?id=2106
来源:互联网 发布:银河国际软件下载 编辑:程序博客网 时间:2024/06/06 05:33
//题意是判断公式的F或V 注意符号的优先级运算
//注意:对于'!',单目表达式的计算,在转后缀的时候,//需将数值进栈,再将其压入后缀表达式中;// 注意:题意中字符串的大小不包括空格#include<iostream>#include<cstdio>#include<stack>#include<map>#include<string.h>#define max 120using namespace std;char s1[max];map<char,int> ma;int Judge(char a){ if(a=='V'||a=='F') return 1; return 0;}void Convect(char ch[]){ stack<char> s; int len=strlen(ch); int sum=-1; for(int i=0; i<len; i++) { if(Judge(ch[i])) { s1[++sum]=ch[i]; } else if(ch[i]==' ') continue; else { switch(ch[i]) { case '(': s.push(ch[i]); break; case ')': while(s.top()!='(') { s1[++sum]=s.top(); s.pop(); } s.pop(); break; case '!': s.push('!');//直接压入 break; case '&': case '|': while((!s.empty())&&ma[s.top()]>=ma[ch[i]]) { s1[++sum]=s.top(); s.pop(); } s.push(ch[i]); break; } } } while(!s.empty()) { s1[++sum]=s.top(); s.pop(); } s1[++sum]=0; /* for(int i=0;i<sum;i++) printf("%c",s1[i]); printf("\n"); */}char Result(){ int op[max]; int len=strlen(s1); int add=-1; for(int i=0; i<len; i++) { if(s1[i]=='V') { op[++add]=1; } else if(s1[i]=='F') { op[++add]=0; } else { switch(s1[i]) { case '|': op[add-1]|=op[add]; add--; break; case '&': op[add-1]&=op[add]; add--; break; case '!': op[add]=!op[add]; break; } } } return op[add];}int main(){ char cha[max]; //优先级留最低的 ma['&']=2; ma['!']=3; ma['|']=1; int ans; int add=1; while(cin.getline(cha,max)) { Convect(cha); // printf("sdfds\n"); ans=Result(); if(ans) printf("Expression %d: V\n",add++); else printf("Expression %d: F\n",add++); } return 0;}
- poj2106 http://poj.org/problem?id=2106
- http://poj.org/problem?id=2965
- http://poj.org/problem?id=2075
- http://poj.org/problem?id=1125
- http://poj.org/problem?id=2983
- http://poj.org/problem?id=2406
- http://poj.org/problem?id=1961
- http://poj.org/problem?id=1486
- http://poj.org/problem?id=1062
- http://poj.org/problem?id=2983
- http://poj.org/problem?id=3159
- http://poj.org/problem?id=1201
- http://poj.org/problem?id=3259
- http://poj.org/problem?id=3233
- http://poj.org/problem?id=1273
- http://poj.org/problem?id=3281
- http://poj.org/problem?id=1007
- http://poj.org/problem?id=1840
- 常用函数举例
- Tomcat 系统架构与设计模式,第 2 部分: 设计模式分析
- android UIWDT
- recycle pool有没有必要
- 430中断
- poj2106 http://poj.org/problem?id=2106
- 异步和多线程的区别
- 2013年Web设计六大趋势
- android实时滤镜的效率问题
- JAVA断言使用
- 【More Effective C#】IEnumerable和IQueryable的那点事
- 探索 Erlang Abstract Form--Module声明和Form
- Redis 学习 (三) 常用命令
- Windows7/win7 iis的安装与配置