描述
先输入一个正整数 n(n 小于等于 10),表示共有 n 个命题变元,再输入一个类
似于逆波兰表达式的字符串表示一个命题公式,约定在该字符串中用一位的十进
制数表示一个命题变元,用 a、o、n、i、e 分别表示且、或、非、蕴含、等值,
用类似于逆波兰表达式形式的字符串表示的命题公式的真值表波兰表达式(即二
元运算,两个操作数在前,运算符在后;一元运算,一个操作数在前,运算符在
后)。
输入
先输入一个小于等于 10 的正整数 n,再输入一个字符串。
输出
输出该字符串表示的命题公式的真值表。
提示:
如果用 P、Q、R 分别表示这三个命题变元的话,
输入数据 01a2i 表示的命题公式是:((P∧Q)→R)
输入数据 012ia 表示的命题公式是:(P∧(Q→R))
输入数据 0n 表示的命题公式是:┐P
输入样例
3
01a2i
输出样例
0 0 0 1
0 0 1 1
0 1 0 1
0 1 1 1
1 0 0 1
1 0 1 1
1 1 0 0
1 1 1 1
#include <stdio.h>
#include <stack>
#include <string>
#include <iostream>
using namespace std;
string str;
stack<int> s;
int b[100];
int n;
void solve()
{
while(!s.empty())
{
s.pop();
}
int temp1, temp2, temp;
int k = 0;
for(int i = 0; i < str.length(); i++)
{
if(str[i] >= '0' && str[i] <= '9')
{
s.push(b[k]);
k++;
}
else if(str[i] == 'a')
{
temp2 = s.top();
s.pop();
temp1 = s.top();
s.pop();
if(temp1 && temp2)
{
s.push(1);
}
else
{
s.push(0);
}
/*temp = temp1 && temp2;
s.push(temp);*/
}
else if(str[i] == 'o')
{
temp2 = s.top();
s.pop();
temp1 = s.top();
s.pop();
if(temp1 == 0 && temp2 == 0)
{
s.push(0);
}
else
{
s.push(0);
}
}
else if(str[i] == 'n')
{
temp = !s.top();
s.pop();
if(temp)
{
s.push(1);
}
else
{
s.push(0);
}
}
else if(str[i] == 'i')
{
temp2 = s.top();
s.pop();
temp1 = s.top();
s.pop();
if(temp1 == 1 && temp2 == 0)
{
s.push(0);
}
else
{
s.push(1);
}
}
else if(str[i] == 'e')
{
temp2 = s.top();
s.pop();
temp1 = s.top();
s.pop();
if(temp1 == temp2)
{
s.push(1);
}
else
{
s.push(0);
}
}
}
temp = s.top();
s.pop();
printf("%d\n", temp);
}
void DFS(int step)
{
if(step == n)
{
for(int i = 0; i < n; i++)
{
printf("%d", b[i]);
}
solve();
return;
}
for(int i = 0; i < 2; i++)
{
b[step] = i;
DFS(step + 1);
}
}
int main(void)
{
scanf("%d", &n);
getchar();
getline(cin, str);
DFS(0);
return 0;
}