C语言实现两个集合的并、交、差、补运算

7/17日 第一题

题目:C语言实现两个集合的并、交、差、补运算

环境:DEV C++

结果图如下:

C语言实现两个集合的并、交、差、补运算_第1张图片

完整代码如下:

#include  
#include  
#define TRUE 1  
#define FALSE 0  
#define OK 1  
#define ERROR 0  
#define OVERFLOW -1  
#define LISTSIZE 100 //初始表空间大小  
#define addlength 20 //表长增量  
typedef int Status; 
typedef char ElemType;  
typedef struct{  
ElemType *elem;  
int length;  
int listsize; 
}SqList;  
   
SqList La,Lb,Lc,Ld;
    
Status InitList_Sq(SqList &L){  
	L.elem = (ElemType *)malloc(LISTSIZE * sizeof(ElemType));  
	if(!L.elem) exit(-1);   
	L.length = 0;   
	L.listsize = LISTSIZE; 
	return OK;  
}   
Status ListInsert_Sq(SqList &L,int i,ElemType e){  
	ElemType *newbase,*p,*q;  
	if(i < 1 || i > L.length + 1) 
		return ERROR;  
	if(L.length >= L.listsize){ 
		newbase = (ElemType *)realloc(L.elem,(L.listsize +addlength) * sizeof(ElemType));  
	if(!newbase) exit(-1);   
	L.elem = newbase; 
	L.listsize +=addlength; 
	}  
	q = &(L.elem[i - 1]); //q为插入位置  
	for(p = &(L.elem[L.length - 1]); p >= q; --p)   
	*(p + 1) = *p; //插入位置及之后的元素往右移  
	*q = e; //插入e  
	L.length++; //表长加1  
	return OK;  
}  
void CreateList_Sq(SqList &L){  
	ElemType ch;  
	int n=0,j;  
	while((ch) != '\n'){  
		scanf("%c",&ch);  
		for(j = 0; j < L.length; j++)  
		if(ch == L.elem[j]){  
			n=1;  
			break;  
		}  
		else  
			n=0; 
			if(!n && ch != '\n') 
			ListInsert_Sq(L,L.length+1,ch);  
	}  
}    
/**判断两元素是否相等,若相等则返回TRUE;否则返回FALSE**/  
Status Equal(ElemType a,ElemType b){  
	if(a == b) 
	return 1;  
	else 
	return 0;  
}  
   
int LocateElem_Sq(SqList L,ElemType e,Status(* compare)(ElemType,ElemType)){  
	ElemType *p;  
	int i;  
	i = 1; //i的初值为第1个元素的位序  
	p = L.elem; //p的初值为第1个元素的储存位置  
	while(i <= L.length && !(* compare)(*p++,e)) ++i;  
	if(i <= L.length) return i;  
	else return 0;  
} //该函数的时间复杂度为O(n)  
     
/**打印顺序表函数**/  
void Print_Sq(SqList L){  
	int i;  
	for(i = 0; i < L.length; i++)  
	{
	if(L.elem[i]>='a'&&L.elem[i]<='z')
		{
			printf("%2c",L.elem[i]);  
		}
	}
	if(L.length == 0)
		printf("该集合为空集");   
	
}  
   
/**求集合的并集的函数**/  
void bing(SqList La,SqList Lb,SqList &Lc){  
	int i;  
	ElemType elem;  
	Lc.length=0;  
	for(i = 0; i < La.length; i++)  
	Lc.elem[Lc.length++]=La.elem[i];  
	for(i = 1; i <= Lb.length; i++){  
		elem = Lb.elem[i-1];  
		if(!LocateElem_Sq(La,elem,Equal))  
		ListInsert_Sq(Lc,Lc.length+1,elem);  
   }  
}  
   
/**求集合的交集的函数**/  
void jiao(SqList La,SqList Lb,SqList &Lc){  
	int i;  
	ElemType elem;  
	Lc.length = 0;  
	for(i = 1; i <= La.length; i++){  
		elem = La.elem[i-1];  
		if(LocateElem_Sq(Lb,elem,Equal))  
		ListInsert_Sq(Lc,Lc.length+1,elem);  
   }  
}  
   
/**求集合的差集函数**/  
void cha(SqList La,SqList Lb,SqList &Lc){  
	int i;  
	ElemType elem;  
	Lc.length = 0;  
	for(i = 1; i <= La.length; i++){  
		elem = La.elem[i-1];  
		if(!LocateElem_Sq(Lb,elem,Equal))  
		ListInsert_Sq(Lc,Lc.length+1,elem);  
   }  
}  
   
/**求集合的补集函数**/  
void buji(SqList La,SqList Lb,SqList &Lc,SqList &Ld){  
	int i;  
	ElemType elem;  
	Ld.length = 0;  
	bing(La,Lb,Lc);  
	for(i = 1; i <= Lc.length; i++)
	{  
		elem = Lc.elem[i-1];  
		if(!LocateElem_Sq(La,elem,Equal))  
		ListInsert_Sq(Ld,Ld.length+1,elem);  
	}  
}  
   
void select(){  
	char s;  
	int l=1;  
	InitList_Sq(La);  
	printf("****** 请输入你的第一个集合:******\n");  
 
	CreateList_Sq(La);  
	printf("集合A为:");  
	Print_Sq(La); //实现表LA的操作  
   	printf("\n");
	InitList_Sq(Lb);  
	printf("****** 请输入你的第二个集合:******\n");  
	CreateList_Sq(Lb);  
	printf("集合B为:");  
	Print_Sq(Lb); //实现表LB的操作  
	printf("\n\n");  
   
	InitList_Sq(Lc); //初始化表LC的操作  
	InitList_Sq(Ld); //初始化表Ld的操作  
	while(l){  
	printf("******* 您可以选择a、b、c或者d执行以下操作 ******\n\n");  
	printf("************* a、进行集合的并运算 ***************\n");  
	printf("************* b、进行集合的交运算 ***************\n");  
	printf("************* c、进行集合的差运算 ***************\n");  
	printf("************* d、进行集合的补运算 ***************\n");  
	printf("************* 0、  退 出   程 序  ***************\n");  
	 
	scanf("%c",&s);  
	switch(s){  
		case 'a':bing(La,Lb,Lc);
		printf("集合A与集合B的并集为:");  
		Print_Sq(Lc); //实现表LA与表LB并集的操作  
		printf("\n");  
		break;  
		case 'b' : jiao(La,Lb,Lc);  
		printf("集合A与集合B的交集为:");  
		Print_Sq(Lc); //实现表LA与表LB交集的操作  
		printf("\n");  
		break;  
		case 'c' : cha(La,Lb,Lc);  
		printf("集合A与集合B的差集为:");  
		Print_Sq(Lc); //实现表LA与表LB差集的操作  
		printf("\n");  
		break;  
		case 'd' : buji(La,Lb,Lc,Ld);  
		printf("集合A的补集为:");  
		Print_Sq(Ld); //实现表LA的补集操作  
		printf("\n");  
		break;  
		case 'e' : exit(0);   
		break;  
		default  : printf("tenter data error!\n");  
		printf("\n");  
		}  
		printf("**** 继续执行请输入1,否则请输入0 ****\n");   
		scanf("%d",&l);  
		getchar();  
	}
}

int main()
{  
	select();  
	return 0;  
}  

 

 

你可能感兴趣的:(C语言)