#include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct { char name[20]; double averageScore; double moralityScore; double finalScore; } Student; int cmp(const void* a, const void* b) { Student* studentA = (Student*)a; Student* studentB = (Student*)b; return studentB->finalScore - studentA->finalScore; } int main() { FILE* inFile = fopen("rawscore.txt", "r"); if (!inFile) { fprintf(stderr, "无法打开文件\n"); return 1; } int studentCount, subjectCount; fscanf(inFile, "%d %d", &studentCount, &subjectCount); Student* students = (Student*)malloc(studentCount * sizeof(Student)); for (int i = 0; i < studentCount; i++) { fscanf(inFile, "%s", students[i].name); double sum = 0; for (int j = 0; j < subjectCount; j++) { double score; fscanf(inFile, "%lf", &score); sum += score; } students[i].averageScore = sum / subjectCount; fscanf(inFile, "%lf", &students[i].moralityScore); } double academicWeight, moralityWeight; printf("请输入专业课成绩与德育成绩之间的比例(如0.7):"); scanf("%lf", &academicWeight); moralityWeight = 1 - academicWeight; for (int i = 0; i < studentCount; i++) { students[i].finalScore = students[i].averageScore * academicWeight + students[i].moralityScore * moralityWeight; } qsort(students, studentCount, sizeof(Student), cmp); int scholarshipLevelCount; printf("请输入奖学金等级数:"); scanf("%d", &scholarshipLevelCount); for (int i = 1; i <= scholarshipLevelCount; i++) { int count; printf("请输入%d等奖的获奖人数:", i); scanf("%d", &count); printf("%d等奖获得者:\n", i); for (int j = 0; j < count; j++) { printf("%s %.2lf\n", students[j].name, students[j].finalScore); } memmove(students, students + count, (studentCount - count) * sizeof(Student)); studentCount -= count; } free(students); return 0; }
时间: 2024-03-15 20:42:01 浏览: 68
这段代码是用来计算学生的最终成绩并根据成绩给出奖学金等级的。程序首先读取一个文本文件,该文件包含了每个学生的姓名、各门课程的成绩以及德育成绩。然后程序通过输入专业课成绩与德育成绩之间的比例来计算每个学生的最终成绩,最终成绩由专业课成绩和德育成绩加权得出。程序使用快速排序算法对学生按照最终成绩进行排序,然后根据输入的奖学金等级数依次给出各个等级的获奖人数和获奖名单。最后释放动态分配的内存并退出程序。
相关问题
#include<stdio.h> #include<stdlib.h> #include<time.h> #include<string.h> typedef struct user_information { int flag; int name; char key[10]; struct user_information* next; }user;
这是一个定义了用户信息的结构体 user_information,并定义了一个别名 user。该结构体包含了用户的标识符 flag、用户名 name、密码 key 和指向下一个用户的指针 next。其中,flag 和 name 都是整型,key 是一个长度为 10 的字符串。这个结构体可以用于存储用户的信息,例如在一个用户管理系统中,可以用单链表来存储所有注册的用户信息,每个节点就是一个 user 结构体,包含了该用户的所有信息。
解释一下这段代码#include<stdio.h> #include<stdlib.h> #include<time.h> #include<string.h> typedef struct user_information { int flag; int name; char key[10]; struct user_information* next; }user;
这段代码定义了一个名为user_information的结构体类型,并且使用typedef给这个类型取了一个别名叫做user。这个结构体包含了四个成员变量:flag、name、key和next。其中flag和name都是int类型的,key是一个长度为10的字符串,next是一个指向下一个user结构体的指针。
这个结构体可能用于描述一个用户的信息,flag用于标记用户是否已经被删除,name是用户的编号或者姓名,key是用户的密码,next是指向下一个用户的指针,用于构建一个链表来存储多个用户的信息。这些信息可能会在一个用户管理系统或者登录系统中使用。
阅读全文
相关推荐

















