THIPTTKTT1

/********************************************************

- Phan tich va thiet ke thuat toan

- Bieu dien tap hop bang mang

- 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,int &na)

{

na=0;

}

// Lay hop cua hai tap hop A va B tao thanh tap hop C

void union_Set(Set A,int na,Set B,int nb,Set &C,int &nc)

{

int a=0,b=0;

while((a<na) && (b<nb))

{

if(A[a]<B[b])

{

C[nc]=A[a];

a++;

}

else if(A[a]>B[b])

{

C[nc]=B[b];

b++;

}

else

{

C[nc]=A[a];

a++;

b++;

}

nc++;

}

while(a<na)

{

C[nc]=A[a];

a++;

nc++;

}

while(b<nb)

{

C[nc]=B[b];

b++;

nc++;

}

}

// Lay giao cua hai tap hop A va B tao thanh tap hop C

void intersection_Set(Set A,int na,Set B,int nb,Set &C,int &nc)

{

int a=0,b=0;

while(a<na && b<nb)

{

if(A[a]<B[b]) a++;

else if(A[a]>B[b]) b++;

else

{

C[nc]=A[a];

nc++;

a++;

b++;

}

}

}

// Kiem tra xem mot phan tu co thuoc tap hop hay khong

// Return vi tri if thuoc, return -1 neu khong thuoc

int member_Set(int x,Set A,int na)

{

int i=0;

while(i<na && A[i]!=x)

i++;

if(i<na) return 1;

else return 0;

}

// Lay hieu cua hai tap hop A va B tao thanh tap hop C

void difference_Set(Set A,int na,Set B,int nb,Set &C,int &nc)

{

for(int a=0;a<na;a++)

if(!member_Set(A[a],B,nb))

{

C[nc]=A[a];

nc++;

}

}

// Ham chen mot phan tu vo tap hop

void insert_Set(int x,Set &A,int &na)

{

if(!member_Set(x,A,na))

{

A[na]=x;

na++;

}

}

// Hien thi cac phan tu cua tap hop ra man hinh

void show_Set(Set A,int na)

{

for(int i=0;i<na;i++)

printf("%4d",A[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

int na;

Set B; // Tap cac so le

int nb;

Set C; // Tap giao cua A va B

int nc;

Set D; // Tap hop cua A va B

int nd;

Set E; // Tap hieu cua A va B

int ne;

// Khoi tao cac tap hop A , B la cac tap rong

makenull_Set(A,na);

makenull_Set(B,nb);

makenull_Set(C,nc);

makenull_Set(D,nd);

makenull_Set(E,ne);

// Insert du lieu vo cac tap hop

for(int i=0;i<n;i++)

{

if(is_nguyento(i))

insert_Set(i,A,na);

if(is_le(i))

insert_Set(i,B,nb);

}

// Hien thi cac tap hop A va B

printf("

--------------------------------------

");

printf("

Tap hop cac so nguyen to A");

printf("

--------------------------------------

");

show_Set(A,na);

printf("

--------------------------------------

");

printf("

Tap hop cac so le B");

printf("

--------------------------------------

");

show_Set(B,nb);

// Giao cua hai tap hop

printf("

--------------------------------------

");

printf("

Giao cua hai tap hop");

printf("

--------------------------------------

");

intersection_Set(A,na,B,nb,C,nc);

show_Set(C,nc);

// Hop cua hai tap hop

printf("

--------------------------------------

");

printf("

Hop cua hai tap hop");

printf("

--------------------------------------

");

union_Set(A,na,B,nb,D,nd);

show_Set(D,nd);

// Hieu cua hai tap hop

printf("

--------------------------------------

");

printf("

Hieu cua hai tap hop");

printf("

--------------------------------------

");

difference_Set(A,na,B,nb,E,ne);

show_Set(E,ne);

getch();

return 0;

Bạn đang đọc truyện trên: TruyenTop.Vip

Tags: