#include <stdio.h>
typedef struct {
int n;//the cardinal of the set
int set[100];
} TSet;
void read(TSet *p) {
printf("number of elements N = "); for(int i
= 0; i
< p
->n
; ++i
) scanf("%d",&p
->set
[i
]); }
int belongTo(TSet p, int elem){
int is = 0;
for(int i = 0; i < p.n && !is; ++i) {
if(elem == p.set[i]) {
is = 1;
}
}
return is;
}
void display(TSet p) {
for(int i
= 0; i
< p.
n-1; ++i
) printf("%d, ",p.
set[i
]); }
int main(int argc, char const *argv[]) {
TSet A, B,//first and second sets
I, //intersection
D, //difference
R; //union
I.n = 0;
D.n = 0;
//read the first set
read(&A);
//read the second set
read(&B);
display(A);
display(B);
R = B;
for(int i = 0; i < A.n;++i) {
if(belongTo(B, A.set[i])==1) {
I.set[I.n++] = A.set[i];
} else {
D.set[D.n++] = A.set[i];
R.set[R.n++] = A.set[i];
}
}
display(I);
display(D);
display(R);
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+Cgp0eXBlZGVmIHN0cnVjdCB7CiAgaW50IG47Ly90aGUgY2FyZGluYWwgb2YgdGhlIHNldAogIGludCBzZXRbMTAwXTsKfSBUU2V0OwoKdm9pZCByZWFkKFRTZXQgKnApIHsKICAgcHJpbnRmKCJudW1iZXIgb2YgZWxlbWVudHMgTiA9ICIpOwogICBzY2FuZigiJWQiLCZwLT5uKTsKICAgZm9yKGludCBpID0gMDsgaSA8IHAtPm47ICsraSkgc2NhbmYoIiVkIiwmcC0+c2V0W2ldKTsKfQoKaW50IGJlbG9uZ1RvKFRTZXQgcCwgaW50IGVsZW0pewogIGludCBpcyA9IDA7CiAgZm9yKGludCBpID0gMDsgaSA8IHAubiAmJiAhaXM7ICsraSkgewogICAgaWYoZWxlbSA9PSBwLnNldFtpXSkgewogICAgICBpcyA9IDE7CiAgICB9CiAgfQogIHJldHVybiBpczsKfQoKdm9pZCBkaXNwbGF5KFRTZXQgcCkgewogIGZvcihpbnQgaSA9IDA7IGkgPCBwLm4tMTsgKytpKSBwcmludGYoIiVkLCAiLHAuc2V0W2ldKTsKICBwcmludGYoIiVkIixwLnNldFtwLm4tMV0pOwogIHByaW50ZigiXG4iKTsKfQoKaW50IG1haW4oaW50IGFyZ2MsIGNoYXIgY29uc3QgKmFyZ3ZbXSkgewogIFRTZXQgQSwgQiwvL2ZpcnN0IGFuZCBzZWNvbmQgc2V0cwogICAgICAgSSwgLy9pbnRlcnNlY3Rpb24KICAgICAgIEQsIC8vZGlmZmVyZW5jZQogICAgICAgUjsgLy91bmlvbgogIEkubiA9IDA7CiAgRC5uID0gMDsKCiAgLy9yZWFkIHRoZSBmaXJzdCBzZXQKICByZWFkKCZBKTsKICAvL3JlYWQgdGhlIHNlY29uZCBzZXQKICByZWFkKCZCKTsKCiAgcHJpbnRmKCJTZXQgQTpcbiIpOwogIGRpc3BsYXkoQSk7CiAgcHJpbnRmKCJTZXQgQjpcbiIpOwogIGRpc3BsYXkoQik7CiAgUiA9IEI7CgogIGZvcihpbnQgaSA9IDA7IGkgPCBBLm47KytpKSB7CiAgICAgaWYoYmVsb25nVG8oQiwgQS5zZXRbaV0pPT0xKSB7CiAgICAgICAgSS5zZXRbSS5uKytdID0gQS5zZXRbaV07CiAgICAgfSBlbHNlIHsKICAgICAgIEQuc2V0W0QubisrXSA9IEEuc2V0W2ldOwogICAgICAgUi5zZXRbUi5uKytdID0gQS5zZXRbaV07CiAgICAgfQogIH0KICBwcmludGYoIkludGVyc2VjdGlvbjpcbiIpOwogIGRpc3BsYXkoSSk7CiAgcHJpbnRmKCJEaWZmZXJlbmNlOlxuIik7CiAgZGlzcGxheShEKTsKICBwcmludGYoIlVuaW9uOlxuIik7CiAgZGlzcGxheShSKTsKICByZXR1cm4gMDsKfQ==
number of elements N = number of elements N = Set A:
1, 2, 3, 4, 5
Set B:
-1, 2, 3
Intersection:
2, 3
Difference:
1, 4, 5
Union:
-1, 2, 3, 1, 4, 5