Mng giúp phần sắp xếp tên sinh viên theo thứ tự alphabet và ghi vào tệp sv.dat(dạng nhị phân) với
#include<bits/stdc++.h>
using namespace std;
typedef struct
{
string msv;
char hoten[30];
char ho[10];
char dem[15];
char ten[10];
string ngaysinh;
string gioitinh;
float dtb;
}SinhVien;
typedef SinhVien sv[100];
void nhap(SinhVien m[], int &n)
{
float tg;
char s[2]=" ";
n=0;
string p="0";
do
{
cout << "Nhap ma sinh vien: ";
fflush(stdin);
getline(cin,m[n].msv);
if(p.compare(m[n].msv)!=0)
{
cout << "Nhap ho: ";
gets(m[n].ho);
cout << "Nhap ten dem: ";
gets(m[n].dem);
cout << "Nhap ten: ";
gets(m[n].ten);
strcpy(m[n].hoten,m[n].ho);
strcat(m[n].hoten,s);
strcat(m[n].hoten,m[n].dem);
strcat(m[n].hoten,s);
strcat(m[n].hoten,m[n].ten);
cout << "Nhap ngay sinh: ";
getline(cin,m[n].ngaysinh);
cout << "Gioi tinh: ";
getline(cin,m[n].gioitinh);
cout << "Diem trung binh: ";
cin >> tg;
m[n].dtb=tg;
n++;
cout << "===================================" << endl;
}
}while(p.compare(m[n].msv)!=0);
}
void prt()
{
cout << setw(15) << "Ma sinh vien" << setw(30) << "Ho va ten" << setw(15) << "Ngay sinh" << setw(15) << "Gioi tinh" << setw(15) << "Diem tb" << endl;
}
void hien(SinhVien m[],int n)
{
int i;
prt();
for(i=0;i<n;i++)
{
cout << setw(15) << m[i].msv << setw(30) << m[i].hoten << setw(15) << m[i].ngaysinh << setw(15) << m[i].gioitinh << setw(15) << m[i].dtb << endl;
}
}
void sapxep(SinhVien m[],int n){
int i,j;
for(i=0;i<n-1;i++){
for(j=n+1;j<n;j++){
if(strcmpi(m[i].hoten,m[j].hoten)<0){
SinhVien a= m[i];
m[i]=m[j];
m[j]=a;
}
}
}
hien(m,n);
}
void timkiem(SinhVien m[], int n)
{
int i,dem=0;
string p="nu";
float min=INT_MAX;
for(i=0;i<n;i++)
{
if(m[i].dtb<min && p.compare(m[i].gioitinh)==0)
{
min=m[i].dtb;
}
}
prt();
for(i=0;i<n;i++)
if(m[i].dtb==min && p.compare(m[i].gioitinh)==0){
cout << setw(15) << m[i].msv << setw(30) << m[i].hoten << setw(15) << m[i].ngaysinh << setw(15) << m[i].gioitinh << setw(15) << m[i].dtb << endl;
dem++;
}
if(dem==0) cout << "Khong tim thay sinh vien nu nao!" << endl;
}
void sl(SinhVien m[], int n){
int i,dem=0;
string p="nam";
for(i=0;i<n;i++)
if(p.compare(m[i].gioitinh)==0)
dem++;
if(dem>0) cout << "Co " << dem << " sinh vien nam!";
else cout << "Khong co sinh vien nam nao!";
}
int main()
{
int n;
sv a;
menu:
system("cls");
cout<<"===================== MENU ====================="<<endl;
cout<<"|| Moi ban chon: ||"<<endl;
cout<<"|| 1. Nhap thong tin sinh vien ||"<<endl;
cout<<"|| 2. Hien danh sach ra man hinh ||"<<endl;
cout<<"|| 3. Sap xep danh sach va ghi vao tep sv.dat ||"<<endl;
cout<<"|| 4. Tim sinh vien nu co dtb thap nhat ||"<<endl;
cout<<"|| 5. Dem so luong sinh vien nam ||"<<endl;
cout<<"|| 0. Ket thuc. ||"<<endl;
cout<<"================================================"<<endl;
int c;
cout<<"Ban chon: ";
cin>>c;
switch (c)
{
case 1:
{
nhap(a,n);
system("pause");
goto menu;
}
case 2:
{
hien(a,n);
system("pause");
goto menu;
}
case 3:
{
sapxep(a,n);
system("pause");
goto menu;
}
case 4:
{
timkiem(a,n);
system("pause");
goto menu;
}
case 5:
{
sl(a,n);
system("pause");
goto menu;
}
default:
return 0;
}
}