Code chỉ mang tính chất tham khảo.
Trang web không kèm tính năng quét khối và sao chép.#include <stdio.h>
#include <conio.h>
#include <iostream.h>
#include <fstream.h>
#include <string.h>
using namespace std;
class SinhVien{
private:
char masv[10];
char tensv[100];
int namsinh;
public:
void NhapTT(char* = "");
void InTT(char* = "");
friend ostream& operator << (ostream& os, SinhVien A);
friend istream& operator >> (istream& is, SinhVien& A);
};
void SinhVien::NhapTT(char* str){
cout<<str;
cout<<"Ma sinh vien:";
cin>>masv;
cout<<"Ten sinh vien:";
cin.ignore();
cin.getline(tensv,49);
cout<<"Nam sinh:";
cin>>namsinh;
}
void SinhVien::InTT(char* str){
cout<<str;
cout<<"\n "<<masv<<" : "<<tensv<<" (NS:"<<namsinh<<")";
};
istream& operator >> (istream& is, SinhVien& A) {
is>>A.masv;
is.ignore();
is.getline(A.tensv,99);
is>>A.namsinh;
return is;
}
ostream& operator << (ostream& os, SinhVien A)
{
os <<A.masv<<endl<<A.tensv<<endl<<A.namsinh;
return os;
}
class LopHoc{
private:
char malop[10];
char* tenlop;
char nienkhoa[20];
int slsv;
SinhVien* dssv[200];
public:
LopHoc();
void NhapTT();
void InTT();
friend ostream& operator << (ostream& os, LopHoc A);
friend istream& operator >> (istream& is, LopHoc& A);
};
LopHoc::LopHoc(){
strcpy(malop,"");
tenlop = new char[50];
strcpy(nienkhoa,"");
slsv =0;
}
void LopHoc::NhapTT(){
cout<<"\n Ma Lop Hoc: ";
cin>>malop;
cout<<"TenLop Hoc";
cin.ignore();
cin.getline(tenlop,49);
cout<<"Nien khoa:";
cin.getline(nienkhoa,15);
cout<<"So luong sinh vien";
cin>>slsv;
for(int i=0; i<slsv;i++){
dssv[i]=new SinhVien();
dssv[i]->NhapTT();
}
}
void LopHoc::InTT(){
cout<<"\n Lop: "<<malop<<". Ten Lop "<<tenlop<<" ("<<nienkhoa<<"). So luong sv "<<slsv<<"";
cout <<"\n Danh sach sinh vien cua lop";
for(int i =0; i<slsv;i++){
dssv[i]->InTT();
}
}
istream& operator >> (istream& is, LopHoc& A) {
is>>A.malop;
is.ignore();
is.getline(A.tenlop,99);
is.getline(A.nienkhoa,15);
is>>A.slsv;
for(int i=0;i<A.slsv;i++){
A.dssv[i]=new SinhVien();
is>>(*A.dssv[i]);
}
return is;
}
ostream& operator << (ostream& os, LopHoc A)
{
os <<A.malop<<endl<<A.tenlop<<endl<<A.nienkhoa<<endl<<A.slsv;
for(int i =0;i< A.slsv;i++){
os<<" ";
os<<(*A.dssv[i]);
}
return os;
}
int main(){
LopHoc A;
cout<<"\nNhap thong tin:";
A.NhapTT();
cout<<"\nluu file";
ofstream f1("MyFile111111.Txt");
f1<<A;
f1.close();
cout<<"\n-----------\nLay tu file";
ifstream f2("MyFile111111.txt");
LopHoc B;
f2>>B;
cout<<"\n Thong Tin Sv luu trong file";
B.InTT();
f2.close();
getch();
}
NGUYỄN THIÊN TỨ
Nguồn: w3refer.com
Nguồn: w3refer.com
Quản lý thông tin lớp học, lưu dữ liệu ra file và đọc dữ liệu từ file bằng ngôn ngữ C++
4/
5
Oleh
Unknown
