并交和差运算

巡山小妖精
531次浏览
2021年02月07日 04:56
最佳经验
本文由作者推荐

欧式家具品牌排行榜-

2021年2月7日发(作者:commissioned)


编制一个能演示执行集合的并、


交和差运算的程序。

集合的元素限定为小写字母


字符


[



a



..



z



]


,演示 程序以用户和计算机的对话方式执行,如:输入集合


1


数据“< /p>


book


”和集合


2

数据“


bag



,则输出两个集合 的并集是“


abgko



,两个


集合的交集是“


b



,两个集合的差集是“


ko












#include


注意把


< p>
文件放入


c


盘根目录下



#include


#include


void UnionList(SqList *LA,SqList *LB,SqList *&LC)//


并集



{


int i=0,j=0,k=0; //i



j



k


分别作为


LA



LB



LC


的下标



LC=(SqList *)malloc(sizeof(SqList));


LC->length=0;


while (ilength && jlength)


{


if (LA->data[i]data[j])


{


LC->data[k]=LA->data[i];


i++;k++;


}


else if(LA->data[i]==LB->data[j])


{


LC->data[k]=LA->data[i];


i++;k++;j++;


}


else if(LA->data[i]>LB->data[j])


{


LC->data[k]=LB->data[j];


k++;j++;


}


}


while (ilength) //LA


尚未扫 描完


,


将其余元素插入


LC

< p>



{


LC->data[k]=LA->data[i];


i++;k++;


}


while (jlength) //LB


尚未扫描完


,


将其余元素插入


LC




{


LC->data[k]=LB->data[j];


j++;k++;


}


LC->length=k;


}


void Commnode(SqList *LA,SqList *LB,SqList *&LC)//


交集



{


int i=0,j=0,k=0;


LC=(SqList *)malloc(sizeof(SqList));


LC->length=0;


while(ilength&&jlength)


{


if(LA->data[i]data[j])


{


i++;


}


else if(LA->data[i]==LB->data[j])


{


LC->data[k]=LA->data[i];


i++;j++;k++;


}


else if(LA->data[i]>LB->data[j])


{


j++;


}


}


LC->length=k;


}


void Subtraction(SqList *LA,SqList *LB,SqList *&LC)//


差集



{


int i=0,j=0,k=0;


LC=(SqList *)malloc(sizeof(SqList));


LC->length=0;


while(ilength&&jlength)


{


if(LA->data[i]data[j])


{


LC->data[k]=LA->data[i];


i++;k++;


}


else if(LA->data[i]==LB->data[j])


{



i++;j++;


}


else if(LA->data[i]>LB->data[j])


{


LC->data[k]=LB->data[j];


j++; k++;


}


}


while(ilength)


{


LC->data[k]=LA->data[i];


i++;k++;


}


while(jlength)


{


LC->data[k]=LB->data[j];


j++;k++;


}


LC->length=k;


}


void main()


{ char a[10],b[10];


SqList *L1,*L2,*L3;


int a1,b1;


InitList(L1);


InitList(L2);


InitList(L3);


int A=0;


do{


cou t<<


请输入集合


A:


cin>>a;


a1=strlen(a);


for(int i=0;i


ListInsert(L1,a[i]);


if(a1>ListLength(L1))


{


cout<<


注意


!


元素有重复



DestroyList(L1);In itList(L1);


}


else if(a1==ListLength(L1))A=1;


}while(A==0);


int B=0;



do{


cout<<

< p>
请输入集合


B:


cin>>b;


b1=strlen(b);


for(int j=0;j


ListInsert(L2,b[j]);


if(b1>ListLength(L2))


{


cout<<


注意


!


元素有重复



DestroyList(L2);In itList(L2);


}


else if(b1==ListLength(L2)) B=1;


}while(B==0);



co ut<<


集合


A


的长度


:


cout<<


集合


B


的长度


:




cout<<


集合


A


在顺序表中的结构


:


DispList(L1);

< p>
cout<<


集合


B


在顺 序表中的结构


:


DispList(L2);



cout<<


并集

< br>:


UnionList(L1,L2,L3);


DispList(L3);


cout<<

< br>交集


:


Commnode(L1,L2,L3);


DispList(L3);


cout<<

< br>差集


:


Subtraction(L1,L2,L3);


DispList(L3);


}




//


文件



#include


#include


#define MaxSize 50


typedef char ElemType;


typedef struct


{ ElemType data[MaxSize]; //


存放顺序表元素



int length; //


存放顺序表的长度



} SqList; //


顺序表的类型定义





void CreateList(SqList *&L,ElemType a[],int n) //


建立顺序表



{


int i;


L=(SqList *)malloc(sizeof(SqList));


for (i=0;i


L->data[i]=a[i];


L->length=n;


}




void InitList(SqList *&L)


{


L=(SqList *)malloc(sizeof(SqList)); //


分配存放线性表的空间



L->length=0;


}


void DestroyList(SqList *&L)


{


free(L);


}


int ListEmpty(SqList *L)


{


return(L->length==0);


}


int ListLength(SqList *L)


{


return(L->length);


}


void DispList(SqList *L)


{


int i;


if (ListEmpty(L)) return;


for (i=0;ilength;i++)


printf(


printf(


}


int GetElem(SqList *L,int i,ElemType &e)


{


if (i<1 || i>L->length)


return 0;


e=L->data[i-1];


return 1;


}


int LocateElem(SqList *L, ElemType e)


{

欧式家具品牌排行榜-


欧式家具品牌排行榜-


欧式家具品牌排行榜-


欧式家具品牌排行榜-


欧式家具品牌排行榜-


欧式家具品牌排行榜-


欧式家具品牌排行榜-


欧式家具品牌排行榜-