Góp ý code ghi file bài này giùm mình nhé! cảm ơn mọi người
#pragma once
#include<fstream>
#include<iomanip>
#include<ctype.h>
#include<conio.h>
#include<iostream>
#include<stdlib.h>
using namespace std;
class hs
{
private:
char ten[100];
int namsinh;
float toan,ly,hoa,van,su,dia,anh,tin,td;
public:
void nhap();
void xuatten();
void xuatnamsinh();
void monthilai();
void ghifile();
float dtb();
int hanhdong();
hs(void);
~hs(void);
};
void hs::nhap()
{
cout<<"\nTen: ";
fflush(stdin);
gets(ten);
do{
cout<<"\nnam sinh: ";
cin>>namsinh;
if(namsinh<1900 || namsinh >2100) cout<<"\nkhong hop le! nhap lai!!";
}while(namsinh<1900 || namsinh >2100);
cout<<"\nnhap diem cac mon:";
do{
cout<<"\nToan: ";cin>>toan;
if(toan<0 || toan>10) cout<<"\nkhong hop le! nhap lai!!";
}while(toan<0 || toan>10);
do{
cout<<"\nLy: ";cin>>ly;
if(ly<0 || ly>10) cout<<"\nkhong hop le! nhap lai!!";
}while(ly<0 || ly>10);
do{
cout<<"\nhoa: ";cin>>hoa;
if(hoa<0 || hoa>10) cout<<"\nkhong hop le! nhap lai!!";
}while(hoa<0 || hoa>10);
do{
cout<<"\nvan: ";cin>>van;
if(van<0 || van>10) cout<<"\nkhong hop le! nhap lai!!";
}while(van<0 || van>10);
do{
cout<<"\nsu: ";cin>>su;
if(su<0 || su>10) cout<<"\nkhong hop le! nhap lai!!";
}while(su<0 || su>10);
do{
cout<<"\ndia: ";cin>>dia;
if(dia<0 || dia>10) cout<<"\nkhong hop le! nhap lai!!";
}while(dia<0 || dia>10);
do{
cout<<"\nanh: ";cin>>anh;
if(anh<0 || anh>10) cout<<"\nkhong hop le! nhap lai!!";
}while(anh<0 || anh>10);
do{
cout<<"\ntin: ";cin>>tin;
if(tin<0 || tin>10) cout<<"\nkhong hop le! nhap lai!!";
}while(tin<0 || tin>10);
ghifile();
}
int hs::hanhdong()
{
if(toan<5 || ly<5 || hoa<5 || van<5 || su<5||dia<5||anh<5||tin<5) return -1;
if(dtb()>7) return 1;
return 0;
}
void hs::xuatten()
{
cout<<ten;
}
void hs::monthilai()
{
cout<<"\n*nhung mon thi lai:";
if(toan<5) cout<<"\n\ttoan";
if(ly<5) cout<<"\n\tly";
if(hoa<5) cout<<"\n\thoa";
if(van<5) cout<<"\n\tvan";
if(su<5) cout<<"\n\tsu";
if(dia<5) cout<<"\n\tdia";
if(anh<5) cout<<"\n\tanh";
if(tin<5) cout<<"\n\ttin";
}
void hs::xuatnamsinh()
{
cout<<namsinh;
}
float hs::dtb()
{
return (toan+ly+hoa+van+su+dia+anh+tin)/8;
}
void hs::ghi_file()
{
fstream f;
f.open("SINHVIEN.TXT", ios::binary|ios::app|ios::out);
f.write((char*)this, sizeof(*this));
f.close();
}
hs::hs(void)
{
}
hs::~hs(void)
{
}
..
#pragma once
class danhsachhs
{
private:
hs x[1000];
int n;
float td;
public:
void nhap();
void xuatthilai();
void xuatthitotnghiep();
void xuatluanvan();
danhsachhs(void);
~danhsachhs(void);
};
void danhsachhs::nhap()
{
int k,i=1;
cout<<"\n*nhap hoc sinh 1";
x[0].nhap();
do{
cout<<"\n------menu-----";
cout<<"\n| 1.nhap tiep.|";
cout<<"\n| 0.ket thuc. |";
cout<<"\n---------------";
do{
cout<<"\nlua chon cua ban: ";
cin>>k;
}while(k!=1 && k!=0);
if(k==1)
{
cout<<"\n\t* nhap hoc sinh "<<i+1;
x[i++].nhap();
}
}while(k==1)
n=i;
}
void danhsachhs::xuatthilai()
{
cout<<"\n\t* DANH SACH THI LAI *\n";
int i,j=0;
for(i=0;i<n;i++)
if(x[i].hanhdong()==-1)
{
j++;
cout<<"\n"<<j<<". ";x[i].xuatten();cout<<" - ";x[i].xuatnamsinh();cout<<"\n";
x[i].monthilai();
}
if(j==0) cout<<"\n\tkhong co!";
}
void danhsachhs::xuatthitotnghiep()
{
cout<<"\n\t* DANH SACH THI TOT NGHIEP *\n";
int i,j=0;
for(i=0;i<n;i++)
if(x[i].hanhdong()==0)
{
j++;
cout<<"\n"<<j<<". ";x[i].xuatten();cout<<" - ";x[i].xuatnamsinh();cout<<"\n";
}
if(j==0) cout<<"\n\tkhong co!\n";
}
void danhsachhs::xuatluanvan()
{
cout<<"\n\t* DANH SACH LAM LUAN VAN TOT NGHIEP *\n";
int i,j=0;
for(i=0;i<n;i++)
if(x[i].hanhdong()==1)
{
j++;
cout<<"\n"<<j<<". ";x[i].xuatten();cout<<" - ";x[i].xuatnamsinh();cout<<"\n";
}
if(j==0) cout<<"\n\tkhong co!\n";
}
danhsachhs::danhsachhs(void)
{
}
danhsachhs::~danhsachhs(void)
{
}
void main()
{
danhsachhs x;
x.nhap();
cout<<"\n-------------------------------------------------------\n";
x.xuatluanvan();
cout<<"\n-------------------------------------------------------\n";
x.xuatthitotnghiep();
cout<<"\n-------------------------------------------------------\n";
x.xuatthilai();
hs a;
cout<<"\n ghi file";
a.ghifile();
system("pause");
}
83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?