-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathset.c
105 lines (99 loc) · 1.82 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
#include<stdio.h>
struct bit
{
unsigned char x:1;
};
int u[]={1,2,3,4,5,6,7,8,9},size=9; //declaring universal set
void main()
{
void insert(struct bit[],int);
void display(struct bit[]);
void setunion(struct bit[],struct bit[],struct bit[]);
void setintersection(struct bit[],struct bit[],struct bit[]);
void setdiff(struct bit[],struct bit[],struct bit[]);
struct bit a[9]={0}; //assigning zeros to all places of a
struct bit b[9]={0}; //assigning zeros to all places of b
struct bit c[9]={0}; //assigning zeros to all places of c
int x,y,i;
printf("Enter the size of set A:\n");
scanf("%d",&x);
printf("Enter the elements of set A:\n");
insert(a,x);
printf("Enter the size of set B:\n");
scanf("%d",&y);
printf("Enter the elements of set B:\n");
insert(b,y);
printf("A U B: ");
setunion(a,b,c);
printf("\n");
printf("\nA I B: ");
setintersection(a,b,c);
printf("\n");
printf("\nA-B: ");
setdiff(a,b,c);
printf("\n");
}
void insert(struct bit a[],int n)
{
int i,d,j;
printf("Enter %d elements: \n",n);
for(i=0;i<n;i++)
{
scanf("%d",&d);
for(j=0;j<size;j++)
{
if(d==u[j])
{
a[j].x=1;
break;
}
}
}
}
void display(struct bit a[])
{
int i;
printf("{");
for(i=0;i<size;i++)
{
if(a[i].x==(unsigned char)1)
{
if(i!=0){
printf(",");
}
printf("%d",u[i]);
}
}
printf("}");
return;
}
void setunion(struct bit a[],struct bit b[],struct bit c[])
{
int i;
for(i=0;i<size;i++)
{
c[i].x=a[i].x | b[i].x;
}
display(c);
return;
}
void setintersection(struct bit a[],struct bit b[],struct bit c[])
{
int i;
for(i=0;i<size;i++)
{
c[i].x=a[i].x & b[i].x;
}
display(c);
return;
}
void setdiff(struct bit a[],struct bit b[],struct bit c[])
{
int i;
for(i=0;i<size;i++)
{
c[i].x=a[i].x & !(b[i].x);
}
display(c);
return;
}