#include<stdio.h>
#include<iostream>
#include<stdlib.h>
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define OVERFLOW -1
#define LIST_INIT_SIZE 100 //初始表空间大小
#define LISTINCREMENT 10 //表长增量
typedef int Status;
typedef char ElemType;
using namespace std;
typedef struct{
ElemType *elem;
int length;
int listsize;
}SqList;
SqList La,Lb,Lc,Ld;
/**构造一个空的线性表L**/
Status InitList_Sq(SqList &L)
{
//L.elem = new ElemType;
L.elem = (ElemType *)malloc(LIST_INIT_SIZE * sizeof(ElemType));
if(!L.elem) exit(OVERFLOW);
L.length = 0;
L.listsize = LIST_INIT_SIZE;
return OK;
}
/*插入新元素e*/
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 + LISTINCREMENT) * sizeof(ElemType));
if(!newbase) exit(OVERFLOW);
L.elem = newbase;
L.listsize += LISTINCREMENT;
}
q = &(L.elem[i - 1]);
for(p = &(L.elem[L.length - 1]); p >= q; --p)
*(p + 1) = *p;
*q = e;
++L.length;
return OK;
}
/*创建并输入*/
void CreateList_Sq(SqList &L)
{
ElemType ch;
int inlist = FALSE,j;
while((ch) != '\n')
{
scanf("%c",&ch);
for(j = 0; j < L.length; j++)
if(ch == L.elem[j])
{
inlist = TRUE;
break;
}
else
inlist = FALSE;
if(!inlist && ch != '\n')
ListInsert_Sq(L,L.length+1,ch);
}
ch=0;
}
/*判断相等*/
Status Equal(ElemType a,ElemType b)
{
if(a == b)
return TRUE;
else
return FALSE;
}
/*查找元素并返回*/
int LocateElem_Sq(SqList L,ElemType e,Status(* compare)(ElemType,ElemType))
{
ElemType *p;
int i;
i = 1;
p = L.elem;
while(i <= L.length && !(* compare)(*p++,e))
++i;
if(i <= L.length)
return i;
else
return 0;
}
/*销毁*/
Status Clear_Sq(SqList &L)
{
ElemType elem;
free(L.elem);
L.elem = NULL;
return OK;
}
/*打印*/
void Print_Sq(SqList L){
int i;
for(i = 0; i < L.length; i++)
cout<<L.elem[i]<<endl;
if(L.length == 0)
cout<<"空集"<<endl;
cout<<"集合中个数为"<<L.length<<endl;
}
/*求并集*/
void Union_Sq(SqList La,SqList Lb,SqList)
{
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))//用Equal比较- -
ListInsert_Sq(Lc,Lc.length+1,elem);
}
}
/*求交集*/
void Mix_Sq(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))//用Equal比较- -
ListInsert_Sq(Lc,Lc.length+1,elem);
}
}
/*求差集*/
void Minus_Sq(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);
}
}
int main()
{
char s;
int l=1;
InitList_Sq(La);
cout<<"请输入第一个集合"<<endl;
CreateList_Sq(La);
Print_Sq(La);
InitList_Sq(Lb);
cout<<"请输入第二个集合"<<endl;
CreateList_Sq(Lb);
Print_Sq(Lb);
InitList_Sq(Lc);
cout<<"输入a进行集合的并运算,输入b进行集合的交运算,输入c进行集合的差运算"<<endl;
cin>>s;
switch(s)
{
case 'a': Union_Sq(La,Lb,Lc);
Print_Sq(Lc);
break;
case 'b': Mix_Sq(La,Lb,Lc);
Print_Sq(Lc);
break;
case 'c': Minus_Sq(La,Lb,Lc);
Print_Sq(Lc);
break;
}
return 0;
}