POJ2106 Boolean Expressions(模拟,双栈法)

题目:

Boolean Expressions
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 4398 Accepted: 1342

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

Source

[Submit]   [Go Back]   [Status]   [Discuss]

思路:

题意很简单,题目给了一串布尔表达式,让你求这一串表达式的真假性。利用两个栈一个用来存表示计算结果的布尔符号,另一个栈来存进行运算的符号(如&、|、!),具体看注释

代码:

#include <stdio.h>
#include <string.h>
#include <string>
#include <iostream>
#include <stack>
#include <cmath>
#include <queue>
#include <vector>
#include <algorithm>
#define mem(a,b) memset(a,b,sizeof(a))
#define inf 0x3f3f3f3f
#define N 1000+10
#define M 1000000+10
#define LL long long
using namespace std;
char s[100];
stack<bool>num;
stack<char>chars;
char to_char(bool x)//布尔值转换成字符
{
    if(x)
        return 'V';
    else
        return 'F';
}
bool to_int(char c)//字符转换成布尔值
{
    if(c=='V')
        return 1;
    else
        return 0;
}
void deal_int(char c)
{
    if(c==')')//当处理到右括号的时候,证明一个括号已经结合完毕,当前的栈顶是左括号,直接使左括号出站
        chars.pop();
    else
        num.push(to_int(c));//把当前的F或者V转换成布尔型
    if(chars.empty()||chars.top()=='(')//有多重括号,或者符号栈为空就返回进行下一次
        return;
    bool t_num;//临时标记当前的布尔值
    while(!chars.empty()&&!num.empty()&&chars.top()!='(')
    {
        if(chars.top()=='!')//‘!’取值的时候不需要num中的符号大于1,!可以直接运算
        {
            if(!chars.empty())
            {
                //进行运算并使原来的符号栈顶出站,把新的布尔值加入num栈
                t_num=(!num.top());
                num.pop();
                chars.pop();
                num.push(t_num);
            }
            else
                break;
        }
        else if(chars.top()=='&')//当运算符为"&"的时候,必需至少有两个布尔值才能进行运算
        {
            if(num.size()>1)
            {
                t_num=num.top();
                num.pop();
                chars.pop();
                t_num=(num.top()&&t_num);
                num.pop();
                num.push(t_num);

            }
            else
                break;

        }
        else if(chars.top()=='|')//与上面同理
        {

            if(num.size()>1)
            {
                t_num=num.top();
                num.pop();
                chars.pop();
                t_num=(num.top()||t_num);
                num.pop();
                num.push(t_num);
            }
            else
                break;
        }
    }
    return;

}
int solve()
{
    //清空栈
    while(!num.empty())num.pop();
    while(!chars.empty())chars.pop();
    int len=strlen(s);
    //遍历字符串
    for(int i=0; i<len; i++)
    {
        if(s[i]==' ')//跳过空格
            continue;
        if(s[i]==')'||s[i]=='F'||s[i]=='V')//需要进行计算的时候
            deal_int(s[i]);
        else
            chars.push(s[i]);
    }
    return to_char(num.top());
}
int main()
{
    int q=1;
    while(gets(s))
    {
        printf("Expression %d: %c\n",q++,solve());
    }

    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值