輸入壹個班10個學生的學號和每個學生考試三門功課(數學、英語、計算機基礎)的成績。編程計算出每個學生的總分和平均分,並按學生成績優劣排序,最後打印壹張按高分到低分名次排序的成績單。要求:
1)排序用壹個函數實現。
2)打印的成績單表項包括:序號,學號、數學、英語、計算機、總分、平均分。
3)按實驗報告電子模板格式填寫實驗內容。
實驗目的
源程序清單:
(調試好的源程序代碼)
#include <stdio.h>
#include <stdlib.h>
#define STU_NUM 10 /*宏定義學生的數量*/
struct student /*定義壹個結構體用來存放學生學號、三門課成績、總分及平均成績*/
{
char stu_id[20]; /*學生學號;*/
float score[3]; /*三門課成績;*/
float total; /*總成績;*/
float aver; /*平均成績;*/
};
/*排序用壹個函數來實現*/
void SortScore(student *stu,int n)
{
student stud;
for(int i = 0; i < n-1; i++)
for(int j = i+1 ; j < n; j++)
{
if(stu[i].total < stu[j].total)
{
stud = stu[i];
stu[i] = stu[j];
stu[j] = stud;
}
}
}
int main( )
{
student stu[STU_NUM]; /*創建結構體數組中有10個元素,分別用來保存這10個人的相關信息。*/
/*輸入這十個學生的相關信息*/
for(int i = 0; i<STU_NUM; i++)
{
printf("請輸入第%d個學生的學號:",i+1);
scanf("%s",&stu[i].stu_id);
printf("輸入第%d個學生的數學成績:",i+1);
scanf("%f",&stu[i].score[0]);
printf("輸入第%d個學生的英語成績:",i+1);
scanf("%f",&stu[i].score[1]);
printf("輸入第%d個學生的計算機成績:",i+1);
scanf("%f",&stu[i].score[2]);
stu[i].total = stu[i].score[0]+stu[i].score[1]+stu[i].score[2];
stu[i].aver = stu[i].total/3;
}
printf("\n");
SortScore(stu,STU_NUM);/*調用排序函數*/
/*輸出排序後的各學生的成績*/
for(i = 0 ; i < STU_NUM; i++)
{
printf("序號: %d\t",i);
printf("學號:%s\t",stu[i].stu_id);
printf("數學:%f\t",stu[i].score[0]);
printf("英語:%f\t",stu[i].score[1]);
printf("計算機:%f\t",stu[i].score[2]);
printf("平均成績:%f\t",stu[i].aver);
printf("總分:%f\t",stu[i].total);
printf("\n\n");
}
return 0;
}
主要標識符說明:
(源程序中主要標識符含義說明)
#define STU_NUM 10 /*宏定義學生的數量*/
struct student /*定義壹個結構體用來存放學生學號、三門課成績、總分及平均成績*/
{
char stu_id[20]; /*學生學號;*/
float score[3]; /*三門課成績;*/
float total; /*總成績;*/
float aver; /*平均成績;*/
};