录入学生各科成绩和学分,系统计算加权平均成绩(精确到小数点后两位),并从高分到低分排序后输出学生学号(如遇平均分相同,则以第一门课程成绩高低排序,依次类推)。用c语言,输入成绩和学分的时候要怎么输入
时间: 2023-12-24 14:05:53 浏览: 62
你可以使用C语言中的结构体来存储学生的成绩和学分信息,然后通过计算加权平均成绩并排序输出。
以下是一个示例代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STUDENTS 100
// 学生结构体
struct Student {
char id[20]; // 学号
char name[20]; // 姓名
float scores[5]; // 成绩
float credits[5]; // 学分
float total_score; // 总成绩
};
// 计算加权平均成绩
float calculate_total_score(float scores[], float credits[]) {
float total_score = 0.0;
float total_credits = 0.0;
for (int i = 0; i < 5; i++) {
total_score += scores[i] * credits[i];
total_credits += credits[i];
}
return total_score / total_credits;
}
// 比较函数,用于排序
int cmp(const void *a, const void *b) {
struct Student *s1 = (struct Student *)a;
struct Student *s2 = (struct Student *)b;
if (s1->total_score != s2->total_score) {
return s1->total_score < s2->total_score ? 1 : -1;
} else {
for (int i = 0; i < 5; i++) {
if (s1->scores[i] != s2->scores[i]) {
return s1->scores[i] < s2->scores[i] ? 1 : -1;
}
}
return 0;
}
}
int main() {
struct Student students[MAX_STUDENTS];
int n;
// 输入学生信息
printf("请输入学生人数(最多%d人):", MAX_STUDENTS);
scanf("%d", &n);
for (int i = 0; i < n; i++) {
printf("请输入第%d个学生的信息:\n", i + 1);
printf("学号:");
scanf("%s", students[i].id);
printf("姓名:");
scanf("%s", students[i].name);
printf("成绩和学分(按照 课程1成绩 课程1学分 课程2成绩 课程2学分 ... 的顺序输入):");
for (int j = 0; j < 5; j++) {
scanf("%f%f", &students[i].scores[j], &students[i].credits[j]);
}
students[i].total_score = calculate_total_score(students[i].scores, students[i].credits);
}
// 排序并输出
qsort(students, n, sizeof(struct Student), cmp);
printf("学号\t姓名\t加权平均分\n");
for (int i = 0; i < n; i++) {
printf("%s\t%s\t%.2f\n", students[i].id, students[i].name, students[i].total_score);
}
return 0;
}
```
在上面的代码中,我们定义了一个 `Student` 结构体来存储学生信息,包括学号、姓名、成绩和学分。然后我们通过 `calculate_total_score` 函数计算每个学生的加权平均成绩,并将其存储在 `total_score` 字段中。最后,我们使用 `qsort` 函数对学生进行排序,并输出学号、姓名和加权平均分。在输入成绩和学分时,我们使用了 `scanf` 函数来读取输入。