01/10/2018, 11:38

Sắp xếp sinh viên theo điểm tăng dần sử dụng class trong C++

#include<iostream>
#include<string>
using namespace std;
int x[100];
class student{
	public:
		string name;
		string fullname;
		string a_class;
};
class scores:public student{
	private:
		float diem_toan;
		float diem_ly;
		float diem_hoa;
		float diem_anh;
	public:
		scores()
		{
			diem_toan = 0;
			diem_ly = 0;
			diem_hoa = 0;
			diem_anh = 0;
		}
		scores(float diem_toan, float diem_ly, float diem_hoa, float diem_anh)
		{
			this->diem_toan = diem_toan;
			this->diem_ly = diem_ly;
			this->diem_hoa = diem_hoa;
			this->diem_anh = diem_anh;
		}
		void input()
		{
			cout << "enter name: ";fflush(stdin);
			getline(cin, name);
			cout << "enter fullname: ";fflush(stdin);
			getline(cin,fullname);
			cout << "enter class: ";fflush(stdin);
			getline(cin,a_class);
			cout << "nhap diem toan: ";
			cin >> diem_toan;
			cout << "nhap diem ly: ";
			cin >> diem_ly;
			cout << "nhap diem hoa: ";
			cin >> diem_hoa;
			cout << "nhap diem anh:";
			cin >> diem_anh;
		}
		void outPut()
		{
			cout << "ten: " << name << endl;
			cout << "full name: " << fullname << endl;
			cout << "class: " << a_class << endl;
			cout << "diem toan: " <<  diem_toan << "	" <<"diem ly: " <<  diem_ly << "	" << "diem hoa: " << diem_hoa << "	" <<"diem anh: "<< diem_anh << endl;
		}
		float avg()
		{
			return (diem_toan + diem_ly + diem_hoa + diem_anh)/4;
		}
};
void swap(float num1, float num2)
{
	float temp;
	temp = num1;
	num1 = num2;
	num2 = temp;
}
int main()
{
	scores scores[1000];
	int length;
	cout << "enter length: ";
	cin >> length;
	for(int i=0; i<length; i++)
	{
		scores[i].input();
		scores[i].avg();
	}
	cout << endl;
	cout << "danh sach student : " << endl;
	for(int i = 0; i< length; i++)
	{
		scores[i].outPut();
		cout << "diem trung binh la: " << scores[i].avg() << endl;
	}
	cout << endl;
	cout << "sap xep theo thu tu tang dan avg la: " << endl;
	cout << endl;
	cout << "sau khi sap xep la: " << endl;
	for(int i = 0; i < length; i++)
	{
		for(int j = i+1; j < length; j++)
		{
			if(scores[i].avg() > scores[j].avg())
			{
				swap(scores[i].avg(),scores[j].avg());
			}
		}
	}
	for(int i = 0 ; i < length; i++)
	{
		scores[i].outPut();
		cout << "diem trung binh la: " << scores[i].avg() << endl;
	}
}

ae cho mình hỏi là sao mình sắp xếp mà k dc nhỉ

HK boy viết 13:49 ngày 01/10/2018

Format lại code bằng cách thêm 3 dấu ` vào đầu và cuối code, như thế này:

// code

hai viết 13:50 ngày 01/10/2018

thank you b nhé…

HK boy viết 13:48 ngày 01/10/2018
for(int i = 0; i < length; i++)
{
	for(int j = i+1; j < length; j++)
	{
		if(scores[i].avg() < scores[j].avg())
		{
			swap(scores[i].avg(),scores[j].avg());
		}
	}
}

Bạn xác định kĩ giúp mình là bạn muốn swap toàn bộ thông tin các điểm của phần tử score[i] hay là chỉ swap điểm trung bình score[i].avg() nhé.

hai viết 13:40 ngày 01/10/2018

mình muốn đổi chỗ các thông tin của sinh viên đó, ví dụ sinh viên 1 có điểm trung bình cao hơn sinh viên 2 thì hiện các thông tin của sinh viên 2 ra trước rồi mới hiện tất cả thông tin của sinh viên 1

HK boy viết 13:53 ngày 01/10/2018

Vậy bạn thử nghĩ xem đoạn trên đoạn swap bạn nên sửa như thế nào nhé.

hai viết 13:49 ngày 01/10/2018

ok mình làm dc rồi, cám ơn b nhé

Bài liên quan
0