Code:
#include<iostream.h>
#include<fstream.h>
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
struct thisinh{
char sbd[10];
char ht[100];
char dc[200];
float toan;
float ly;
float hoa;
};
int N=100;
int sots=0;
//Ham tim kiem thi sinh trong danh sach
int timkiem(thisinh ds[],char *sbd)
{
for(int i=0;i<sots;i++)
if(strcmpi(ds[i].sbd,sbd)==0)
return i;
return -1;
}
//Ham tim kiem thi sinh theo so bao danh trong danh sach thi sinh
int timkiemsbd(thisinh ds[],char * sbd)
{
for(int i=0;i<sots;i++){
if(strcmpi(ds[i].sbd,sbd)==0){
cout<<"--Thong tin thi sinh co so bao danh "<<ds[i].sbd<<" la: "<<endl;
cout<<"+So bao danh: "<<ds[i].sbd<<endl;
cout<<"+Ho va ten: "<<ds[i].ht<<endl;
cout<<"+Dia chi: "<<ds[i].dc<<endl;
cout<<"+Diem toan: "<<ds[i].toan<<"\tLy:"<<ds[i].ly<<"\tHoa:"<<ds[i].hoa;
}
}
return -1;
}
//Ham them thi sinh vao danh sach thi sinh
int themts(thisinh ds[],thisinh ts)
{
if(timkiem(ds,ts.sbd)==-1)
{
ds[sots]=ts;
sots++;
return 1;
}
return -1;
}
//Ham in danh sach thi sinh
void inds(thisinh ds[])
{
for(int i=0;i<sots;i++){
cout<<"--------------------------------------------------------------\n";
cout<<"So bao danh: "<<ds[i].sbd<<endl;
cout<<"Ho va ten: "<<ds[i].ht<<endl;
cout<<"Dia chi: "<<ds[i].dc<<endl;
cout<<"Diem toan: "<<ds[i].toan<<"\tLy:"<<ds[i].ly<<"\tHoa:"<<ds[i].hoa;
cout<<endl;
}
}
//Ham xoa thi sinh theo so bao danh
int xoats(thisinh ds[], int N, char*sbd)
{
int x=timkiem(ds,sbd);
if(x==1)
{
cout<<"Khong co thi sinh co so bao danh nay";
return -1;
}
for(int i=0;i<sots;i++)
ds[i]=ds[i+1];
sots--;
}
//Ham tim kiem thi sinh theo ten trong danh sach
int timkiemten(thisinh ds[],char * ht)
{
for(int i=0;i<sots;i++){
if(strcmpi(ds[i].ht,ht)==0){
cout<<"--Thong tin thi sinh co ho ten "<<ds[i].ht<<" la: "<<endl;
cout<<"+So bao danh: "<<ds[i].sbd<<endl;
cout<<"+Ho va ten: "<<ds[i].ht<<endl;
cout<<"+Dia chi: "<<ds[i].dc<<endl;
cout<<"+Diem toan: "<<ds[i].toan<<"\tLy:"<<ds[i].ly<<"\tHoa:"<<ds[i].hoa;
}
}
return -1;
}
//Ham xep thi sinh theo ten
//Copy tu blog cua thay Hai
void xeptheoten(thisinh ds[])
{
for(int i=0;i<sots-1;i++)
for(int j=i+1;j<sots;j++){
if (strcmpi(ds[i].ht,ds[j].ht)==1)
{
thisinh t = ds[i];
ds[i]=ds[j];
ds[j]=t;
}
}
}
//Ham xep thi sinh theo diem
void xeptheodiem(thisinh ds[])
{
for(int i=0;i<sots-1;i++)
for(int j=i+1;j<sots;j++){
float diem1=ds[i].toan+ds[i].ly+ds[i].hoa;
float diem2=ds[j].toan+ds[j].ly+ds[j].hoa;
if (diem1>diem2)
{
thisinh t = ds[i];
ds[i]=ds[j];
ds[j]=t;
}
}
}
int suaTTSV(thisinh ds[],int n,char*sbd,thisinh ts)
{
int x=timkiem(ds,ts.sbd);
if(x==-1)
{
cout<<"***Khong ton tai sinh vien nay***";
return -1;
}
ds[x].sbd=ts.sbd;
ds[x].ht=ts.ht;
ds[x].dc=ts.dc;
ds[x].toan=ts.toan;
ds[x].ly=ts.ly;
ds[x].hoa=ts.hoa;
}
//ham ghi du lieu xuong file
//Ham nay duoc copy tu blog cua thay Hai
void write2file(char * filename, thisinh ds[],int n){
ofstream fs(filename, ios::binary | ios::out );//| ios::app
for(int i=0;i<n;i++){
fs.write((char*)&ds[i],sizeof(ds[i]));
}
fs.close();
}
//hàm doc du lieu tu file
//Ham nay duoc copy tu blog cua thay Hai
void readfromfile(char * filename, thisinh ds[]){
ifstream infile(filename,ios::in|ios::binary);
thisinh ts;
infile.read((char*)&ts,sizeof(thisinh));
sots=0;
while(!infile.eof()){
ds[sots]=ts;
sots++;
infile.read((char*)&ts,sizeof(thisinh));
}
infile.close();
}
int main()
{
thisinh ds[N];
cout<<"Loading data...\n";
readfromfile("c:\\data.data",ds);
cout<<"loading completed with "<<sots<<" records.\n";
do{
system("cls");
cout<<"\tBai tap Labreport"<<endl;
cout<<"1.Nhap thong tin thi sinh."<<endl;
cout<<"2.in ds."<<endl;
cout<<"3.Xoa thi sinh khi biet so bao danh."<<endl;
cout<<"4.Tim theo so bao danh."<<endl;
cout<<"5.Tim theo ho ten."<<endl;
cout<<"6.Sap xep theo ten thi sinh."<<endl;
cout<<"7.Sap xep theo diem thi sinh."<<endl;
cout<<"8.Hieu chinh thong tin thi sinh."<<endl;
cout<<"20.Luu file va Thoat..."<<endl;
cout<<"---Chon so tuong ung muc luc---: ";
int tl;
cin>>tl;
switch(tl)
{
case 1:
float t,l,h;
thisinh ts;
cout<<"Nhap so bao danh: ";gets(ts.sbd);
cout<<"Nhap ten: ";gets(ts.ht);
cout<<"Nhap dia chi: ";gets(ts.dc);
cout<<"Nhap diem toan:";cin>>t;
cout<<"Nhap diem ly:";cin>>l;
cout<<"Nhap diem hoa:";cin>>h;
ts.toan=t;ts.ly=l,ts.hoa=h;
themts(ds,ts);
break;
case 2:
inds(ds);
getchar();
break;
case 3:
cout<<"Nhap so bao danh mun xoa: ";
gets(ts.sbd);
xoats(ds,N,ts.sbd);
break;
case 4:
cout<<"Nhap so bao danh mun tim: ";
gets(ts.sbd);
timkiemsbd(ds,ts.sbd);
getchar();
break;
case 5:
cout<<"Nhap ho ten muon tim: ";
gets(ts.ht);
timkiemten(ds,ts.ht);
getchar();
break;
case 6:
xeptheoten(ds);
inds(ds);
getchar();
break;
case 7:
xeptheodiem(ds);
inds(ds);
getchar();
break;
case 8:
cout<<"Nhap so bao danh thi sinh muon tim:";
gets(ts.sbd);
suaTTSV(ds,sots,ts.sbd,ts);
getchar();
break;
case 20:
write2file("c:\\data.data",ds,sots);
cout<<"Du lieu da duoc luu vao file. \nFinished"<<endl;
exit(1);
break;
}
}while(1);
return 1;
}
Hàm hiệu chỉnh thông tin thí sinh bị lỗi ko thấy thông tin để sửa...hic mọi người xem hộ