Bai 27: Viet lai bai 1 bang cach xay dung lop sinh vien BAI GIAI #include #include #include #include #include #include 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);
Trang 1Bai 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);
Trang 2void 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)
Trang 3int 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)<<"=========="<<setw(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];
Trang 4} }
}
}
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: ";
Trang 5cout<<"\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;
Trang 6for (int i=n-1; i>vitri; i )
{
a[i] = a[i-1];
a[vitri] = x;
}
n++;
}
void QLSV::XoaBatKy_Soft(sinhvien a[], int &n, int vitri)
{
for (int i=vitri; i<n-1; i++)
a[i] = a[i+1];
n ;
}
void QLSV::TimKiemSinhVien(sinhvien a[], int n, sinhvien x)
{
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)<<"=========="<<setw(15)<
<"==========="<<setw(10)<<"=======";
for (int i=0; i<n; i++)
{
if (strcmp(a[i].tensv,x.tensv)==0) {
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;
Trang 7} else {
cout<<setw(15)<<"\n\tKhong tim thay sinh vien nay."; }
}
}
void main()
{
int n, chon,vitri;
char c;
QLSV sv;
sinhvien a[50],x;
clrscr();
do
{
cout<<"\nChon 1 de nhap diem sinh vien ";
cout<<"\nChon 2 de sap xep thong tin diem sinh vien "; cout<<"\nChon 3 de in thong tin diem sinh vien ";
cout<<"\nChon 4 de them thong tin diem sinh vien ";
cout<<"\nChon 5 de xoa thong tin diem sinh vien ";
cout<<"\nChon 6 de tim kiem thong tin diem sinh vien "; cout<<"\nChon 7 de thoat chuong trinh ";
cout<<"\nHay chon chuc nang: ";
Trang 8switch (chon)
{
case 1:
cout<<"\nNhap so sinh vien: ";
cin>>n;
sv.Nhapsv(a,n);
break;
case 2:
sv.Sapxepsv(a,n);
break;
case 3:
cout<<"\nIn danh sach diem sinh vien:"; sv.Insv(a,n);
break;
case 4:
//ThemCuoiDanhSach(a,n,x);
cout<<"\nChon vi tri de them diem sinh vien: "; cin>>vitri;
sv.ThemVaoBatKy_Soft(a,n,x,vitri);
//ThemVaoBatKy(a,n,x,vitri);
break;
case 5:
cout<<"\nChon vi tri de xoa diem sinh vien: "; cin>>vitri;
Trang 9break;
case 6:
cout<<"\nNhap ten sinh vien de tim kiem: ";
gets(x.tensv);
sv.TimKiemSinhVien(a,n,x);
break;
case 7:
cout<<"\nTiep tuc nua khong (c/k), khong tiep nua nhan n de thoat: "; c=getche();
} }while(c!='c' && c!='C');
getch();
}