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

实验原理及内容

说明:这部分内容主要包括:

1、形式化描述实验中所使用的数据结构和存储结构,给出函数之间的调用关系和数据传递方式;

2、给出核心算法的C++或Java等语言的源代码,并加上详细注释,分析算法的时间复杂度;

该实验先将中缀表达式转换成后缀表达式,

例如:stack 栈用来存放运算符,post 栈用来存放最后的后缀表达式。具体规则如下:

从左到右扫描中缀表达式,若是操作数,直接存入 post 栈;若是运算符:
(1)该运算符是左括号(, 则直接存入 stack 栈。
(2)该运算符是右括号),则将 stack 栈中( 前的所有运算符出栈,存入 post 栈。
(3)若该运算符为非括号,则将该运算符和 stack 栈顶运算符作比较:若高于栈顶运算符,则直接存入 stack 栈,否则将栈顶运算符出栈(从栈中弹出元素直到遇到发现更低优先级的元素(或者栈为空)为止),存入 post 栈。
(4)当扫描完后,stack 栈中还有运算符时,则将所有运算符出栈,存入 post 栈。

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

#include <cstdio>
#include <iostream>
#include <vector>//vector是顺序容器
#include <string>
#include <cstdlib>
#include <queue>//queue 模板类也需要两个模板参数,一个是元素类型,一个容器类型,元素类型是必要的,容器类型是可选的,默认为deque 类型
#include <stack>
#include <map>
#include <sstream>
using namespace std;
string middle;  //中缀表达式
char latter[1000];   //后缀表达式
string alpha;   //存放所有字母变量
map<char,int> M;   //映射,将字母变量与0或1一一对应
 
struct note
{
	int a[100];
};
vector<note> tr;  //不定长数组,存放主析取范式对应字母变量的01情况,也就是表达式真值为T
vector<note> flase;  //不定长数组,存放主合取范式对应字母变量的01情况,也就是表达式真值是F
 
void ddd()   //预处理,去除中缀表达式中条件->中的>,和双条件<=>中的= and > ,将这两个运算符当成一个字符处理,更方便
{
	string::iterator i=middle.begin();
	int flag=1;
	while(flag)
    {
        flag=0;
        for(i=middle.begin();i!=middle.end();++i)
        {
            if(*i=='>')
            {
                middle.erase(i);
                flag=1;
                break;
            }
            if(*i=='=')
            {
                middle.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<char> s;
	char ch,y;
	s.push('#');
	stringstream ss(middle);
	while(ss>>ch,ch!='#')
	{
		if(isalpha(ch))
		{
			latter[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())
			{
				latter[j++]=y;
			}
		}
		else
		{
			for(y=s.top(),s.pop();icp(ch)<=isp(y);y=s.top(),s.pop())
			{
				latter[j++]=y;
			}
			s.push(y);
			s.push(ch);
		}
	}
	while(!s.empty())
	{
		y=s.top();
		s.pop();
		if(y!='#')
		{
			latter[j++]=y;
		}
	}
	latter[j]='#';
}
 
 int cal()   //对赋值后的后缀表达式进行计算
{
	stack<int> s;
	char ch;
	int j=0;
	int t1,t2;
	while(1)
	{
		ch=latter[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<alpha.size();i++)
		{
			if(M[alpha[i]])
			{
				cout<<"T\t";
			}
			else
			{
				cout<<"F\t";
			}
		}
		if(ans==1)   //真值为T 计入到tr数组,以待后面主析取范式使用
		{
			cout<<"T\n";
			note t;
			for(int i=0;i<alpha.size();i++)
			{
				t.a[i]=M[alpha[i]];
			}
			tr.push_back(t);
		}
		else   //真值为F  计入到flase数组,以待后面主合取范式使用
		{
			cout<<"F\n";
			note t;
			for(int i=0;i<alpha.size();i++)
			{
				t.a[i]=M[alpha[i]];
			}
			flase.push_back(t);
		}
		return ;
	}
	M[alpha[cur]]=1;
	dfs(cur+1);
	M[alpha[cur]]=0;
	dfs(cur+1);
}
int main()
{
	while(true)
	{
		int i;
		M.clear();
		alpha.clear();
		tr.clear();
		flase.clear();
		printf("或运算为 |  , 与运算为 &   ,单条件为 ->  ,双条件我 <=> ,非运算为 !\n");
		printf("请输入表达式,回车结束\n");
	 	cin>>middle;
	 	middle.append("#");
	 	ddd();
	 	change();
	 	for(i=0;i<alpha.size();i++)
	 	{
	 		printf("%c\t",alpha[i]);
	 	}
		printf("表达式真值\n");
	 	dfs(0);
	 	printf("主析取范式为\n");
		int lena=tr.size();
		for(i=0;i<lena;i++)
		{
			if(i!=0) printf("∨");
			int *p=tr[i].a;
			printf("(");
			for(int j=0;j<alpha.size();j++)
			{
				if(j!=0) printf("∧");
				if(p[j]==1)
				{
					printf("%c",alpha[j]);
				}
				else
				{
					printf("¬%c",alpha[j]);
				}
			}
			printf(")");
		}
		printf("\n");
		printf("主合取范式为\n");
		for(i=0;i<flase.size();i++)
		{
			if(i!=0) printf("∧");
			int *p=flase[i].a;
			printf("(");
			for(int j=0;j<alpha.size();j++)
			{
				if(j!=0) printf("∨");
				if(p[j]==0)
				{
					printf("%c",alpha[j]);
				}
				else
				{
					printf("¬%c",alpha[j]);
				}
			}
			printf(")");
		}
		printf("\n\n");
	 }
	return 0;
}

  • 7
    点赞
  • 35
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
南京邮电大学数据结构课程中,顺序表是一个非常基础的数据结构,其实现要包括以下几个方面: 1. 顺序表的定义:顺序表是一种线性表,其特点是元素在物理空间上连续存储,逻辑上也是连续的。顺序表的定义可以使用数组来实现。 2. 顺序表的基本操作:包括初始化、插入、删除、查找、修改等操作。其中,插入和删除操作需要考虑到元素的移动问题。 3. 顺序表的组合应用:顺序表可以用来实现其他数据结构,例如栈、队列等。 下面是一个简单的Python代码示例,演示了如何实现顺序表的基本操作: ```python class SeqList: def __init__(self, maxsize=None): self.maxsize = maxsize self.array = [None] * self.maxsize self.length = 0 def __len__(self): return self.length def __getitem__(self, index): if index < self.length: return self.array[index] else: raise IndexError('Index out of range') def __setitem__(self, index, value): if index < self.length: self.array[index] = value else: raise IndexError('Index out of range') def insert(self, index, value): if self.length >= self.maxsize: raise Exception('SeqList is full') if index < 0 or index > self.length: raise IndexError('Index out of range') for i in range(self.length, index, -1): self.array[i] = self.array[i-1] self.array[index] = value self.length += 1 def delete(self, index): if index < 0 or index >= self.length: raise IndexError('Index out of range') for i in range(index, self.length-1): self.array[i] = self.array[i+1] self.length -= 1 ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

cookie爱吃小饼干

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值