C++ 学生类的排序

C++ 学生类的排序,第1张

#define _CRT_SECURE_NO_WARNINGS
#include 
#include
using namespace std;
class Student
{
	static int index;
public:
	long Num=0;
	string Name;
	int Score=0;
	Student() 
	{
		index++;
		printf("第%d条记录建立\n", index);
	}
	~Student()
	{
		printf("第%d条记录销毁\n", index);
		index--;
	}

};
int Student::index = 0;
int main()
{
	const int N = 6;
	Student* p = new Student[N];
	p[0].Num = 1001;
	p[0].Name ="张三";
	p[0].Score = 90;

	p[1].Num = 1002;
	p[1].Name = "张四";
	p[1].Score = 99;

	p[2].Num = 1003;
	p[2].Name = "张五";
	p[2].Score = 70;

	p[3].Num = 1004;
	p[3].Name = "张六";
	p[3].Score = 80;

	p[4].Num = 1005;
	p[4].Name = "张七";
	p[4].Score = 60;

	p[5].Num = 1006;
	p[5].Name = "张八";
	p[5].Score = 88;

	int i, j;

	long Num;
	string Name;
	int Score;

	for (i = 0; i < N-1; i++)
	{
		for (j = 0; j < N - 1 - i; j++)
		{
			if (p[j].Score > p[j + 1].Score)
			{

				Num = p[j].Num;
				Name = p[j].Name;
				Score = p[j].Score;

				p[j].Num= p[j+1].Num;
				p[j].Name= p[j+1].Name;
				p[j].Score= p[j+1].Score;

				p[j+1].Num=Num;
				p[j+1].Name=Name;
				p[j+1].Score=Score;
				
			}
		}
	}
	for (i = 0; i < N; i++)
	{
		printf("%d\t%s\t%d\n",p[i].Num,p[i].Name.c_str(), p[i].Score);
	}

	delete[] p;
	return 0;
}

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/563242.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-02
下一篇 2022-04-02

发表评论

登录后才能评论

评论列表(0条)

保存