/*************************************** ****************************************
**
* File: Aggregate. C *
* Date: 2007-10-29 *
* Version: 1.0 *
**
**
**************************************** ****************************************
* Description: Operation of the set: intersection, union, and supplement *
* 1. Set input: automatically removes duplicate and invalid characters *
* 2. display of sets: all elements of the output set *
* 3. output the completion set of a given set *
* 4. output the intersection and union of two given sets *
**
**
**************************************** ***************************************/
# Include <stdio. h>
# Include <stdlib. h>
# Include <malloc. h>
# Include <conio. h>
# Define maxsize 150/* maximum array limit */
Typedef int elemtype;/* Data Element type */
Typedef struct
{Elemtype A [maxsize];/* One-dimensional array subdomain */
Int length;/* Table length subdomain */
} Sqlist;/* structure type of sequential storage */
/* Function declaration */
Void creat_whole (sqlist * l);/* create a complete set */
Void creat_list (sqlist * l);/* input set */
Void leach_list (sqlist * l);/* remove duplicate characters */
Void out_list (sqlist L);/* output set */
Void complement_list (sqlist * L1, sqlist * l2);/* Find a complementary set */
Void intersect_list (sqlist * L1, sqlist * L2, sqlist * l3);/* intersection calculation */
Void union_list (sqlist * L1, sqlist * L2, sqlist * l3);/* calculate the Union set */
/* Main function */
Main ()
{Int choice;/* record the operation selected */
Do {
Sqlist a, B, C1, C2, D1, D2, D3, D4;
A. Length = B. Length = 0;/* initialize the struct variable */
C1.length = c2.length = 0;
D1.length = d2.length = d3.length = d4.length = 0;
Printf ("/n ================================ =========================================/N ");
Printf ("/n 1. Output full set ");
Printf ("/N 2. Enter a set and output the valid elements of the Set ");
Printf ("/N 3. Enter a set and display its complementary set ");
Printf ("/N 4. Enter two sets and display their intersection and union ");
Printf ("/n 5. Terminate the program ");
Printf ("/n ==================================== ============================ ");
Printf ("/n please input your choice (, 5 ):");
Scanf ("% d", & choice );
Switch (choice)
{Case 1 :{
Creat_whole (& A);/* create a complete set */
Printf ("the whole aggergate is :");
Out_list (a);/* output Complete Set */
} Break;
Case 2: {printf ("/n please input data:");/* Indicates entering a set */
Creat_list (& B );
Printf ("/n the aggregate is :");
Out_list (B );
} Break;
Case 3: {printf ("Please input data :");
Creat_list (& C1 );
Printf ("/n the aggregate you input is :");
Out_list (C1 );
Creat_whole (& C2 );
Complement_list (& C1, & C2);/* Find a supplement */
Printf ("/n the complementary is :");
Out_list (C2);/* output completion set */
} Break;
Case 4: {printf ("/nplease input aggregate_1st :");
Creat_list (& D1);/* create the first set */
Printf ("/nplease input aggrespon_2nd :");
Creat_list (& D2);/* Create the second set */
Printf ("/nthe aggregate_1st is :");
Out_list (D1);/* output the first set */
Printf ("/nthe aggrespon_2nd is :");
Out_list (D2);/* output the second set */
Printf ("/nthe intersection is :");
Intersect_list (& D1, & D2, & D3);/* calculates the intersection */
Out_list (D3);/* output intersection */
Printf ("/nthe union is :");
Union_list (& D1, & D2, & D4);/* calculate the Union set */
Out_list (D4);/* output Union */
} Break;
}/* Switch */
} While (choice> = 1 & choice <5);/* the input is valid and loops */
Printf ("/n goodbye! ");
}/* Main */
/* Create a linear table containing 26 uppercase letters */
Void creat_whole (sqlist * l ){
Int I;
For (I = 65; I <= 90; I ++ ){
L-> A [i-65] = I;
L-> length ++;/* The table length increases by 1 */
}
}/* Creat_whole */
/* Enter a set */
Void creat_list (sqlist * l)
{Int I = 0;
Char ch;
Ch = getchar ();/* receives the '/N' character of the keyboard and discards it */
Ch = getchar ();/* take the next character */
While (Ch! = '/N '){
If (CH> = 65 & Ch <= 90) {/* store uppercase letters in the linear table */
L-> A [I] = CH;
L-> length ++;
I ++;
}
Ch = getchar ();
}
Leach_list (l);/* call the filter function to remove duplicate characters */
}/* Creat_list */
/* Delete duplicate elements */
Void leach_list (sqlist * l ){
Int I, J, K;
For (I = 0; I <L-> length; I ++ ){
For (j = I + 1; j <L-> length ;){
If (L-> A [I] = L-> A [J]) {/* If repeated elements are found */
For (k = J + 1; k <L-> length; k + +) L-> A [k-1] = L-> A [k]; /* move the data element forward */
L-> length --;/* The table length minus 1 */
}
Else J ++;
}
}
}/* Leach_list */
/* Calculate the complement set of the Set L1 to the set L2 */
Void complement_list (sqlist * L1, sqlist * l2 ){
Int I, j = 0, K;
For (I = 0; I <L1-> length; I ++ ){
While (L2-> A [J]! = L1-> A [I] & J <L2-> length ){
J ++;
}
If (L1-> A [I] = L2-> A [J]) {/* If an equal element is found */
For (k = J + 1; k <L2-> length; k + +) L2-> A [k-1] = L2-> A [k]; /* move the data element forward */
L2-> length --;
}
J = 0;
}
}/* Complement_list */
/* Calculate the intersection of two sets */
Void intersect_list (sqlist * L1, sqlist * L2, sqlist * l3 ){
Int I, j = 0, K = 0;
For (I = 0; I <L1-> length; I ++ ){
While (L2-> A [J]! = L1-> A [I] & J <L2-> length ){
J ++;
}
If (L1-> A [I] = L2-> A [J]) {/* If an equal element is found */
L3-> A [k] = L1-> A [I];/* store equal elements in the new table */
L3-> length ++;
K ++;
}
J = 0;
}
}/* Intersect_list */
/* Calculate the union of two sets */
Void union_list (sqlist * L1, sqlist * L2, sqlist * l3 ){
Int I, j = 0, K;
For (k = 0; k <L2-> length; k ++) {/* Copy one of the tables to the new table */
L3-> A [k] = L2-> A [k];
L3-> length ++;
}
For (I = 0; I <L1-> length; I ++ ){
While (L2-> A [J]! = L1-> A [I] & J <L2-> length ){
J ++;
}
If (j = L2-> length) {/* If an unequal element is found */
L3-> A [L3-> length] = L1-> A [I];/* Insert unequal elements into the new table */
L3-> length ++;
}
J = 0;
}
}/* Union_list */
/* Output linear table */
Void out_list (sqlist L)
{Int I;
If (L. Length = 0) printf ("null! ");/* If the table is empty */
For (I = 0; I <L. length; I ++) printf ("% 2C", L. A [I]);
}/* Out_list */