Bài 2. Xây dựng lớp HOCSINH (Nhập từ bàn phím, xuất ra màn hình, ghi, đọc tệp)

Thứ hai - 21/03/2022 22:43
Đề bài: Sử dụng ngôn ngữ C++, thực hiện các công việc sau:
•    Xây dựng lớp HOCSINH gồm các thuộc tính: mã học sinh, họ và tên, tuổi, điểm môn toán, điểm môn văn và điểm môn anh.
•    Nhập danh sách các học sinh (ghi vào tệp: HOCSINH.DAT).
•    Tính điểm trung bình cho các đối tượng học sinh.
•    In ra danh sách các nhân sự theo thứ tự của tên.(đọc từ tệp: HOCSINH.DAT).
•    Có bao nhiêu học sinh loại: giỏi, khá, trung bình, không đạt ? 
(quy định: điểm trung bình >= 8.5: loại giỏi; 7.0 <= điểm trung bình <8.5: loại khá; 5.0 <= điểm trung bình <7.0: loại trung bình;  điểm trung bình < 5.0: không đạt)
Họ và tên: Vàng Văn Quyn
Nhóm: 01 (Bảo Thắng + Si Ma Cai)
Lớp: Tin A20-PH Lào Cai.
Môi trường: Visual Studio 2019 (Console)
* Không kiểm tra điểm nhập vào (0 <= điểm <= 10)
// Bai2.cpp : This file contains the 'main' function. Program execution begins and ends there.//#include <iostream>#include <iomanip>#include <fstream>using namespace std;class HOCSINH {private:	char maso[10], hotendem[30], ten[15];	int tuoi;	float toan, van, anh;public:	HOCSINH();	HOCSINH(string maso, string hotendem, string ten, int tuoi, float toan, float van, float anh);	void NHAP();	float DTB();	void INRA();	string getTen() { return this->ten; }};HOCSINH::HOCSINH() {	strcpy_s(this->maso, "");	strcpy_s(this->hotendem, "");	strcpy_s(this->ten, "");	this->tuoi = 0;	this->toan = 0;	this->van = 0;	this->anh = 0;}HOCSINH::HOCSINH(string maso, string hotendem, string ten, int tuoi, float toan, float van, float anh) {	strcpy_s(this->maso, maso.c_str());	strcpy_s(this->hotendem, hotendem.c_str());	strcpy_s(this->ten, ten.c_str());	this->tuoi = tuoi;	this->toan = toan;	this->van = van;	this->anh = anh;}void HOCSINH::NHAP() {	cout << "Ma so: "; cin.getline(this->maso, 10); //cin.ignore();	cout << "Ho va ten dem: "; cin.getline(this->hotendem, 30);// cin.ignore();	cout << "Ten: "; cin.getline(this->ten, 15); //cin.ignore();	cout << "Tuoi: "; cin >> tuoi;	cout << "Diem toan: "; cin >> this->toan; //cin.ignore();	cout << "Diem van: "; cin >> this->van; //cin.ignore();	cout << "Diem T. Anh: "; cin >> this->anh; //cin.ignore();	}float HOCSINH::DTB() {	float tb = 0;	tb = this->toan + this->van + this->anh;	tb = tb / 3.0;	return tb;}void HOCSINH::INRA() {	cout << "Hoc sinh: " << setw(10) << this->maso << setw(30) << this->hotendem << setw(15) << this->ten << setw(5) << this->tuoi		<< setw(8) << setprecision(2) << fixed << this->toan << setw(8) << setprecision(2) << fixed << this->van		<< setw(8) << setprecision(2) << fixed << this->anh << setw(8) << setprecision(2) << fixed << this->DTB() << endl;}class DSHOCSINH {private:		void SAPXEP(HOCSINH* ds, int soluonghocsinh, bool sapxepgiamdan);	void XEPLOAI(int gioi, int kha, int tb, int kdat) {		cout << "Gioi: " << gioi << setw(20) << "Kha: " << kha << setw(20) << "Tb: " << tb << setw(20) << "Khong dat: " << kdat;	}public:	void NHAP();	void INRA();};void DSHOCSINH::NHAP() {	ofstream fout;		HOCSINH hs=HOCSINH();	fout.open("HOCSINH.DAT", ios::out);	char ok;		while (1) {		hs.NHAP();					fout.write((char*)&hs, sizeof(hs));		if (fout.good()) {			cout << "Da ghi thanh cong! ";		}		cout << "Co nhap tiep khong (C/K)?"; cin >> ok; cin.ignore();		if ((ok == 'k') || (ok == 'K')) {			break;		}	}	fout.close();}void DSHOCSINH::SAPXEP(HOCSINH* ds, int soluonghocsinh, bool sapxepgiamdan) {	int i = soluonghocsinh;	int j1 = 0;	HOCSINH temp;	int j2 = 0;	for (j1 = 0; j1 < i; j1++) {				for (j2 = 0; j2 < i; j2++) {			if (sapxepgiamdan)			{				if (ds[j1].getTen() < ds[j2].getTen()) {					temp = ds[j1];					ds[j1] = ds[j2];					ds[j2] = temp;				}			}			else {				if (ds[j1].getTen() > ds[j2].getTen()) {					temp = ds[j1];					ds[j1] = ds[j2];					ds[j2] = temp;				}			}		}	}	}void DSHOCSINH::INRA() {	ifstream fin;	int i = 0;	HOCSINH *ds = new HOCSINH[100];	fin.open("HOCSINH.DAT", ios::in);	if (fin.fail()) {		cout << "Mo tep bi loi!";		return;	}	int gioi = 0, kha = 0, tb = 0, khongdat = 0;	HOCSINH hs;	while (fin.read((char*)&hs, sizeof(hs))) {		ds[i] = hs;				i++;			}	fin.close();			this->SAPXEP(ds,i, true);//Tang dan		int j1 = 0;		for (j1 = 0; j1 < i; j1++) {				//bat dau xep loai            		if (ds[j1].DTB() >= 8.5) {			gioi++;		}		else if ((ds[j1].DTB() >= 7.0) && (ds[j1].DTB() < 8.5)) {			kha++;		}		else if ((ds[j1].DTB() >= 5.0) && (ds[j1].DTB() < 7.0)) {			tb++;		}		else {			khongdat++;		}		//Ket thuc xep loai		ds[j1].INRA();	}		this->XEPLOAI(gioi, kha, tb, khongdat);}int main(){	DSHOCSINH hs = DSHOCSINH();	hs.NHAP();	hs.INRA();}
 * Có kiểm tra điểm nhập vào (0 <= điểm <= 10)
// Bai2.cpp : This file contains the 'main' function. Program execution begins and ends there.//#include <iostream>#include <iomanip>#include <fstream>using namespace std;class HOCSINH {private:	char maso[10], hotendem[30], ten[15];	int tuoi;	float toan, van, anh;	bool Kiemtranhapdiem(float diem) {		if ((diem >= 0) && (diem <= 10)) {			return true;		}		else {			cout << "Thong bao loi, chi duoc nhap gia tri: 0 <= " << "Gia tri nhap vao phai" << " <= 10" << endl;			return false;		}	}public:	HOCSINH();	HOCSINH(string maso, string hotendem, string ten, int tuoi, float toan, float van, float anh);	void NHAP();	float DTB();	void INRA();	string getTen() { return this->ten; }};HOCSINH::HOCSINH() {	strcpy_s(this->maso, "");	strcpy_s(this->hotendem, "");	strcpy_s(this->ten, "");	this->tuoi = 0;	this->toan = 0;	this->van = 0;	this->anh = 0;}HOCSINH::HOCSINH(string maso, string hotendem, string ten, int tuoi, float toan, float van, float anh) {	strcpy_s(this->maso, maso.c_str());	strcpy_s(this->hotendem, hotendem.c_str());	strcpy_s(this->ten, ten.c_str());	this->tuoi = tuoi;	this->toan = toan;	this->van = van;	this->anh = anh;}void HOCSINH::NHAP() {	cout << "Ma so: "; cin.getline(this->maso, 10); //cin.ignore();	cout << "Ho va ten dem: "; cin.getline(this->hotendem, 30);// cin.ignore();	cout << "Ten: "; cin.getline(this->ten, 15); //cin.ignore();	cout << "Tuoi: "; cin >> tuoi;	do {		cout << "Diem toan: "; cin >> this->toan; //cin.ignore();	} while (!this->Kiemtranhapdiem(this->toan));	do {		cout << "Diem van: "; cin >> this->van; //cin.ignore();	} while (!this->Kiemtranhapdiem(this->van));	do {		cout << "Diem T. Anh: "; cin >> this->anh; //cin.ignore();		} while (!this->Kiemtranhapdiem(this->anh));}float HOCSINH::DTB() {	float tb = 0;	tb = this->toan + this->van + this->anh;	tb = tb / 3.0;	return tb;}void HOCSINH::INRA() {	cout << "Hoc sinh: " << setw(10) << this->maso << setw(30) << this->hotendem << setw(15) << this->ten << setw(5) << this->tuoi		<< setw(8) << setprecision(2) << fixed << this->toan << setw(8) << setprecision(2) << fixed << this->van		<< setw(8) << setprecision(2) << fixed << this->anh << setw(8) << setprecision(2) << fixed << this->DTB() << endl;}class DSHOCSINH {private:		void SAPXEP(HOCSINH* ds, int soluonghocsinh, bool sapxepgiamdan);	void XEPLOAI(int gioi, int kha, int tb, int kdat) {		cout << "Gioi: " << gioi << setw(20) << "Kha: " << kha << setw(20) << "Tb: " << tb << setw(20) << "Khong dat: " << kdat;	}public:	void NHAP();	void INRA();};void DSHOCSINH::NHAP() {	ofstream fout;		HOCSINH hs=HOCSINH();	fout.open("HOCSINH.DAT", ios::out);	char ok;		while (1) {		hs.NHAP();					fout.write((char*)&hs, sizeof(hs));		if (fout.good()) {			cout << "Da ghi thanh cong! ";		}		cout << "Co nhap tiep khong (C/K)?"; cin >> ok; cin.ignore();		if ((ok == 'k') || (ok == 'K')) {			break;		}	}	fout.close();}void DSHOCSINH::SAPXEP(HOCSINH* ds, int soluonghocsinh, bool sapxepgiamdan) {	int i = soluonghocsinh;	int j1 = 0;	HOCSINH temp;	int j2 = 0;	for (j1 = 0; j1 < i; j1++) {				for (j2 = 0; j2 < i; j2++) {			if (sapxepgiamdan)			{				if (ds[j1].getTen() < ds[j2].getTen()) {					temp = ds[j1];					ds[j1] = ds[j2];					ds[j2] = temp;				}			}			else {				if (ds[j1].getTen() > ds[j2].getTen()) {					temp = ds[j1];					ds[j1] = ds[j2];					ds[j2] = temp;				}			}		}	}	}void DSHOCSINH::INRA() {	ifstream fin;	int i = 0;	HOCSINH *ds = new HOCSINH[100];	fin.open("HOCSINH.DAT", ios::in);	if (!fin.good()) {		cout << "Mo tep bi loi!";		return;	}	int gioi = 0, kha = 0, tb = 0, khongdat = 0;	HOCSINH hs;	while (fin.read((char*)&hs, sizeof(hs))) {		ds[i] = hs;				i++;	}	fin.close();		this->SAPXEP(ds,i, true);//Tang dan		int j1 = 0;		for (j1 = 0; j1 < i; j1++) {				//bat dau xep loai            		if (ds[j1].DTB() >= 8.5) {			gioi++;		}		else if ((ds[j1].DTB() >= 7.0) && (ds[j1].DTB() < 8.5)) {			kha++;		}		else if ((ds[j1].DTB() >= 5.0) && (ds[j1].DTB() < 7.0)) {			tb++;		}		else {			khongdat++;		}		//Ket thuc xep loai		ds[j1].INRA();	}		this->XEPLOAI(gioi, kha, tb, khongdat);}int main(){	DSHOCSINH hs = DSHOCSINH();	hs.NHAP();	hs.INRA();}

Tác giả: Vàng Văn Quyn

Chú ý: Việc đăng lại bài viết trên ở website hoặc các phương tiện truyền thông khác mà không ghi rõ nguồn https://365.io.vn là vi phạm bản quyền

Tổng số điểm của bài viết là: 1 trong 1 đánh giá

Xếp hạng: 1 - 1 phiếu bầu
Click để đánh giá bài viết
Thống kê
  • Đang truy cập31
  • Hôm nay1,655
  • Tháng hiện tại8,701
  • Tổng lượt truy cập9,200,184
Bạn đã không sử dụng Site, Bấm vào đây để duy trì trạng thái đăng nhập. Thời gian chờ: 60 giây