- Code:
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<dos.h>
#define I 20
#define J 10
typedef struct qhr{
char a[5];
int b;
}r;
typedef struct qhs{
int b;
char c[6];
}s;
typedef struct qhu{
char a[5];
int b;
char c[6];
}u;
typedef r array_r[100];
typedef s array_s[100];
void taor(void);
void taos(void);
void xem_r(void);
void xem_s(void);
void xem_u(void);
u lay_u(r ur,s us);
void nap_r(array_r mr,int *nr,r rnap);
void nap_s(array_s ms,int *ns,s snap);
void noi(array_r mr,array_s ms,int nr,int ns);
void noinho2chimuc(array_r mr,array_s ms,int nr,int ns);
int main(void)
{
int nr,ns,chon;
FILE *fu;
array_r mr; //bo nho chua r
array_s ms; //bo nho chua s
while(1){
clrscr();
printf("\n_________ menu____________");
printf("\n 1:tao R va S");
printf("\n 2:xem R");
printf("\n 3:xem S");
printf("\n 4:tien hanh noi R va S thanh U");
printf("\n 5:xem U");
printf("\n 6:thoat");
printf("\n chon viec: ");scanf("%d",&chon);
switch(chon){
case 1:taor();taos();break;
case 2:xem_r();break;
case 3:xem_s();break;
case 4:{ fu=fopen("u.db","wb");
fclose(fu);
noinho2chimuc(mr,ms,nr,ns);break;}
case 5:xem_u();break;
case 6:return 0;
default:printf("\n ban chon sai");
}
}
}
//tao file r.db
void taor(void)
{
FILE *f;
r tam;
int n,i=1,j=1,k=0;
f=fopen("r.db","wb");
while(i<=I)
{
n=random(20)+10;
for (j=1;j<n;j++)
{
for(k=0;k<4;k++) tam.a[k]=random(25)+65;tam.a[4]='\0';
tam.b=i;
fwrite(&tam,sizeof(r),1,f);
//printf("\n %s ",tam.a);
//printf("%d",tam.b);
}
i++;
}
fclose(f);
}
void taos(void)
{
FILE *f;
s tam;
int n,i=1,j=1,k=0;
f=fopen("s.db","wb");
while(i<=J)
{
n=random(20)+10;
for (j=1;j<n;j++)
{
for(k=0;k<5;k++) tam.c[k]=random(25)+65;tam.c[5]='\0';
tam.b=i;
fwrite(&tam,sizeof(s),1,f);
}
i++;
}
fclose(f);
}
void xem_r(void)
{
int n=0;
FILE *f;
r tam;
f=fopen("r.db","rb");
if(ferror(f)) {printf("\n co loi vao ra");exit;}
fseek(f,0,SEEK_SET);
while( !feof(f))
{
fread(&tam,sizeof(r),1,f);
printf("\n %s %d",tam.a,tam.b);
n++;
}
fclose(f);
printf("\n R co %d ban nghi",n);
getch();
}
void xem_s(void)
{
int n=0;
FILE *f;
s tam;
f=fopen("s.db","rb");
if(ferror(f)) printf("\n co loi vao ra");
fseek(f,0,SEEK_SET);
while( !feof(f))
{
fread(&tam,sizeof(s),1,f);
printf("\n %d %s",tam.b,tam.c);
n++;
}
fclose(f);
printf("\n S co %d ban nghi",n);
getch();
}
void xem_u(void)
{
int n=0;
FILE *f;
u tam;
f=fopen("u.db","rb");
if(ferror(f)) printf("\n co loi vao ra");
fseek(f,0,SEEK_SET);
while( !feof(f))
{
fread(&tam,sizeof(u),1,f);
printf("\n %s %d %s",tam.a,tam.b,tam.c);
n++;
}
fclose(f);
printf("\n U co %d ban nghi",n);
getch();
}
u lay_u(r ur,s us)
{
int i;
u tam;
for(i=0;i<5;i++)
tam.a[i]=ur.a[i];
tam.b=ur.b;
for(i=0;i<6;i++)
tam.c[i]=us.c[i];
return tam;
}
void nap_r(array_r mr,int *nr,r rnap)
{
(*nr)++;
mr[*nr]=rnap;
}
void nap_s(array_s ms,int *ns,s snap)
{
(*ns)++;
ms[*ns]=snap;
}
void noi(array_r mr,array_s ms,int nr,int ns) //noi trong bo nho
{
int i,j,n;
FILE *f;
u tam_u;
f=fopen("u.db","ab");
fseek(f,0,2); //DI CHUYEN VE CUOI FILE U
for(i=0;i<ns;i++)
for(j=0;j<nr;j++)
{
tam_u=lay_u(mr[j],ms[i]);n++;
fwrite(&tam_u,sizeof(u),1,f);
};
fclose(f);
}
void noinho2chimuc(array_r mr,array_s ms,int nr,int ns)
{
FILE *fr,*fs;
r tam_r;
s tam_s;
int i,k;
long vitris=0,vitrir=0;
ns=-1;
nr=-1;
fr=fopen("r.db","rb");
if(ferror(fr))
{printf("\n chua co du lieu");
exit;
}
fs=fopen("s.db","rb");
if(ferror(fs))
{printf("\n chua co du lieu");
exit;
}
while(!feof(fs))
{
ns=-1;
fseek(fs,vitris,0);
fread(&tam_s,sizeof(s),1,fs);
i=tam_s.b;
do
{
vitris=ftell(fs);
nap_s(ms,&ns,tam_s);
fread(&tam_s,sizeof(s),1,fs);
}
while((!feof(fs))&&(ns<100)&&(tam_s.b==i));
rewind(fr);
while(!feof(fr))
{
nr=-1;
do
{
fread(&tam_r,sizeof(r),1,fr);
nap_r(mr,&nr,tam_r);
}
while((!feof(fr))&&(nr< 110-ns)&&(tam_r.b==i));
noi(mr,ms,nr,ns);
}
}
fclose(fr);
fclose(fs);
}
Còn tiếp.....