C语言---给出含有n个元素的集合的两个子集A和B,利用位串求出:Ã,A∩B,A∪B,A-B,A⊕B

设计以下问题的算法:

给出含有n个元素的集合的两个子集A和B,利用位串求出:Ã,A∩B,A∪B,A-B,A⊕B

任务难度:

真值表:

 代码如下:

#include<stdio.h>
#include<stdlib.h>
typedef struct list{//建立结构体 
	int Data;
	int n;
	struct list* Next; 
	}List;
void Show(); //展示 
List* Input(int m);//输入 
int NotA(list *Ahead); 
int Intersection(List *Ahead,List *Bhead);//交集 
int Union(List *Ahead,List *Bhead);//并集 
int Difference(List *Ahead,List *Bhead);//差集 	
int Xor(List *Ahead,List *Bhead);//异或 

int main(){
	List *Ahead,*Bhead;
	Show();
	int n,m;
//	printf("请输入命令:");
	while(1){
		scanf("%d",&n);//条件语句 
	switch(n){
		case 1:Ahead=Input(1);Bhead=Input(2);break;
		case 2:NotA(Ahead);break;
		case 3:Intersection(Ahead,Bhead);break;
		case 4:Union(Ahead,Bhead);break;
		case 5:Difference(Ahead,Bhead);break;
		case 6:Xor(Ahead,Bhead); 
		case 7:NotA(Ahead);Intersection(Ahead,Bhead);Union(Ahead,Bhead);Difference(Ahead,Bhead);Xor(Ahead,Bhead);break;
		case 0:printf("                                                        感谢使用\n");return 0;
	}
	Show();
	}	

}
//展示函数 
void Show(){ 
	printf("                                             ****************************\n");
	printf("                                             ***  1.  输入集合A与B    ***\n");
	printf("                                             ***  2.    A  ∩  B      ***\n");
	printf("                                             ***  3.      非A         ***\n");
	printf("                                             ***  4.    A  ∪  B      ***\n");
	printf("                                             ***  5.    A  --  B      ***\n");
	printf("                                             ***  6.    A  ⊕  B      ***\n");
	printf("                                             ***  7.    全部展示      ***\n");
	printf("                                             ***  0.   退      出     ***\n");
	printf("                                             ****************************\n");
	printf("请输入命令:");
}
List* Input(int m){
	List *head;
	List *r;
	List *p;
	head= (List *)malloc(sizeof(List));
	head->Next=NULL;
	head->Data=1;
	head->n=0;
	r=head;

	for(int H=2;H<=10;H++){
	p= (List *)malloc(sizeof(List));
	p->Data=H;
	p->n=0;
	r->Next=p;
	r=p;
	}
	p=head;
	int a,b;
	int j,k;
	if(m==1){
		printf("输入A集合元素个数:");
	scanf("%d",&a);
	printf("输入A集合元素(1-10): ");
		for(int i=1;i<=a;i++){
		scanf("%d",&j);
		for(int H=1;H<=10;H++){
			if(j==p->Data){
				p->n=1; 
				break;
			}
			p=p->Next;
		}
		
	}	
	p=head;
		printf("A集合位串为:");
		for(int i=0;i<10;i++){
	//printf("%d ",p1->Data);
	printf("%d ",p->n);
		p=p->Next;	
	}
	}
	if(m==2){
		printf("输入B集合元素个数:");
	scanf("%d",&b);
	printf("输入B集合元素(1-10): ");
			for(int i=1;i<=b;i++){
		scanf("%d",&k);
		for(int H=1;H<=10;H++){
			if(k==p->Data){
				p->n=1; 
				break;
			}
			p=p->Next;
		}
	}	
	p=head;
	printf("B集合位串为:"); 
		for(int i=0;i<10;i++){
	//printf("%d ",p2->Data);
	printf("%d ",p->n);
		p=p->Next;	
	}
	}
	printf("\n");
	return head;
	 
}
int NotA(list *Ahead){
	List *p1;
	p1=Ahead;
	printf("非 A 为   :");
	for(int i=1;i<=10;i++){
		if(p1->n==1){
			printf("0 "); 
		}else{
			printf("1 ");
		}
		p1=p1->Next;
	}
	printf("\n\n\n");
}
int Intersection(List *Ahead,List *Bhead){//交集 
	List *p1,*p2; 
	p1=Ahead;
	p2=Bhead;
	printf("A  ∩  B为:");
	for(int i=1;i<=10;i++){
		if(p1->n*p2->n==1){
			printf("1 ");
		}else{
			printf("0 ");
		}
		p1=p1->Next;
		p2=p2->Next;
	}
	printf("\n\n\n\n"); 
}
int Union(List *Ahead,List *Bhead){//并集 
	List *p1,*p2; 
	p1=Ahead;
	p2=Bhead;
	printf("A  --  B为:");
	for(int i=1;i<=10;i++){
		if(p1->n+p2->n>=1){
			printf("1 ");
		}else{
			printf("0 ");
		}
		p1=p1->Next;
		p2=p2->Next;
	}
	printf("\n\n\n\n");
}
int Difference(List *Ahead,List *Bhead){//差集 
	List *p1,*p2; 
	p1=Ahead;
	p2=Bhead;
	printf("A  ∩  B为:");
	for(int i=1;i<=10;i++){
		if(p1->n-p2->n==1){
			printf("1 ");
		}else{
			printf("0 ");
		}
		p1=p1->Next;
		p2=p2->Next;
	}
	printf("\n\n\n\n");
}
int Xor(List *Ahead,List *Bhead){//异或 
	List *p1,*p2; 
	p1=Ahead;
	p2=Bhead;
	printf("A  ⊕  B为:");
	for(int i=1;i<=10;i++){
		if(p1->n+p2->n==1){
			printf("1 ");
		}else{
			printf("0 ");
		}
		p1=p1->Next;
		p2=p2->Next;
	}
	printf("\n\n\n\n");
}

小白写博客,有问题请各位大佬指正,谢谢!

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值