1. 程式人生 > >有N個學生資訊(包括學號、姓名、成績),按照成績的高低順序輸出各學生的資訊

有N個學生資訊(包括學號、姓名、成績),按照成績的高低順序輸出各學生的資訊

有N個學生資訊(包括學號、姓名、成績),按照成績的高低順序輸出各學生的資訊
/*用結構體陣列存放學生資訊,採用冒泡法進行排序/

#include<stdio.h>
struct student //宣告結構體型別
{
	int num;
	char name[20];
	float score;
};
void sort(student *stu,int N);
int main()
{
	int N = 5;
	struct student stu[5] = { {10101,"lili",90},{10102,"mimi",87},{10103,"weiwei",91},
	                          {
10104,"huhu",89},{10105,"daidai",93} };//定義結構體陣列並初始化 sort(stu,N); return 0; } void sort(student *stu,int N) { struct student temp;//定義結構體變數temp,用作交換 int i, j; for (i = 0; i < N ; i++) { for (j = 0; j < N-i-1; j++) { if (stu[j].score > stu[j+1].score)//使用冒泡法從小到大進行排序 { temp = stu[j+1];
stu[j+1] = stu[j]; stu[j] = temp; } } printf("%6d%8s%6.2f\n",stu[i].num,stu[i].name,stu[i].score); } printf("\n"); }