poj 2106 Boolean Expressions

Boolean Expressions
   
   

问题分析:本题的基本大意是计算表达式的值,其中运算符包括‘|、&、!、(、)’这几种,操作数包括‘V、F’两种

 

我用栈计算这个表达式的值,建立两个栈:运算符栈和操作数栈,然后定义一个出栈函数和一个入栈函数,再就是进行运算符优先级的比较,

 

这是比较关键的一步,然后就是进行表达式的读入和计算。要考虑几种特殊情况,’!!!!F ‘和(((F))),题目不是很难,就是比较

 

繁琐,只要把情况分析清楚就可以了。

 

Description

The objective of the program you are going to produce is to evaluate boolean expressions as the one shown next:
Expression: ( V | V ) & F & ( F | V )
where V is for True, and F is for False. The expressions may include the following operators: ! for not , & for and, | for or , the use of parenthesis for operations grouping is also allowed.

To perform the evaluation of an expression, it will be considered the priority of the operators, the not having the highest, and the or the lowest. The program must yield V or F , as the result for each expression in the input file.

Input

The expressions are of a variable length, although will never exceed 100 symbols. Symbols may be separated by any number of spaces or no spaces at all, therefore, the total length of an expression, as a number of characters, is unknown.

The number of expressions in the input file is variable and will never be greater than 20. Each expression is presented in a new line, as shown below.

Output

For each test expression, print "Expression " followed by its sequence number, ": ", and the resulting value of the corresponding test expression. Separate the output for consecutive test expressions with a new line.

Use the same format as that shown in the sample output shown below.

Sample Input

( V | V ) & F & ( F| V)
!V | V & V & !F & (F | V ) & (!F | F | !V & V)
(F&F|V|!V&!F&!(F|F&V))

Sample Output

Expression 1: F
Expression 2: V
Expression 3: V
贴一下我的代码:
#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
const int maxn=100005;
char op[maxn],rp[maxn];
int top1,top2;
char Pop(char s[],int &top)
{
   top--;
   return s[top];
}
void Push(char s[],int &top,char c)
{
   s[top++]=c;
}
//判断两个运算符的优先级 ch1代表栈顶运算符,ch2代表入栈运算符 
//返回0代表ch1<ch2,直接入栈;
//返回1代表ch1>ch2,先出栈再入栈
//返回-1代表左右括号相遇
int cmp(char ch1,char ch2)
{
   if(ch1=='('&&ch2==')')
      return -1;
   if(ch1=='('||ch1=='#'||ch2=='(')
      return 1;
   if(ch2=='|'||ch2==')')
      return 0;
   if(ch2=='&')
   {
      if(ch1=='|')
         return 1;
      return 0;
   }
   if(ch2=='!')
      return 1;
}
//计算表达式的值 
char cal(char ch1,char ch2,char ch3)
{
   if(ch1=='|')
   {
      if(ch2=='F'&&ch3=='F')
         return 'F';
      return 'V';
   }
   if(ch1=='&')
   {
      if(ch2=='V'&&ch3=='V')
         return 'V';
      return 'F';
   }
}
//获得运算符栈栈顶元素
char gettop()
{
   return op[top1-1];
}
int main()
{
   char str[maxn];
   char ch1,ch2,ch3,ch4,ch5;
   int cnt=0;
   while(gets(str))
   {
      cnt++;
      top1=0,top2=0;
      op[top1++]='#';
      int len=strlen(str);
      for(int i=0;i<len;i++)
      {
         if(str[i]==' ')
            continue;
         if(str[i]=='V'||str[i]=='F')
            Push(rp,top2,str[i]);
         else
         {
            while(cmp(gettop(),str[i])==0)
            {
               ch1=Pop(op,top1);
               if(ch1=='!')
               {
                  ch2=Pop(rp,top2);
                  if(ch2=='F')
                     ch4='V';
                  else ch4='F';
               }
               else
               {
                  ch2=Pop(rp,top2);
                  ch3=Pop(rp,top2);
                  ch4=cal(ch1,ch2,ch3);
               }
               Push(rp,top2,ch4);
            }
            if(cmp(gettop(),str[i])==-1)
            {
               ch2=Pop(op,top1);
               continue;
            }
            Push(op,top1,str[i]);
         }
      } 
      ch2=Pop(op,top1);
      while(ch2!='#')
      {
         if(ch2!='!')
         {
            ch3=Pop(rp,top2);
            ch4=Pop(rp,top2);
            ch5=cal(ch2,ch3,ch4);
         }
         else
         {
            ch3=Pop(rp,top2);
            if(ch3=='F')
               ch5='V';
            else
               ch5='F';
         }
         Push(rp,top2,ch5);
         ch2=Pop(op,top1);
      }
      ch5=Pop(rp,top2);
      printf("Expression %d: %c/n",cnt,ch5);
   }
  
   return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值