将军听说最近东6闹鬼了,作为一个无神论者,将军当然不相信。但是这个传言已经泛滥了,许多人都在说这个事情,将军从每个人那里听到一个传言,将军可以容易的就知道这个传言是真还是假,但是当一大堆消息组合起来,将军就不知道了,所以将军就找到了你。
提供两种组合方式:
A&B:代表A和B都为真的时候,A和B组合起来为真,否则为假
A|B:代表A和B里面至少有1个为真的时候,A和B组合起来为真,否则为假
优先级顺序 “括号”>“&”>“|”
例如:a&b|(a|b&c),a=真,b=假,c=真;
那么上式可以这样转化
真&假 | (真|假&真)
假 | (真|假)
假 | 真
真
提供两种组合方式:
A&B:代表A和B都为真的时候,A和B组合起来为真,否则为假
A|B:代表A和B里面至少有1个为真的时候,A和B组合起来为真,否则为假
优先级顺序 “括号”>“&”>“|”
例如:a&b|(a|b&c),a=真,b=假,c=真;
那么上式可以这样转化
真&假 | (真|假&真)
假 | (真|假)
假 | 真
真
Description
有多组测试数据,每组测试数据共两行,第一行是一个逻辑范式,数据不用判错;
接下来三个字符,以空格隔开,分表代表a,b,c的真假(T/F);
接下来三个字符,以空格隔开,分表代表a,b,c的真假(T/F);
Input
对于每一组数据,输出最终的结果(TRUE or FALSE);
Output
1
2
|
a&b|(a|b&c)
T F T
|
Sample Input
1
|
TRUE
|
Sample Output
/*题解:
先将式子中a,b,c转成是T就为1,否则为0,
再将中缀表达式转成后缀表达式在计算就行了
注意:式子的结果只要>=1就是TURE,否则为FALSE*/
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include<stack>
#include<iostream>
#include<string.h>
using namespace std;
char a, b, c;
stack<char>q;
struct node
{
char k;
int Second;
}p[7] = { { '(',1 },{ ')',8 },{ '*',5 },{ '/',5 },{ '+',4 },{ '-',4 },{ '^',6 } };
void change(char *st)
{
for (int i = 0; i < strlen(st); i++)
{
if (st[i] == '&')
{
st[i] = '*';
}
else if (st[i] == '|')
{
st[i] = '+';
}
else if (st[i] == 'a')
st[i] = a;
else if (st[i] == 'b')
{
st[i] = b;
}
else if (st[i] == 'c')
{
st[i] = c;
}
}
}
bool juge(char a)
{
int ss = -1;
int tt = -1;
for (int j = 0; j < 7; j++)
{
if (a == p[j].k)
{
ss = p[j].Second;
}
if (q.top() == p[j].k)
{
tt = p[j].Second;
}
}
if (ss > tt)
{
return true;
}
else
{
return false;
}
}
void fun(char *str, char *ans)
{
int t = 0;
for (int i = 0; i < strlen(str); i++)
{
if (str[i] >= '0'&&str[i] <= '9')
{
ans[t++] = str[i];
}
else if (str[i] == '(')
{
q.push(str[i]);
}
else if (str[i] == ')')
{
while (q.top() != '(')
{
ans[t++] = q.top();
q.pop();
}
q.pop();
}
else if (q.empty())
{
q.push(str[i]);
}
else if (str[i] == '^')
{
q.push(str[i]);
}
else
{
if (juge(str[i]))
{
q.push(str[i]);
}
else
{
while (!q.empty() && !juge(str[i]))
{
ans[t++] = q.top();
q.pop();
}
q.push(str[i]);
}
}
if (i == strlen(str) - 1)
{
while (!q.empty())
{
ans[t++] = q.top();
q.pop();
}
}
}
ans[t] = '\0';
}
bool Panduan(char *str)
{
stack<char>p;
for (int i = 0; i < strlen(str); i++)
{
if (str[i] == '1' || str[i] == '0')
{
p.push(str[i]);
}
else if (str[i] == '+')
{
int aa, bb;
aa = p.top();
p.pop();
bb = p.top();
p.pop();
p.push(aa + bb - '0');
}
else if (str[i] == '*')
{
int aa, bb;
aa = p.top()-'0';
p.pop();
bb = p.top()-'0';
p.pop();
p.push(aa * bb + '0');
}
}
//cout << p.size() << endl;
if (p.top() >= '1')
{
return true;
}
else
return false;
}
int main()
{
char str[500];
while (cin >> str)
{
getchar();
cin >> a;
getchar();
cin >> b;
getchar();
cin >> c;
//cout << a << b << c << endl;
if (a == 'T')
{
a = '1';
}
else
a = '0';
if (b == 'T')
{
b = '1';
}
else
b = '0';
if (c == 'T')
{
c = '1';
}
else
c = '0';
change(str);
//cout << str << endl;
char ans[1000];
fun(str, ans);
//cout << ans << endl;
if (Panduan(ans))
{
cout << "TRUE" << endl;
}
else
{
cout << "FALSE" << endl;
}
}
return 0;
}