假设某年级有4个班,每班有45名同学。本学期有5门课程考试,每门课程成绩是百分制。假定每个同学的成绩记录包含:学号、姓名各门课程的成绩共7项,其中学号是一个10位的字符串,每个学生都有唯一的学号,并且这4个班的成绩分别放在4个数组中,完成以下操作要求: ⑴ 编写一个成绩生成函数,使用随机数方法,利用随机函数生成学生的各门课程的成绩(每门课程的成绩都是0∽100之间的整数),通过调用该函数生成全部学生的成绩; ⑵ 编写一个平均成绩计算函数,计算每个同学的平均成绩并保存在成绩数组中; ⑶ 用冒泡排序法对4个班的成绩按每个同学的平均成绩的以非递增方式进行班内排序; ⑷ 用选择排序法对4个班的成绩按每个同学的平均成绩的以非递增方式进行班内排序; ⑸ 对已按平均成绩排好序的4个班的同学的构造一个所有按平均成绩的以非递增方式排列的新的单链表; ⑹ 设计一个菜单,至少具有上述操作要求的基本功能。c语言课程设计
时间: 2024-03-21 10:44:15 浏览: 276
好的,我可以帮你设计一个基于 C 语言的成绩管理系统,包含上述所述功能。以下是一个示例代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#define CLASS_NUM 4
#define STUDENT_NUM 45
#define COURSE_NUM 5
#define MAX_ID_LEN 10
struct student {
char id[MAX_ID_LEN + 1];
char name[20];
int scores[COURSE_NUM];
float avg_score;
};
struct node {
struct student stu;
struct node *next;
};
struct class {
struct student students[STUDENT_NUM];
struct node *sorted_list;
};
// 随机生成学生的成绩
void generate_scores(struct student *stu) {
for (int i = 0; i < COURSE_NUM; i++) {
stu->scores[i] = rand() % 101;
}
}
// 计算学生的平均成绩
void calculate_avg_score(struct student *stu) {
float sum = 0.0;
for (int i = 0; i < COURSE_NUM; i++) {
sum += stu->scores[i];
}
stu->avg_score = sum / COURSE_NUM;
}
// 冒泡排序
void bubble_sort(struct student *students, int n) {
for (int i = 0; i < n-1; i++) {
for (int j = 0; j < n-i-1; j++) {
if (students[j].avg_score < students[j+1].avg_score) {
struct student temp = students[j];
students[j] = students[j+1];
students[j+1] = temp;
}
}
}
}
// 选择排序
void selection_sort(struct student *students, int n) {
for (int i = 0; i < n-1; i++) {
int max_idx = i;
for (int j = i+1; j < n; j++) {
if (students[j].avg_score > students[max_idx].avg_score) {
max_idx = j;
}
}
if (max_idx != i) {
struct student temp = students[i];
students[i] = students[max_idx];
students[max_idx] = temp;
}
}
}
// 创建一个新的节点
struct node *create_node(struct student stu) {
struct node *new_node = (struct node*)malloc(sizeof(struct node));
new_node->stu = stu;
new_node->next = NULL;
return new_node;
}
// 将学生按平均成绩插入到新链表中
void insert_sorted_list(struct node **head, struct student stu) {
struct node *new_node = create_node(stu);
if (*head == NULL) {
*head = new_node;
return;
}
struct node *prev = NULL;
struct node *curr = *head;
while (curr != NULL && curr->stu.avg_score >= new_node->stu.avg_score) {
prev = curr;
curr = curr->next;
}
if (prev == NULL) {
new_node->next = *head;
*head = new_node;
} else {
prev->next = new_node;
new_node->next = curr;
}
}
// 初始化班级成绩数据
void init_class(struct class *cls) {
for (int i = 0; i < STUDENT_NUM; i++) {
sprintf(cls->students[i].id, "202100%03d", i+1);
sprintf(cls->students[i].name, "Student%02d", i+1);
generate_scores(&cls->students[i]);
calculate_avg_score(&cls->students[i]);
}
}
// 计算并保存每个同学的平均成绩
void calculate_avg_scores(struct class *cls) {
for (int i = 0; i < STUDENT_NUM; i++) {
calculate_avg_score(&cls->students[i]);
}
}
// 按平均成绩对班级成绩进行排序
void sort_scores(struct class *cls, void (*sort_func)(struct student*, int)) {
for (int i = 0; i < STUDENT_NUM; i++) {
cls->students[i].avg_score = 0;
}
for (int i = 0; i < STUDENT_NUM; i++) {
calculate_avg_score(&cls->students[i]);
}
sort_func(cls->students, STUDENT_NUM);
// 构造新的按平均成绩排序的链表
cls->sorted_list = NULL;
for (int i = 0; i < STUDENT_NUM; i++) {
insert_sorted_list(&cls->sorted_list, cls->students[i]);
}
}
// 打印班级成绩信息
void print_class_scores(struct class cls) {
printf("学号\t姓名\t");
for (int i = 0; i < COURSE_NUM; i++) {
printf("课程%d\t", i+1);
}
printf("平均成绩\n");
for (int i = 0; i < STUDENT_NUM; i++) {
printf("%s\t%s\t", cls.students[i].id, cls.students[i].name);
for (int j = 0; j < COURSE_NUM; j++) {
printf("%d\t", cls.students[i].scores[j]);
}
printf("%.2f\n", cls.students[i].avg_score);
}
}
// 打印按平均成绩排序后的班级成绩信息
void print_sorted_class_scores(struct class cls) {
printf("学号\t姓名\t平均成绩\n");
struct node *curr = cls.sorted_list;
while (curr != NULL) {
printf("%s\t%s\t%.2f\n", curr->stu.id, curr->stu.name, curr->stu.avg_score);
curr = curr->next;
}
}
int main() {
// 初始化随机数种子
srand((unsigned int)time(NULL));
// 初始化班级数据
struct class classes[CLASS_NUM];
for (int i = 0; i < CLASS_NUM; i++) {
init_class(&classes[i]);
}
int choice;
do {
printf("\n请选择操作:\n");
printf("1. 生成成绩\n");
printf("2. 计算平均成绩\n");
printf("3. 冒泡排序\n");
printf("4. 选择排序\n");
printf("5. 构造新链表\n");
printf("6. 打印班级成绩\n");
printf("7. 打印按平均成绩排序后的班级成绩\n");
printf("0. 退出\n");
printf("请输入选项:");
scanf("%d", &choice);
switch (choice) {
case 1:
for (int i = 0; i < CLASS_NUM; i++) {
init_class(&classes[i]);
}
printf("成绩已生成。\n");
break;
case 2:
for (int i = 0; i < CLASS_NUM; i++) {
calculate_avg_scores(&classes[i]);
}
printf("平均成绩已计算。\n");
break;
case 3:
for (int i = 0; i < CLASS_NUM; i++) {
sort_scores(&classes[i], bubble_sort);
}
printf("冒泡排序已完成。\n");
break;
case 4:
for (int i = 0; i < CLASS_NUM; i++) {
sort_scores(&classes[i], selection_sort);
}
printf("选择排序已完成。\n");
break;
case 5:
for (int i = 0; i < CLASS_NUM; i++) {
sort_scores(&classes[i], bubble_sort);
}
printf("新链表已构造。\n");
break;
case 6:
for (int i = 0; i < CLASS_NUM; i++) {
printf("班级%d成绩如下:\n", i+1);
print_class_scores(classes[i]);
}
break;
case 7:
for (int i = 0; i < CLASS_NUM; i++) {
printf("班级%d按平均成绩排序后的成绩如下:\n", i+1);
print_sorted_class_scores(classes[i]);
}
break;
case 0:
printf("程序已退出。\n");
break;
default:
printf("无效的选项,请重新选择。\n");
break;
}
} while (choice != 0);
return 0;
}
```
该程序中包含了成绩生成、平均成绩计算、冒泡排序、选择排序、链表构造、打印成绩等多个功能,并通过菜单方式提供用户选择。你可以根据自己的需要进行修改和优化。
阅读全文