You are on page 1of 3

#include <stdio.

h>
#include <string.h>

#define max 100


struct SINHVIEN{
char masv[10];
char hosv[30];
char tensv[20];
float d1,d2,d3,dtb;
};
/*
ghi cau truc vao file, file co nhieu sv
doc file dulieu
tim kiem theo ten: tim xem co ten trong lop
tim ten trong lop va tra ve gia tri cua sv
tim xem trong lop co bao nhieu nguoi ten la...

*/
void input_1_sv(SINHVIEN &sv);
void output_1_sv(SINHVIEN sv);
void ghi_sv_file(char filename[]);
void ghi_them_sv_file(char filename[]);
void doc_sv_file(char filename[],SINHVIEN sv[],int &n);
int tim_sv_theo_ten(SINHVIEN sv[],int n, char tensv[]);
void out_arr_sv(SINHVIEN sv[],int n);
int tim_theo_ten(SINHVIEN sv[],int n,char tensv[],SINHVIEN &a);
int main(int argc, char *argv[])
{
char tenfile[20];SINHVIEN sv[max];int n,chon;
do{
printf("\n1.tao file moi\n");
printf("2.ghi them vao file\n");
printf("3.doc file ra mang\n");
printf("4.xuat sv theo danh sach\n");
printf("5.tim kiem sv theo ten\n");
printf("6.tim kiem sv theo ten tra ve sv\n");
printf("chon cong viec:");scanf("%d",&chon);
switch(chon){
case 1:{
printf("nhap ten file can
ghi:");scanf("%s",tenfile);
ghi_sv_file(tenfile);
}break;
case 2:{
printf("nhap ten file can
ghi:");scanf("%s",tenfile);
ghi_them_sv_file(tenfile);
}break;
case 3:{
printf("nhap ten file can doc:");scanf("%s",tenfile);
doc_sv_file(tenfile,sv,n);
}break;
case 4:out_arr_sv(sv,n);break;
case 5:{
char tensv[20];
printf("nhap ten sv can
tim:");scanf("%s",&tensv);
if(tim_sv_theo_ten(sv,n,tensv)==1)
printf("co!!!!");
else
printf("k co!!!");
}break;
case 6:{
char tensv[20];
printf("nhap ten sv can
tim:");scanf("%s",&tensv);
SINHVIEN a;
if(tim_theo_ten(sv,n,tensv,a)==1)
output_1_sv(a);
else
printf("k co!!!");
}break;
}
}while(chon<10);
return 0;
}
void input_1_sv(SINHVIEN &sv){
printf("nhap masv:");scanf("%s",sv.masv);fflush(stdin);
printf("nhap ho lot:");gets(sv.hosv);fflush(stdin);
printf("nhap ten sv:");scanf("%s",sv.tensv);fflush(stdin);
printf("nhap d1,d2,d3:");scanf("%f%f%f",&sv.d1,&sv.d2,&sv.d3);
sv.dtb=(sv.d1+sv.d2+sv.d3)/3;
}
void output_1_sv(SINHVIEN sv){
printf("\n%s %s %s %f %f %f
%f",sv.masv,sv.hosv,sv.tensv,sv.d1,sv.d2,sv.d3,sv.dtb);
}
void ghi_sv_file(char filename[]){
FILE *f;SINHVIEN a;char tl;
f=fopen(filename,"wb");
do{
input_1_sv(a);
fwrite(&a,sizeof(SINHVIEN),1,f);fflush(stdin);
printf("muon nhap nua k?");scanf("%c",&tl);
}while(tl!='n'&&tl!='N');
fclose(f);
}
void ghi_them_sv_file(char filename[]){
FILE *f;SINHVIEN a;char tl;
f=fopen(filename,"ab");
do{
input_1_sv(a);
fwrite(&a,sizeof(SINHVIEN),1,f);fflush(stdin);
printf("muon nhap nua k?");scanf("%c",&tl);
}while(tl!='n'&&tl!='N');
fclose(f);
}
void doc_sv_file(char filename[],SINHVIEN sv[],int &n){
FILE *f;
f=fopen(filename,"rb");
n=0;
while(fread(&sv[n],sizeof(SINHVIEN),1,f)!=NULL)
n++;
fclose(f);
}
void out_arr_sv(SINHVIEN sv[],int n){
for(int i=0;i<n;i++)
output_1_sv(sv[i]);
}
int tim_sv_theo_ten(SINHVIEN sv[],int n, char tensv[]){
for(int i=0;i<n;i++)
if(strcmp(sv[i].tensv,tensv)==0)
return 1;
return 0;
}
int tim_theo_ten(SINHVIEN sv[],int n,char tensv[],SINHVIEN &a){
for(int i=0;i<n;i++)
if(strcmp(sv[i].tensv,tensv)==0){
a=sv[i];return 1;
}
return 0;
}

You might also like