南邮离散数学实验 利用真值表法求取主析取范式以及主合取范式的实现

一、    实验目的和要求

内容:

编程实现用真值表法求取任意数量变量的合式公式的主析取范式和主合取范式。

要求:

能够列出任意合式公式的真值表并给出相应主析取和主合取范式。


内容:

编程实现用真值表法求取任意数量变量的合式公式的主析取范式和主合取范式。

原理:

先将中缀表达式转换成后缀表达式,再将后缀表达式中每一个字母变量一一赋值,用递归枚举的方法枚举所有赋值情况,并且用map映射将每一个字母变量与当前被枚举的值一一映射,对每一种赋值情况调用后缀表达式计算函数计算后缀表达式的值,打印真假情况。如果是真,记录到名为zhen的vector不定长数组中,如果是假,记录到名为jia的vector不定长数组中。最后根据zhen和jia的不定长数组来打印主析取范式和主合取范式。 

此程序可以实现任意数量的字母变量的主析取范式求取和主合取范式求取,以及真值表打印。      



//或运算 |  , 与运算 &   ,单条件 ->  ,双条件 <=> ,非运算 !

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
string zhong;  //中缀表达式
char hou[1000];   //后缀表达式
string alpha;   //存放所有字母变量
map M;   //映射,将字母变量与0或1一一对应

struct note
{
	int a[100];
};
vector zhen;  //不定长数组,存放主析取范式对应字母变量的01情况,也就是表达式真值为T
vector jia;  //不定长数组,存放主合取范式对应字母变量的01情况,也就是表达式真值是F

void ddd()   //预处理,去除中缀表达式中条件->中的>,和双条件<=>中的= and > ,将这两个运算符当成一个字符处理,更方便
{
	string::iterator i=zhong.begin();
	int flag=1;
	while(flag)
    {
        flag=0;
        for(i=zhong.begin();i!=zhong.end();++i)
        {
            if(*i=='>')
            {
                zhong.erase(i);
                flag=1;
                break;
            }
            if(*i=='=')
            {
                zhong.erase(i);
                flag=1;
                break;
            }
        }
	}
}

int icp(char a)
{
	if(a=='#') return 0;
	if(a=='(') return 12;
	if(a=='!') return 10;
	if(a=='&') return 8;
	if(a=='|') return 6;
	if(a=='-') return 4;
	if(a=='<') return 2;
	if(a==')') return 1;
}
int isp(char a)
{
	if(a=='#') return 0;
	if(a=='(') return 1;
	if(a=='!') return 11;
	if(a=='&') return 9;
	if(a=='|') return 7;
	if(a=='-') return 5;
	if(a=='<') return 3;
	if(a==')') return 12;
}


void change()    //中缀表达式转换后缀表达式
{
	int j=0;
	stack s;
	char ch,y;
	s.push('#');
	char t1,t2;
	stringstream ss(zhong);
	while(ss>>ch,ch!='#')
	{
		if(isalpha(ch))
		{
			hou[j++]=ch;
			if(alpha.find(ch)==-1)
			{
				alpha.push_back(ch);
			}
		}
		else if(ch==')')
		{
			for(y=s.top(),s.pop();y!='(';y=s.top(),s.pop())
			{
				hou[j++]=y;
			}
		}
		else
		{
			for(y=s.top(),s.pop();icp(ch)<=isp(y);y=s.top(),s.pop())
			{
				hou[j++]=y;
			}
			s.push(y);
			s.push(ch);
		}
	}
	while(!s.empty())
	{
		y=s.top();
		s.pop();
		if(y!='#')
		{
			hou[j++]=y;
		}
	}
	hou[j]='#';
}


int cal()   //对赋值后的后缀表达式进行计算
{
	stack s;
	char ch;
	int j=0;
	int t1,t2;
	while(1)
	{
		ch=hou[j];
		if(ch=='#') break;
		if(ch==0) break;
		j++;
		if((ch>='A'&&ch<='Z')||(ch>='a'&&ch<='z'))
		{
			s.push(M[ch]);
		}
		else
		{
			if(ch=='!')
			{
				t1=s.top();
				s.pop();
				s.push(!t1);
			}
			else if(ch=='&')
			{
				t1=s.top();
				s.pop();
				t2=s.top();
				s.pop();
				if(t1==1&&t2==1)
				{
					s.push(1);
				}
				else
				{
					s.push(0);
				}
			}
			else if(ch=='|')
			{
				t1=s.top();
				s.pop();
				t2=s.top();
				s.pop();
				if(t1==0&&t2==0)
				{
					s.push(0);
				}
				else
				{
					s.push(1);
				}
			}
			else if(ch=='-')
			{
				t1=s.top();
				s.pop();
				t2=s.top();
				s.pop();
				if(t1==0&&t2==1)
				{
					s.push(0);
				}
				else
				{
					s.push(1);
				}
			}
			else if(ch=='<')
			{
				t1=s.top();
				s.pop();
				t2=s.top();
				s.pop();
				if((t1==1&&t2==1)||(t1==0&&t2==0))
				{
					s.push(1);
				}
				else
				{
					s.push(0);
				}
			}
		}
	}
	int ans=s.top();
	return ans;
}









void dfs(int cur)   //递归枚举每一种字符变量的取值情况
{
	if(cur==alpha.size())
	{
		int ans=cal();
		for(int i=0;i  ,双条件我 <=> ,非运算为 !\n");
		printf("请输入表达式,回车结束\n");
	 	cin>>zhong;
	 	zhong.append("#");
	 	ddd();
	 	change();
	 	for(i=0;i
南邮离散数学实验 利用真值表法求取主析取范式以及主合取范式的实现_第1张图片


南邮离散数学实验 利用真值表法求取主析取范式以及主合取范式的实现_第2张图片

南邮离散数学实验 利用真值表法求取主析取范式以及主合取范式的实现_第3张图片

你可能感兴趣的:(作业)