编制一个能演示执行集合的并、交和差运算的程序。(有序表)

来源:互联网 发布:龙胆亮银枪玩具淘宝 编辑:程序博客网 时间:2024/04/29 16:40

 

编制一个能演示执行集合的并、交和差运算的程序。  

Code:
  1. #include "F:/SqList.cpp"  
  2. #include<iostream.h>  
  3. #include<string.h>  
  4. void UnionList(SqList *LA,SqList *LB,SqList *&LC)//并集  
  5. {  
  6.     int i=0,j=0,k=0;    //i、j、k分别作为LA、LB、LC的下标  
  7.     LC=(SqList *)malloc(sizeof(SqList));  
  8.     LC->length=0;  
  9.     while (i<LA->length && j<LB->length)  
  10.     {     
  11.         if (LA->data[i]<LB->data[j])  
  12.         {     
  13.             LC->data[k]=LA->data[i];  
  14.             i++;k++;  
  15.         }          
  16.         else  if(LA->data[i]==LB->data[j])  
  17.         {  
  18.             LC->data[k]=LA->data[i];  
  19.             i++;k++;j++;  
  20.         }  
  21.         else if(LA->data[i]>LB->data[j])  
  22.         {  
  23.             LC->data[k]=LB->data[j];  
  24.             k++;j++;  
  25.         }  
  26.     }  
  27.     while (i<LA->length)    //LA尚未扫描完,将其余元素插入LC中  
  28.     {     
  29.         LC->data[k]=LA->data[i];  
  30.         i++;k++;  
  31.     }  
  32.     while (j<LB->length)  //LB尚未扫描完,将其余元素插入LC中  
  33.     {     
  34.         LC->data[k]=LB->data[j];  
  35.         j++;k++;  
  36.     }   
  37.     LC->length=k;  
  38. }  
  39. void Commnode(SqList *LA,SqList *LB,SqList *&LC)//交集  
  40. {  
  41.      int i=0,j=0,k=0;  
  42.      LC=(SqList *)malloc(sizeof(SqList));  
  43.      LC->length=0;  
  44.      while(i<LA->length&&j<LB->length)    
  45.     {  
  46.          if(LA->data[i]<LB->data[j])      
  47.         {  
  48.             i++;      
  49.         }  
  50.         else if(LA->data[i]==LB->data[j])  
  51.         {  
  52.             LC->data[k]=LA->data[i];  
  53.                i++;j++;k++;           
  54.         }  
  55.         else if(LA->data[i]>LB->data[j])  
  56.         {  
  57.             j++;      
  58.         }  
  59.     }  
  60.     LC->length=k;  
  61. }  
  62. void Subtraction(SqList *LA,SqList *LB,SqList *&LC)//差集  
  63. {  
  64.      int i=0,j=0,k=0;  
  65.      LC=(SqList *)malloc(sizeof(SqList));  
  66.      LC->length=0;  
  67.      while(i<LA->length&&j<LB->length)    
  68.     {  
  69.          if(LA->data[i]<LB->data[j])      
  70.         {  
  71.             LC->data[k]=LA->data[i];  
  72.             i++;k++;      
  73.         }   
  74.         else if(LA->data[i]==LB->data[j])  
  75.         {  
  76.             i++;j++;  
  77.         }  
  78.         else if(LA->data[i]>LB->data[j])  
  79.         {  
  80.             j++;  
  81.         }  
  82.     }  
  83.     while(i<LA->length)  
  84.     {  
  85.          LC->data[k]=LA->data[i];  
  86.           i++;k++;        
  87.     }      
  88.     LC->length=k;  
  89. }  
  90. void main()  
  91. {   char a[10],b[10];  
  92.     SqList *L1,*L2,*L3;  
  93.     int a1,b1;  
  94.         InitList(L1);  
  95.         InitList(L2);  
  96.         InitList(L3);  
  97.         int A=0;  
  98.     do{   
  99.        cout<<"请输入集合A:";  
  100.        cin>>a;  
  101.        a1=strlen(a);  
  102.        for(int i=0;i<a1;i++)  
  103.           ListInsert(L1,a[i]);  
  104.        if(a1>ListLength(L1))  
  105.        {  
  106.          cout<<"注意 ! 元素有重复"<<endl;  
  107.          DestroyList(L1);InitList(L1);  
  108.        }  
  109.        else if(a1==ListLength(L1))A=1;  
  110.       }while(A==0);   
  111.         int B=0;  
  112.   
  113.          do{      
  114.         cout<<"请输入集合B:";  
  115.         cin>>b;  
  116.         b1=strlen(b);  
  117.         for(int j=0;j<b1;j++)  
  118.           ListInsert(L2,b[j]);  
  119.         if(b1>ListLength(L2))  
  120.         {  
  121.           cout<<"注意 ! 元素有重复"<<endl;  
  122.           DestroyList(L2);InitList(L2);  
  123.         }  
  124.         else if(b1==ListLength(L2)) B=1;  
  125.        }while(B==0);  
  126.   
  127.       cout<<"集合A的长度:"<<a1<<endl;  
  128.       cout<<"集合B的长度:"<<b1<<endl;  
  129.        
  130.   
  131.       cout<<"集合A在顺序表中的结构:"<<endl;  
  132.       DispList(L1);  
  133.       cout<<"集合B在顺序表中的结构:"<<endl;  
  134.       DispList(L2);   
  135.   
  136.       cout<<"并集:"<<endl;  
  137.       UnionList(L1,L2,L3);  
  138.       DispList(L3);  
  139.       cout<<"交集:"<<endl;  
  140.       Commnode(L1,L2,L3);  
  141.       DispList(L3);  
  142.       cout<<"差集:"<<endl;  
  143.       Subtraction(L1,L2,L3);  
  144.       DispList(L3);   
  145. }  
  146. //SqList.cpp文件  
  147. #include <stdio.h>  
  148. #include <malloc.h>  
  149. #define MaxSize 50  
  150. typedef char ElemType;   
  151. typedef struct   
  152. {   ElemType data[MaxSize];     //存放顺序表元素  
  153.     int length;         //存放顺序表的长度  
  154. } SqList;                               //顺序表的类型定义  
  155.   
  156.                           
  157. void CreateList(SqList *&L,ElemType a[],int n)  //建立顺序表  
  158. {  
  159.     int i;  
  160.     L=(SqList *)malloc(sizeof(SqList));  
  161.     for (i=0;i<n;i++)  
  162.         L->data[i]=a[i];  
  163.     L->length=n;  
  164. }  
  165.   
  166.   
  167. void InitList(SqList *&L)                       //初始化线性表  
  168. {  
  169.     L=(SqList *)malloc(sizeof(SqList)); //分配存放线性表的空间  
  170.     L->length=0;  
  171. }  
  172. void DestroyList(SqList *&L)                    //销毁线性表  
  173. {  
  174.     free(L);  
  175. }  
  176. int ListEmpty(SqList *L)                        //判线性表是否为空表  
  177. {  
  178.     return(L->length==0);  
  179. }  
  180. int ListLength(SqList *L)                       //求线性表的长度  
  181. {  
  182.     return(L->length);  
  183. }  
  184. void DispList(SqList *L)                        //输出线性表  
  185. {  
  186.     int i;  
  187.     if (ListEmpty(L)) return;  
  188.     for (i=0;i<L->length;i++)  
  189.         printf("%c ",L->data[i]);  
  190.     printf("/n");  
  191. }  
  192. int GetElem(SqList *L,int i,ElemType &e)        //求线性表中某个数据元素的值  
  193. {  
  194.     if (i<1 || i>L->length)  
  195.         return 0;  
  196.     e=L->data[i-1];  
  197.     return 1;  
  198. }  
  199. int LocateElem(SqList *L, ElemType e)           //按元素值查找  
  200. {  
  201.     int i=0;  
  202.     while (i<L->length && L->data[i]!=e) i++;  
  203.     if (i>=L->length)  
  204.         return 0;  
  205.     else  
  206.         return i+1;  
  207. }  
  208. int ListInsert(SqList *&L,int i,ElemType e)     //插入数据元素  
  209. {  
  210.     int j;  
  211.     if (i<1 || i>L->length+1)  
  212.         return 0;  
  213.     i--;                    //将顺序表位序转化为elem下标  
  214.     for (j=L->length;j>i;j--)           //将data[i]及后面元素后移一个位置  
  215.         L->data[j]=L->data[j-1];  
  216.     L->data[i]=e;  
  217.     L->length++;                //顺序表长度增1  
  218.     return 1;  
  219. }  
  220. int ListInsert(SqList *&L,ElemType e)           //有序表  
  221. {  
  222.    int i=0,j;  
  223.    while(i<L->length&&L->data[i]<e)i++;  
  224.    if(L->data[i]==e)return 0;  
  225.    for(j=ListLength(L);j>i;j--)  
  226.        L->data[j]=L->data[j-1];  
  227.    L->data[i]=e;  
  228.    L->length++;  
  229.    return 1;  
  230. }  
  231. int ListDelete(SqList *&L,int i,ElemType &e)    //删除数据元素  
  232. {  
  233.     int j;  
  234.     if (i<1 || i>L->length)  
  235.         return 0;  
  236.     i--;                    //将顺序表位序转化为elem下标  
  237.     e=L->data[i];  
  238.     for (j=i;j<L->length-1;j++)         //将data[i]之后的元素前移一个位置  
  239.         L->data[j]=L->data[j+1];  
  240.     L->length--;                //顺序表长度减1  
  241.     return 1;  
  242. }  
  243.   
  244.    

 

原创粉丝点击