给老师做得分数从高到低自动排序学生分数程序

#include <iostream>
#include <iomanip>       //包含setw()函数的头文件
using namespace std;
#define MAX_NAME_LENGTH 25	//姓名的最大长度
typedef struct
{
	int number;
	char Name[MAX_NAME_LENGTH];
	int score;
}STUDENT;
void Sort(STUDENT* J, int Num);
int main()
{
	int i;
	int STUDENT_NUM_PER_CLASS;
	cout << "请输入学生人数:";
	cin>>STUDENT_NUM_PER_CLASS;
	STUDENT *P = new STUDENT[STUDENT_NUM_PER_CLASS];
	//STUDENT P[STUDENT_NUM_PER_CLASS];
	for (i = 0; i < STUDENT_NUM_PER_CLASS; i++)
	{
		cout << "请输入学生的"<<i<<"的学号,姓名和分数:";
		cin >> P[i].number >> P[i].Name >> P[i].score;
	}
	STUDENT* J=P;
	Sort(J, STUDENT_NUM_PER_CLASS);
	cout << STUDENT_NUM_PER_CLASS<<"个学生从高到低"<<endl;
	for (i = 0; i < STUDENT_NUM_PER_CLASS; i++)
		cout << P[i].number <<" "<< P[i].Name<<" " << P[i].score<<endl;
	system("pause");
}
void Sort(STUDENT* J, int Num)
{
	STUDENT Temp;
	int i,j;
	for(i=0;i<Num-1;i++)
		for (j = i + 1; j < Num; j++)
		{
			if ((*(J+i)).score < (*(J + j)).score)
			{
				Temp = *(J + i);
				*(J + i) = *(J + j);
				*(J + j) = Temp;
			}
		}
}

在这里插入图片描述

链接:https://pan.baidu.com/s/1IVqg90DDKJ4etyOOH37-xw
提取码:352x

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章