#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 13:42:01 浏览: 34
这段代码是用来计算学生的最终成绩并根据成绩给出奖学金等级的。程序首先读取一个文本文件,该文件包含了每个学生的姓名、各门课程的成绩以及德育成绩。然后程序通过输入专业课成绩与德育成绩之间的比例来计算每个学生的最终成绩,最终成绩由专业课成绩和德育成绩加权得出。程序使用快速排序算法对学生按照最终成绩进行排序,然后根据输入的奖学金等级数依次给出各个等级的获奖人数和获奖名单。最后释放动态分配的内存并退出程序。
相关问题
#include <stdio.h> #include <stdlib.h> #define MAXSIZE 100 typedef int ElemType; typedef struct
这段代码是C语言中的头文件引用和一些定义的示例。其中,`#include <stdio.h>`和`#include <stdlib.h>`是用来引入标准库函数的头文件。`stdio.h`包含了输入输出相关的函数,如`printf`和`scanf`;`stdlib.h`包含了一些常用的函数,如内存分配函数`malloc`和随机数生成函数`rand`。
`#define MAXSIZE 100`是一个宏定义,用来定义一个常量`MAXSIZE`,它的值为100。
`typedef int ElemType;`是用来给`int`类型起一个别名`ElemType`,方便在代码中使用。
至于代码中的结构体部分,由于你没有提供完整的代码,所以无法给出具体的解释。但一般来说,结构体用于定义一种自定义的数据类型,可以包含多个不同类型的成员变量。
#include <stdio.h> #include <stdlib.h> typedef int ElemType; typedef struct LNode { ElemType da
`#include <stdio.h>`和`#include <stdlib.h>`是C语言中的两个头文件。`<stdio.h>`是标准输入输出头文件,提供了一些用于输入输出的函数和宏定义。`<stdlib.h>`是标准库头文件,提供了一些常用的函数和宏定义,如内存分配、随机数生成等。
在你的代码中,`typedef int ElemType;`定义了一个类型别名,将`int`类型命名为`ElemType`。
`typedef struct LNode`定义了一个结构体类型`LNode`,结构体是一种自定义的数据类型,可以包含多个不同类型的成员变量。在这里,`LNode`结构体中的成员变量还没有完整定义,因此代码截断了。
相关推荐
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)