输出命题公式真值表

描述
先输入一个正整数 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;
}

 

  • 7
    点赞
  • 58
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值