-
Notifications
You must be signed in to change notification settings - Fork 0
/
set.test.c
153 lines (130 loc) · 2.47 KB
/
set.test.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "set.h"
typedef struct UserInfo_{
int seq;
char username[20];
}UserInfo;
int match(const void *data1, const void *data2){
if(((UserInfo *)data1)->seq == ((UserInfo *)data2)->seq &&
strcmp(((UserInfo *)data1)->username, ((UserInfo *)data2)->username) == 0){
return 1;
}
return 0;
}
// void destroy(void* data){
// free(data);
// }
void print(const void *data){
if(data == NULL){
return;
}
puts(((UserInfo *)data)->username);
}
// int match_int(const void *data1, const void *data2){
// if(*((int *)data1) == *((int *)data2)){
// return 1;
// }
// return 0;
// }
Set *st, *st1, *st2;
UserInfo *u[3];
int create_set(){
if((st = (Set *)malloc(sizeof(Set))) == NULL){
return -1;
}
if((st1 = (Set *)malloc(sizeof(Set))) == NULL){
free(st);
return -1;
}
if((st2 = (Set *)malloc(sizeof(Set))) == NULL){
free(st);
free(st1);
return -1;
}
set_init(st, NULL, match);
set_init(st1, NULL, match);
set_init(st2, NULL, match);
for(int i=0; i<3; i++){
if((u[i] = (UserInfo *)malloc(sizeof(UserInfo))) == NULL){
for(int j=i-1; j>=0; j--){
free(u[j]);
return -1;
}
}
u[i]->seq = i;
sprintf(u[i]->username, "%d-item", i);
}
return 0;
}
void destroy_set(){
set_destroy(st);
set_destroy(st1);
set_destroy(st2);
free(st);
free(st1);
free(st2);
for(int i=0; i<3; i++){
free(u[i]);
}
}
int main(){
if(create_set() == -1){
return -1;
}
puts("Add 0-item into Set1: ");
set_insert(st1, u[0]);
set_dump(st1, print);
puts("");
puts("Add 1-item into Set1: ");
set_insert(st1, u[1]);
set_dump(st1, print);
puts("");
puts("Add 0-item into Set2: ");
set_insert(st2, u[0]);
set_dump(st2, print);
puts("");
puts("Set1 intersection with Set2 is: ");
set_intersection(st, st1, st2);
set_dump(st, print);
puts("");
puts("Set is equal to Set2: ");
if(set_is_equal(st, st2)){
puts("Yes!");
}
else{
puts("No!");
}
puts("");
puts("Set1 difference with Set2 is: ");
set_difference(st, st1, st2);
set_dump(st, print);
puts("");
puts("Set1 union with Set2 is: ");
set_union(st, st1, st2);
set_dump(st, print);
puts("");
puts("Set2 in Set1: ");
if(set_is_subset(st1, st2)){
puts("Yes!");
}
else{
puts("No!");
}
puts("");
puts("Remove 1-item from Set1: ");
set_remove(st1, u[1]);
set_dump(st1, print);
puts("");
puts("Set1 now equal to Set2: ");
if(set_is_equal(st1, st2)){
puts("Yes!");
}
else{
puts("No!");
}
puts("");
destroy_set();
return 0;
}