[C] 集合的并交差运算 →→→→→进入此内容的聊天室

来自 , 2019-05-16, 写在 C, 查看 126 次.
URL http://www.code666.cn/view/2f4fe03d
  1. #include<stdio.h>
  2. #include<malloc.h>
  3. #include<stdlib.h>
  4. #include<iostream>
  5. using namespace std;
  6.  
  7. typedef int ElemType;  //假设线性表中的元素均为整型
  8. typedef struct
  9. {
  10.     ElemType* elem;   //存储空间基地址
  11.     int length;       //表中元素的个数
  12. } SqList;   //顺序表类型定义
  13.  
  14. int ListCreate_Sq(SqList &L,int n)
  15. {
  16.     L.elem=new ElemType[n];
  17.     for(int i=0; i<n; i++)
  18.         cin>>L.elem[i];
  19.     L.length=n;
  20.     return 1;
  21. }
  22. void ListPrintf_Sq(SqList &L)
  23. {
  24.     ElemType *p;
  25.     cout<<L.length;
  26.     for(p=L.elem; p<L.elem+L.length; ++p)
  27.         printf(" %d",*p);
  28.     cout<<endl;
  29. }
  30. void Listjiao(SqList &a,SqList &b,SqList &c)
  31. {
  32.     c.elem=new ElemType[200];
  33.     int p,i,j;
  34.     for(i=0;i<a.length;i++)
  35.         c.elem[i]=a.elem[i];
  36.     c.length=a.length;
  37.     for(i=0,j=a.length;i<b.length;i++)
  38.     {
  39.         for(p=0;p<a.length;p++)
  40.         {
  41.             if(a.elem[p]==b.elem[i])
  42.                 break;
  43.         }
  44.         if(p==a.length)
  45.         {
  46.             c.elem[j++]=b.elem[i];
  47.             c.length++;
  48.         }
  49.     }
  50. }
  51. void Listbing(SqList &a,SqList &b,SqList &c)
  52. {
  53.     c.elem=new ElemType[100];
  54.     c.length=0;
  55.     int i,j,p;
  56.     for(i=0;i<a.length;i++)
  57.     {
  58.         for(j=0;j<b.length;j++)
  59.         {
  60.             if(b.elem[j]==a.elem[i])
  61.                c.elem[c.length++]=a.elem[i];
  62.         }
  63.     }
  64. }
  65. void Listcha(SqList &a,SqList &b,SqList &c)
  66. {
  67.     c.elem=new ElemType[100];
  68.     c.length=0;
  69.     int i,j;
  70.     for(i=0; i<a.length; i++)
  71.     {
  72.  
  73.         for(j=0; j<b.length; j++)
  74.         {
  75.             if(b.elem[j]==a.elem[i])
  76.                 break;
  77.         }
  78.         if(j==b.length)
  79.             c.elem[c.length++]=a.elem[i];
  80.     }
  81. }
  82. int main()
  83. {
  84.     SqList La,Lb,L1,L2,L3;
  85.     int a,b;
  86.     cin>>a>>b;
  87.     ListCreate_Sq(La,a);
  88.     ListCreate_Sq(Lb,b);
  89.     Listjiao(La,Lb,L1);
  90.     Listbing(La,Lb,L2);
  91.     Listcha(La,Lb,L3);
  92.     ListPrintf_Sq(L1);
  93.     ListPrintf_Sq(L2);
  94.     ListPrintf_Sq(L3);
  95.     return 0;
  96. }
  97.  

回复 "集合的并交差运算"

这儿你可以回复上面这条便签

captcha