#include<iostream.h>
#include<fstream.h>
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
struct thisinh{
char sbd[15];
char ht[150];
char dc[300];
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 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; }
}
//Ham xoa thi sinh theo so bao danh
//x la vi tri thi sinh muon xoa
int xoats(thisinh ds[], int N, char*sbd)
{
int x=timkiem(ds,sbd);
if(x==-1)
{
cout<<"***Khong ton tai sinh vien nay***"; return -1;
}
for(int i=0;i<sots;i++)
if(i==x){
ds[i]=ds[i+1];
sots--;
}
}
//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 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;
}
}
}
//Ham hieu chinh thong tin thi sinh
//x la vi tri thi sinh can doi thong tin
int hieuchinhthongtints(thisinh ds[],char*sbd,thisinh ts)
{
int x=timkiem(ds,ts.sbd);
if(x==-1)
{
cout<<"***Khong ton tai sinh vien nay***"; return -1;
}
float t,l,h;
cout<<"Nhap so bao danh muon doi: ";gets
(ts.
sbd); cout<<"Nhap ten muon doi: ";gets
(ts.
ht); cout<<"Nhap dia chi muon doi: ";gets
(ts.
dc); cout<<"Nhap diem toan muon doi:";cin>>t
; cout<<"Nhap diem ly muon doi:";cin>>l
; cout<<"Nhap diem hoa muon doi:";cin>>h
; ts.toan=t;ts.ly=l,ts.hoa=h;
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;
}
void xeptheosbd(thisinh ds[])
{
for(int i=0;i<sots-1;i++)
for(int j=i+1;j<sots;j++){
if (strcmpi(ds[i].sbd,ds[j].sbd)==1)
{
thisinh t = ds[i];
ds[i]=ds[j];
ds[j]=t;
}
}
}
//Ham tim kiem thi sinh bang phuong phap nhi phan
/*int tknhiphan(thisinh ds[],char*sbd,int left,int right)
{
if(left<=right)
{
int mid=(left+right)/2;
if(strcmpi(ds[mid].sbd,sbd)==0)
return mid;
else if(strcmpi(ds[mid].sbd,sbd)==1)
return tknhiphan(ds,sbd,left,mid-1);
else
return tknhiphan(ds,sbd,mid+1,right);
}
return -1;
}*/
//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];
readfromfile("c:\\data.data",ds);
do{
system("cls");
cout<<"\t----------Bai tap Labreport 1----------"<<endl
; cout<<"1.Nhap thong tin thi sinh."<<endl
; cout<<"2.In danh sach thi sinh vua nhap."<<endl
; cout<<"3.Xoa thi sinh khi biet so bao danh."<<endl
; cout<<"4.Tim thi sinh theo so bao danh."<<endl
; cout<<"5.Tim thi sinh theo ho ten."<<endl
; cout<<"6.Sap xep thi sinh theo ho ten."<<endl
; cout<<"7.Sap xep thi sinh theo tong diem."<<endl
; cout<<"8.Hieu chinh thong tin thi sinh."<<endl
; cout<<"9.Sap xep thi sinh theo so bao danh."<<endl
; //cout<<"10.Tim kiem thi sinh bang phuong phap nhi phan"<<endl;
cout<<"10.Luu file va Thoat..."<<endl
; cout<<"\n---Chon so tuong ung muc luc---: "; int 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); 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);
cout<<"Danh sach thi sinh sau khi xoa: "<<endl
; inds(ds);
getchar();
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 thay doi thong tin:"; gets(ts.sbd);
hieuchinhthongtints(ds,ts.sbd,ts);
cout<<"Danh sach sau khi chinh thong tin: "<<endl
; inds(ds);
getchar();
break;
case 9:
xeptheosbd(ds);
inds(ds);
getchar();
break;
/*case 10:
int k;
char*sbd;
cout<<"Nhap so bao danh ban muon tim: ";gets(ts.sbd);
k=tknhiphan(ds,sbd,0,sots);
if(k==-1)
cout<<"Ko tim thay "<<endl;
else
cout<<"Tim thay "<<ts.sbd<<" tai vi tri "<<k<<endl;
getchar();
break; */
case 10:
write2file("c:\\data.data",ds,sots);
cout<<"Du lieu da duoc luu vao file. \nLuu thanh cong"<<endl
; exit(1);
break;
}
}while(1);
return 1;
}