Bai 27: Viet lai bai 1 bang cach xay dung lop sinh vien
BAI GIAI
#include <iostream.h>
#include <iomanip.h>
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct sinhvien
{
char mssv[5];
char tensv[50];
char msmh[5];
char tenmh[50];
float dtb;
};
class QLSV
{
public:
void Nhapsv(sinhvien a[], int n);
void Insv(sinhvien a[], int n);
void Sapxepsv(sinhvien a[], int n);
void ThemCuoiDanhSach(sinhvien a[], int &n, sinhvien x);
void ThemVaoBatKy(sinhvien a[], int &n, sinhvien x, int vitri);
void ThemVaoBatKy_Soft(sinhvien a[], int &n, sinhvien x, int vitri);
void XoaBatKy_Soft(sinhvien a[], int &n, int vitri);
void TimKiemSinhVien(sinhvien a[], int n, sinhvien x);
};
void QLSV::Nhapsv(sinhvien a[], int n)
{
int i;
for(i=0;i<n;i++)
{
cout<<"\nNhap sinh vien thu: "<<i+1;
cout<<"\n\t+ Nhap ma sinh vien: ";
gets(a[i].mssv);
cout<<"\n\t+ Nhap ten sinh vien: ";
gets(a[i].tensv);
cout<<"\n\t+ Nhap ma mon hoc: ";
cin>>a[i].msmh;
cout<<"\n\t+ Nhap ten mon hoc: ";
gets(a[i].tenmh);
cout<<"\n\t+ Nhap diem trung binh: ";
cin>>a[i].dtb;
}
}
void QLSV::Insv(sinhvien a[], int n)
{
int i;
cout<<"\n"<<setw(15)<<"MA SINH VIEN"<<setw(20)<<"TEN SINH VIEN"<<setw(15)<<"MA MON
HOC"<<setw(15)<<"TEN MON HOC"<<setw(10)<<"DIEM TB";
cout<<"\n"<<setw(15)<<"============"<<setw(20)<<"============="<<setw(15)<<"=========="<<set
w(15)<<"==========="<<setw(10)<<"======";
for(i=0;i<n;i++)
{
cout<<"\n"<<setw(15)<<a[i].mssv<<setw(20)<<a[i].tensv<<setw(15)<<a[i].msmh<<setw(15)<<a[i].tenmh<<
setw(10)<<a[i].dtb;
}
}
void QLSV::Sapxepsv(sinhvien a[], int n)
{
int i,j;
sinhvien tg;
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i].dtb>a[j].dtb)
{
tg=a[i];
a[i]=a[j];
a[j]=tg;
}
}
}
}
void QLSV::ThemCuoiDanhSach(sinhvien a[], int &n, sinhvien x)
{
cout<<"\n\t+ Nhap ma sinh vien: ";
gets(x.mssv);
cout<<"\n\t+ Nhap ten sinh vien: ";
gets(x.tensv);
cout<<"\n\t+ Nhap ma mon hoc: ";
cin>>x.msmh;
cout<<"\n\t+ Nhap ten mon hoc: ";
gets(x.tenmh);
cout<<"\n\t+ Nhap diem trung binh: ";
cin>>x.dtb;
a[n] = x;
n++;
}
void QLSV::ThemVaoBatKy(sinhvien a[], int &n, sinhvien x, int vitri)
{
cout<<"\n\t+ Nhap ma sinh vien: ";
gets(x.mssv);
cout<<"\n\t+ Nhap ten sinh vien: ";
gets(x.tensv);
cout<<"\n\t+ Nhap ma mon hoc: ";
cin>>x.msmh;
cout<<"\n\t+ Nhap ten mon hoc: ";
gets(x.tenmh);
cout<<"\n\t+ Nhap diem trung binh: ";
cin>>x.dtb;
a[n]=a[vitri];
a[vitri] = x;
n++;
}
void QLSV::ThemVaoBatKy_Soft(sinhvien a[], int &n, sinhvien x, int vitri)
{
cout<<"\n\t+ Nhap ma sinh vien: ";
gets(x.mssv);
cout<<"\n\t+ Nhap ten sinh vien: ";
gets(x.tensv);
cout<<"\n\t+ Nhap ma mon hoc: ";
cin>>x.msmh;
cout<<"\n\t+ Nhap ten mon hoc: ";
gets(x.tenmh);
cout<<"\n\t+ Nhap diem trung binh: ";
cin>>x.dtb;