主析取范式

#include<bits/stdc++.h>
using namespace std;
stack<char>optr;
stack<char>opnd;
int cmp[10][10]
{
	{' ','!','!','!','!','<','!','!'},//!
	{'<','>','>','>','>','<','>','>'},//*
	{'<','<','>','>','>','<','>','>'},//|
	{'<','<','<','>','>','<','>','>'},//-
	{'<','<','<','<','>','<','>','>'},//=
	{'<','<','<','<','<','<','=',' '},//(
	{'>','>','>','>','>',' ','>','>'},//)
	{'<','<','<','<','<','<',' ',  '='},//#
};

int in(char c)
{
	int ans;
	switch(c)
	{
		case '!':
			ans=0;
			break;
		case '*':
			ans=1;
			break;
		case '|':
			ans=2;
			break;
		case '-':
			ans=3;
			break;
		case '=':
			ans=4;
			break;
		case '(':
			ans=5;
			break;
		case ')':
			ans=6;
			break;
		case '#':
			ans=7;
			break;
		default:
			ans=8;
	}
	return ans;
}

char precede(char a,char b)
{
	int v1=in(a),v2=in(b);
	return cmp[v1][v2];
}

char operate(char a,char t,char b)
{
	char ans;
	switch(t)
	{
		case '*':
			if(a=='0'||b=='0') ans='0';
			else ans='1';
			break;
		case '|':
			if(a=='0'&&b=='0') ans='0';
			else ans='1';
			break;
		case '-':
			if(a=='1'&&b=='0') ans='0';
			else ans='1';
			break;
		case '=':
			if(a==b) ans='1';
			else ans='0';
	}
	return ans;
}

int give(string s)
{
	optr.push('#');
	char theta,a,b,e;
	for(int i=0; i<s.size(); i++)
	{
		if(in(s[i])==8)
		{
			opnd.push(s[i]);
		}
		else
		{
			e=precede(optr.top(),s[i]);
			if(e=='<')
			{
				optr.push(s[i]);
			}
			else if(e=='=')
			{
				optr.pop();
			}
			else if(e=='>')
			{
				theta=optr.top();
				optr.pop();
				b=opnd.top();
				opnd.pop();
				a=opnd.top();
				opnd.pop();
				opnd.push(operate(a,theta,b));
				optr.push(s[i]);
			}
			else if(e=='!')
			{
				optr.pop();
				a=opnd.top();
				opnd.pop();
				if(a-48) opnd.push('0');
				else opnd.push('1');
				optr.push(s[i]);
			}
		}
	}
	theta=optr.top();
	optr.pop();
	b=opnd.top();
	opnd.pop();
	a=opnd.top();
	opnd.pop();
	return operate(a,theta,b);
}

struct node
{
	char ala;
	int num;
};

int main()
{
	int k=0,t=0,a;
	string s,ss;
	cin>>s;
	ss=s;
	node al[55];
	for(int i=0; i<s.size(); i++)
	{
		if(isalpha(s[i]))
		{
			for(int j=0; j<k; j++)
			{
				if(al[j].ala==s[i]) t=1;
			}
			if(!t)
			{
				al[k++].ala=s[i];
				t=0;
			}
		}
	}
	for(int i=0; i<pow(2,k); i++)
	{
		a=i;
		s=ss;
		for(int j=k-1; j>=0; j--)
		{
			al[j].num=a%2;
			a/=2;
			for(int m=0; m<s.size(); m++)
			{
				if(s[m]==al[j].ala)
				{
					if(al[j].num==0) s[m]='0';
					else s[m]='1';
				}
			}
		}
		if(give(s)-48) cout<<i<<" ";
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值