THIPTTKTT2
/********************************************************
- Phan tich va thiet ke thuat toan
- Bieu dien tap hop bang vecto nhi phan
- Thuc hien cac phep toan voi
- tap hop cac so le va so nguyen to nho hon 10
- Writed by Nguyen The Linh
********************************************************/
#include<stdio.h>
#include<conio.h>
#include<math.h>
// Initialize application
const int n=10; //Max
typedef int Set[n]; // Define Vecto
/********************************************************
- Class tap hop
********************************************************/
// Tao tap hop rong
void makenull_Set(Set &A)
{
for(int i=0;i<n;i++)
A[i]=0;
}
// Lay hop cua hai tap hop A va B tao thanh tap hop C
void union_Set(Set A,Set B,Set &C)
{
for(int i=0;i<n;i++)
C[i]=A[i]||B[i];
}
// Lay giao cua hai tap hop A va B tao thanh tap hop C
void intersection_Set(Set A,Set B,Set &C)
{
for(int i=0;i<n;i++)
C[i]=A[i]&&B[i];
}
// Lay hieu cua hai tap hop A va B tao thanh tap hop C
void difference_Set(Set A,Set B,Set &C)
{
for(int i=0;i<n;i++)
C[i]=A[i]&&(!B[i]);
}
// Kiem tra xem mot phan tu co thuoc tap hop hay khong
// Return 1 if thuoc, return 0 neu khong thuoc
int member_Set(int x,Set A)
{
if(x<0 || x>=n)
return 0;
else
return A[x];
}
// Ham chen mot phan tu vo tap hop
void insert_Set(int x,Set &A)
{
if(!member_Set(x,A))
A[x]=1;
}
// Hien thi cac phan tu cua tap hop ra man hinh
void show_Set(Set A)
{
for(int i=0;i<n;i++)
if(A[i])
printf("%4d",i);
}
/********************************************************
- Chuong trinh chinh
********************************************************/
// Mot so ham phu
// Ham kiem tra mot so n co phai so nguyen to
int is_nguyento(int n)
{
if(n<=1) return 0;
int ok=1,i=2;
while(ok && (i<=(int)sqrt(n)))
{
ok=n%i;
i++;
}
return (ok!=0);
}
// Kiem tra mot so x co phai la so le khong
int is_le(int x)
{
return (x&0x0001);
}
int main()
{
Set A; // Tap cac so nguyen to
Set B; // Tap cac so le
Set C; // Tap giao cua A va B
Set D; // Tap hop cua A va B
Set E; // Tap hieu cua A va B
// Khoi tao cac tap hop A , B la cac tap rong
makenull_Set(A);
makenull_Set(B);
makenull_Set(C);
makenull_Set(D);
makenull_Set(E);
// Insert du lieu vo cac tap hop
for(int i=0;i<n;i++)
{
if(is_nguyento(i))
insert_Set(i,A);
if(is_le(i))
insert_Set(i,B);
}
// Hien thi cac tap hop A va B
printf("
--------------------------------------
");
printf("
Tap hop cac so nguyen to A");
printf("
--------------------------------------
");
show_Set(A);
printf("
--------------------------------------
");
printf("
Tap hop cac so le B");
printf("
--------------------------------------
");
show_Set(B);
// Giao cua hai tap hop
printf("
--------------------------------------
");
printf("
Giao cua hai tap hop");
printf("
--------------------------------------
");
intersection_Set(A,B,C);
show_Set(C);
// Hop cua hai tap hop
printf("
--------------------------------------
");
printf("
Hop cua hai tap hop");
printf("
--------------------------------------
");
union_Set(A,B,D);
show_Set(D);
// Hieu cua hai tap hop
printf("
--------------------------------------
");
printf("
Hieu cua hai tap hop");
printf("
--------------------------------------
");
difference_Set(A,B,E);
show_Set(E);
getch();
return 0;
}
Bạn đang đọc truyện trên: AzTruyen.Top