/*
就是每当遇到一个数据,就进行判断这个数据的前面一个操作符号是什么,可不可运算;
就这样运行下去,直到只剩下一个数据了;
*/
#include <iostream>
#include <fstream>
#include <cstring>
#include <stack>
using namespace std;
stack<char> sta_ch;
stack<int> sta_num;
void solve();
void execute(int op){
switch(op){
case 1:{
int cha = sta_num.top(); sta_num.pop();
int chb = sta_num.top(); sta_num.pop();
if(cha & chb){
sta_num.push(1);
solve();
}else{
sta_num.push(0);
solve();
}
break;
}
case 2:{
int cha = sta_num.top(); sta_num.pop();
int chb = sta_num.top(); sta_num.pop();
if(cha | chb){
sta_num.push(1);
solve();
}else{
sta_num.push(0);
solve();
}
break;
}
case 3:{
int cha = sta_num.top(); sta_num.pop();
if(cha){
sta_num.push(0);
solve();
}else{
sta_num.push(1);
solve();
}
break;
}
}
}
void solve(){
if(sta_ch.empty()){
return ;
}
char ch = sta_ch.top();
if(ch == '('){
return ;
}
if(ch == ')'){
sta_ch.pop();
return ;
}
if(ch == '&'){
sta_ch.pop();
execute(1);
return ;
}
if(ch == '|'){
sta_ch.pop();
execute(2);
return ;
}
if(ch == '!'){
sta_ch.pop();
execute(3);
return ;
}
}
int main(){
char str1[200];
int count = 0;
while(gets(str1)){
int len = strlen(str1);
for(int i=0; i<len; ++i){
while(str1[i] == 1){
++i;
}
if(str1[i] == '(' || str1[i] == ')' || str1[i] == '!' || str1[i] == '&' || str1[i] == '|'){
if(str1[i] == ')'){
sta_ch.pop();
solve();
continue ;
}
sta_ch.push(str1[i]);
}else if(str1[i] == 'V' || str1[i] == 'F'){
if(str1[i] == 'V'){
sta_num.push(1);
}else{
sta_num.push(0);
}
solve();
}
}
while(sta_num.size() != 1){
solve();
}
int num = sta_num.top(); sta_num.pop();
char temp = 'F';
if(num == 1){
temp = 'V';
}
printf("Expression %d: %c\n",++count, temp);
}
return 0;
}
POJ 2106(一个一个死算出来的)
猜你喜欢
转载自blog.csdn.net/julicliy/article/details/79336245
今日推荐
周排行