-
Notifications
You must be signed in to change notification settings - Fork 0
/
set.c
194 lines (165 loc) · 4.07 KB
/
set.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#include "set.h"
#include "utilities.h"
#include <stdio.h>
#include <stdlib.h>
void symdiff_set(set *groupA, set *groupB, set *groupC)
{
}
void sub_set(set *groupA, set *groupB, set *groupC)
{
int i = 0;
int j = 0;
int list[MAX_MEMBERS_IN_SET] = {EOF};
/*set the 3'rd set to empty group*/
resetSet (groupC);
/*add all members from the first set to dest set*/
while (groupA->members[i] != EXTENSION_OF_SET)
{
if (isExist (groupB,groupA->members[i]) == FALSE){
list[j++] = groupA->members[i];
}
i++;
}
list[j] = EXTENSION_OF_SET;
read_set (groupC, (int *) list);
}
void intersect_set(set *groupA, set *groupB, set *groupC)
{
int i = 0;
int j = 0;
int list[MAX_MEMBERS_IN_SET] = {EOF};
/*set the 3'rd set to empty group*/
resetSet (groupC);
/*add all members from the first set to dest set*/
while (groupA->members[i] != EXTENSION_OF_SET)
{
if (isExist (groupB,groupA->members[i]) == TRUE){
list[j++] = groupA->members[i];
}
i++;
}
i = 0;
/*add all members from the second set to dest set*/
while (groupB->members[i] != EXTENSION_OF_SET)
{
if (isExist (groupA,groupB->members[i]) == TRUE){
list[j++] = groupB->members[i];
}
i++;
}
list[j] = EXTENSION_OF_SET;
read_set (groupC, (int *) list);
}
void union_set(set *groupA, set *groupB, set *groupC)
{
int i = 0;
int j = 0;
int list[MAX_MEMBERS_IN_SET] = {EOF};
/*set the 3'rd set to empty group*/
resetSet (groupC);
/*add all members from the first set to dest set*/
while (groupA->members[i] != EXTENSION_OF_SET)
{
list[j++] = groupA->members[i++];
}
i = 0;
/*add all members from the second set to dest set*/
while (groupB->members[i] != EXTENSION_OF_SET)
{
list[j++] = groupB->members[i++];
}
list[j] = EXTENSION_OF_SET;
read_set (groupC, (int *) list);
}
void read_set(set *group, int *list)
{
int i;
int j;
sortToMax (list);
/*set the group to empty group*/
resetSet (group);
i = 0;
j = 0;
while ((list[i] != EXTENSION_OF_SET))
{
if (isExist (group, list[i]) == FALSE)
{
group->members[j] = list[i];
i++;
j++;
}
else
{
i++;
}
if (i == MAX_MEMBERS_IN_SET - 1)
{
break;
}
}
group->members[i] = EXTENSION_OF_SET;
}
/* This function get an error id and print the error message to the console*/
void printError(int errId)
{
switch (errId)
{
/*labels errors*/
case 1:
printf ("Undefined set name\n");
break;
case 2:
printf ("Invalid set member – value out of range\n");
break;
case 3:
printf ("List of set members is not terminated correctly\n");
break;
case 4:
printf ("Invalid set member – not an integer\n");
break;
case 5:
printf ("Extraneous text after end of command\n");
break;
case 6:
printf ("Missing parameter\n");
break;
case 7:
printf ("Multiple consecutive commas\n");
break;
case 8:
printf ("Missing comma\n");
break;
case 9:
printf ("Illegal comma\n");
break;
case 10:
printf ("Undefined command name\n");
break;
default:
printf ("Undefined Error ID\n");/*failure*/
}
}
void print_set(set *group)
{
int i = 0;
if (group->members[0] == EXTENSION_OF_SET)
{
printf ("The set is empty");
return;
}
while (group->members[i] != EXTENSION_OF_SET)
{
printf ("%3u ", (unsigned char) group->members[i]);
i++;
if (i % MAX_MEMBERS_TO_PRINT == 0)
{
printf ("\n");
}
}
printf ("\n");
}
void stop()
{
printf("Exiting the program...");
exit(0);
}