求大神帮忙 给个 最简单的程序 !
(1) 每个学生的学号和四门功课的成绩从键盘读入。
(2) 计算每个学生的总分、平均分。
(3) 按平均成绩进行排序,输出排序后的成绩单(包括学号、四门功课的成绩、总分、平均分),给出名次。如果分数相同,则名次并列,其他学生名次不变。
实验步骤与要求:
(1) 每个功能为一个独立的函数。
(2) 调试程序时,可先输入少量学生的成绩作为实验数据。如可输入3名学 生4门课成绩
#if 1 #include <stdio.h> typedef struct score { float subject1; float subject2; float subject3; float subject4; float totalScore; float averageScore; }SCO; typedef struct student { unsigned long id; unsigned int ranknum; SCO score; }STU; STU stus[100]; void getMes(int &stuNum) { int stuCount = 0; char sel; printf("进行学生信息录入:\n"); do { printf("请输入第%d个学生学号:",stuCount+1); scanf("%u",&(stus[stuCount].id)); while (!getchar()); printf("请输入该生成绩1:"); scanf("%f", &(stus[stuCount].score.subject1)); while (!getchar()); printf("请输入该生成绩2:"); scanf("%f", &(stus[stuCount].score.subject2)); while (!getchar()); printf("请输入该生成绩3:"); scanf("%f", &(stus[stuCount].score.subject3)); while (!getchar()); printf("请输入该生成绩4:"); scanf("%f", &(stus[stuCount].score.subject4)); while (!getchar()); printf("是否继续学生信息录入(y/n):"); scanf("%c", &sel); while (!getchar()); if ('n' == sel) { printf("共计录入%d人,结束录入\n",stuCount+1); stuNum = stuCount + 1; break; } stuCount++; } while (1); } void get_aver_total(int stuNum) { for (int i = 0; i < stuNum; ++i) { stus[i].score.totalScore = stus[i].score.subject1 + stus[i].score.subject2 + stus[i].score.subject3 + stus[i].score.subject4; stus[i].score.averageScore = stus[i].score.totalScore / 4; } } void rankByAverage(int stuNum) { STU stuTemp; for (int i = 0; i < stuNum - 1;++i) { for (int j = 0; j < stuNum - i;++j) { if (stus[j].score.averageScore < stus[j+1].score.averageScore) { stuTemp = stus[j]; stus[j] = stus[j+1]; stus[j+1] = stuTemp; } } } for (int i = 0; i < stuNum; ++i) { stus[i].ranknum = i + 1; if (stus[i].score.averageScore == stus[i + 1].score.averageScore) { stus[i + 1].ranknum = stus[i].ranknum; ++i; } } } void printMes(int stuNum) { printf("id\tsub1\tsub2\tsub3\tsub4\ttotal\tave\tNo\n"); for (int i = 0; i < stuNum; ++i) { printf("%u\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t%u\n", stus[i].id, stus[i].score.subject1, stus[i].score.subject2, stus[i].score.subject3 , stus[i].score.subject4, stus[i].score.totalScore, stus[i].score.averageScore , stus[i].ranknum); } } int main() { int stuNum; getMes(stuNum); //录入学生信息 get_aver_total(stuNum); //求平均分和总分 rankByAverage(stuNum); //排序,计算名次 printMes(stuNum); //输出信息 getchar(); } #endif