#define listmaxsize 100
#define listinc 10
typedef int status;
typedef int elemtype;
typedef struct
{
elemtype *elem;
int length;
int listsize;
}sqlist;

void bangzhu()
{
clrscr();
printf("\n\t\t\t***********************************");
printf("\n\t\t\t*    输入1求A集合与B集合的交集    *");
printf("\n\t\t\t*    输入2求A集合与B集合的并集    *");
printf("\n\t\t\t*    输入3求A集合与B集合的差      *");
printf("\n\t\t\t*    输入5出本程序                *");
printf("\n\t\t\t*     按任意键返回主菜单!         *");
printf("\n\t\t\t***********************************");
getch();
return;
}

void printstar()
{
clrscr();
gotoxy (24,4);
printf("************************************");
gotoxy (24,5);
printf("*      求集合的交、并、差运算       *");
gotoxy (24,6);
printf("*            1.求交运算            *");
gotoxy (24,7);
printf("*            2.求并运算            *");
gotoxy (24,8);
printf("*            3.求差运算            *");
gotoxy (24,9);
printf("*            4.帮助                *");
gotoxy (24,10);
printf("*            5.退出                *");
gotoxy (24,11);
printf("************************************");
gotoxy (24,12);
printf("            请选择(1-5):");
}

void  initlist_sq (sqlist *l)
{
l->elem=(elemtype*)malloc(listmaxsize*sizeof(elemtype));
if (!l->elem)
   {
    printf("overflow");
    getch();
   }
l->length=0;
l->listsize=listmaxsize;
return;
}

void inputab (sqlist *a,sqlist *b)
{
int e,flag,i;
elemtype *newbase;
initlist_sq(a);
initlist_sq(b);
printf("\n请输入A集合的值(以-1结束):");
scanf("%d",&e);
while(e!=-1)
{
  flag=1;
  for (i=1;i<=a->length;i++)
    if (e==a->elem[i-1]) {flag=0;break;}
  if (flag)
  {
  a->elem[a->length]=e;
  a->length++;
  if (a->length>a->listsize)
     {
      newbase=(elemtype*)realloc(a->elem,(a->listsize+listinc)*sizeof(elemtype));
      if (!newbase)
         {
          printf("overflow");
          getch();
         }
      a->elem=newbase;
      a->listsize+=listinc;
      }
   }    
  scanf("%d",&e);
  }
printf("\n请输入B集合的值(以-1结束):");
scanf("%d",&e);
while(e!=-1)
{
  flag=1;
  for (i=1;i<=b->length;i++)
    if (e==b->elem[i-1]) {flag=0;break;}
  if (flag)
  {
  b->elem[b->length]=e;
  b->length++;
  if (b->length>b->listsize)
     {
      newbase=(elemtype*)realloc(b->elem,(b->listsize+listinc)*sizeof(elemtype));
      if (!newbase)
         {
          printf("overflow");
          getch();
         }
      b->elem=newbase;
      b->listsize+=listinc;
      }
    }  
   scanf("%d",&e);
  }
return;
}

void ajiaob(sqlist *a,sqlist *b)
{
sqlist c;
int i,j,flag;
initlist_sq(&c);
for (i=1;i<=a->length;i++)
   {
    flag=0;
    for (j=1;j<=b->length;j++)
      if (a->elem[i-1]==b->elem[j-1]) {flag=1;break;}
    if (flag)
     {
      c.elem[c.length]=a->elem[i-1];
      c.length++;
     }
   }
printa(a);
printb(b);
printf("\nA集合交B集合的值:");
printc(&c);
free(a);
free(b);
free(c);
return;
}

void abinb(sqlist *a,sqlist *b)
{
int i,j,flag;
sqlist c;
elemtype *newbase;
initlist_sq(&c);
if ((a->length+b->length)>c.listsize)
    {
      newbase=(elemtype*)realloc(c.elem,(b->listsize+listinc)*sizeof(elemtype));
      if (!newbase)
         {
          printf("overflow");
          getch();
         }
      c.elem=newbase;
      c.listsize+=listinc;
     }
  for (i=1;i<=a->length;i++)
   {
    c.elem[c.length]=a->elem[i-1];
    c.length++;    
   }
  for (i=1;i<=b->length;i++)
   {
    flag=1;
    for (j=1;j<=c.length;j++)
     if (c.elem[j-1]==b->elem[i-1]) {flag=0;break;}
    if (flag)
     {
      c.elem[c.length]=b->elem[i-1];
      c.length++;
     }
   }
  printa(a);
  printb(b);
  printf("\nA集合并B集合的值:");
  printc(&c);
return;
}

void asubb(sqlist *a,sqlist *b)
{
int i,j,flag;
sqlist c;
initlist_sq(&c);
for (i=1;i<=a->length;i++)
  {
   flag=1;
   for (j=1;j<=b->length;j++)
    if (a->elem[i-1]==b->elem[j-1]) {flag=0;break;}
   if (flag)
    {
     c.elem[c.length]=a->elem[i-1];
     c.length++;
    }
   }
printa(a);
printb(b);
printf("\nA集合与B集合的差:");
printc(&c);
return;
}

printa (sqlist *a)
{
int i;
printf("\nA集合的值 :");
for (i=1;i<=a->length;i++)
   {
    printf("\t %d",a->elem[i-1]);
    if (i%8==0) printf("\n\t");
   }
}

printb (sqlist *b)
{
int i;
printf("\nB集合的值 :");
for (i=1;i<=b->length;i++)
   {
    printf("\t %d",b->elem[i-1]);
    if (i%8==0) printf("\n\t");
   }                 
return;
}

printc (sqlist *c)
{
int i;
for (i=1;i<=c->length;i++)
   {
    printf("\t%d",c->elem[i-1]);
    if (i%7==0) printf("\n\t\t");
   }
printf("\n\t\t\t\t 按任意键返回主菜单!!");
getch();
return;
}

void main()
{
sqlist a,b;
int n;
while(1)
{
  printstar();
  scanf("%d",&n);
  if ((n<1)||(n>5))
   {
    printf("\n\t\t\t     输入不合法,按任意键重新输入!!!");
    getch();
   }
   else
   {
     if (n==5) {printf("\n\n\n\n\t\t\t\t  按任意键退出!!!!");getch();return;}
     if (n==4) bangzhu();
       else
       {
       inputab(&a,&b);
       switch(n)
       {
        case 1:ajiaob(&a,&b);break;
        case 2:abinb(&a,&b);break;
        case 3:asubb(&a,&b);break;
        }
       }
    }
  }
}