C語言實現兩個集合的並、交、差、補運算

7/17日 第一題

題目:C語言實現兩個集合的並、交、差、補運算

環境:DEV C++

結果圖如下:

完整代碼如下:

#include<stdio.h>  
#include<stdlib.h>  
#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;  
}  

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章