使用C语言结构体来写一个程序某班有最多不超过30人(具体人数由键盘输入)参加期末考试,考试科目最多不超过6门(具体门数由键盘输入),定义结构体类型,用结构体数组作函数参数,编程实现如下学生成绩管理: (1)录入每个学生的学号、姓名和各科考试成绩;(2)计算每门课程的总分和平均分; (3)计算每个学生的总分和平均分; (4)按每个学生的总分由高到低排出名次表;(5)按每个学生的总分由低到高排出名次表;(6)按学号由小到大排出成绩表; (7)按姓名的字典顺序排出成绩表; (8)按学号查询学生排名及其各科考试成绩;(9)按姓名查询学生排名及其各科考试成绩; (10)输出每个学生的学号、姓名、各科考试成绩、总分、平均分,以及每门课程的总分和平均分。并给出每一句的注释

时间: 2023-06-17 08:07:36 浏览: 92
```c #include <stdio.h> #include <string.h> // 定义一个课程结构体,包括课程名、总分和平均分 struct Course { char name[20]; float totalScore; float avgScore; }; // 定义一个学生结构体,包括学号、姓名、各科成绩、总分和平均分 struct Student { char id[10]; char name[20]; float score[6]; float totalScore; float avgScore; }; // 函数声明 void inputStudents(struct Student[], int, int); void calcCourseScore(struct Student[], int, int, struct Course[]); void calcStudentScore(struct Student[], int, int); void sortStudentByTotalScore(struct Student[], int); void sortStudentByTotalScoreReverse(struct Student[], int); void sortStudentById(struct Student[], int); void sortStudentByName(struct Student[], int); void searchStudentById(struct Student[], int, char[]); void searchStudentByName(struct Student[], int, char[]); int main() { int n, m; printf("请输入学生人数(不超过30人):"); scanf("%d", &n); printf("请输入考试科目数(不超过6门):"); scanf("%d", &m); struct Student students[n]; // 定义学生结构体数组 struct Course courses[m]; // 定义课程结构体数组 inputStudents(students, n, m); // 录入学生信息和成绩 calcCourseScore(students, n, m, courses); // 计算每门课程的总分和平均分 calcStudentScore(students, n, m); // 计算每个学生的总分和平均分 sortStudentByTotalScore(students, n); // 按每个学生的总分由高到低排出名次表 sortStudentByTotalScoreReverse(students, n); // 按每个学生的总分由低到高排出名次表 sortStudentById(students, n); // 按学号由小到大排出成绩表 sortStudentByName(students, n); // 按姓名的字典顺序排出成绩表 char target[10]; printf("请输入要查询的学生学号:"); scanf("%s", target); searchStudentById(students, n, target); // 按学号查询学生排名及其各科考试成绩 printf("请输入要查询的学生姓名:"); scanf("%s", target); searchStudentByName(students, n, target); // 按姓名查询学生排名及其各科考试成绩 printf("学号\t姓名\t"); for (int i = 0; i < m; i++) { printf("%s总分\t%s平均分\t", courses[i].name, courses[i].name); } printf("总分\t平均分\n"); for (int i = 0; i < n; i++) { printf("%s\t%s\t", students[i].id, students[i].name); for (int j = 0; j < m; j++) { printf("%.2f\t%.2f\t", students[i].score[j], students[i].score[j] / m); } printf("%.2f\t%.2f\n", students[i].totalScore, students[i].avgScore); } return 0; } // 录入学生信息和成绩 void inputStudents(struct Student students[], int n, int m) { for (int i = 0; i < n; i++) { printf("请输入第%d个学生的学号、姓名和各科成绩:\n", i + 1); scanf("%s %s", students[i].id, students[i].name); for (int j = 0; j < m; j++) { scanf("%f", &students[i].score[j]); } } } // 计算每门课程的总分和平均分 void calcCourseScore(struct Student students[], int n, int m, struct Course courses[]) { for (int i = 0; i < m; i++) { strcpy(courses[i].name, "科目"); courses[i].name[5] = i + '1'; courses[i].name[6] = '\0'; courses[i].totalScore = 0; for (int j = 0; j < n; j++) { courses[i].totalScore += students[j].score[i]; } courses[i].avgScore = courses[i].totalScore / n; } } // 计算每个学生的总分和平均分 void calcStudentScore(struct Student students[], int n, int m) { for (int i = 0; i < n; i++) { students[i].totalScore = 0; for (int j = 0; j < m; j++) { students[i].totalScore += students[i].score[j]; } students[i].avgScore = students[i].totalScore / m; } } // 按每个学生的总分由高到低排出名次表 void sortStudentByTotalScore(struct Student students[], int n) { for (int i = 0; i < n - 1; i++) { for (int j = i + 1; j < n; j++) { if (students[i].totalScore < students[j].totalScore) { struct Student temp = students[i]; students[i] = students[j]; students[j] = temp; } } } } // 按每个学生的总分由低到高排出名次表 void sortStudentByTotalScoreReverse(struct Student students[], int n) { for (int i = 0; i < n - 1; i++) { for (int j = i + 1; j < n; j++) { if (students[i].totalScore > students[j].totalScore) { struct Student temp = students[i]; students[i] = students[j]; students[j] = temp; } } } } // 按学号由小到大排出成绩表 void sortStudentById(struct Student students[], int n) { for (int i = 0; i < n - 1; i++) { for (int j = i + 1; j < n; j++) { if (strcmp(students[i].id, students[j].id) > 0) { struct Student temp = students[i]; students[i] = students[j]; students[j] = temp; } } } } // 按姓名的字典顺序排出成绩表 void sortStudentByName(struct Student students[], int n) { for (int i = 0; i < n - 1; i++) { for (int j = i + 1; j < n; j++) { if (strcmp(students[i].name, students[j].name) > 0) { struct Student temp = students[i]; students[i] = students[j]; students[j] = temp; } } } } // 按学号查询学生排名及其各科考试成绩 void searchStudentById(struct Student students[], int n, char target[]) { int rank = -1; for (int i = 0; i < n; i++) { if (strcmp(students[i].id, target) == 0) { rank = i + 1; printf("%d\t%s\t", rank, students[i].name); for (int j = 0; j < 6; j++) { printf("%.2f\t", students[i].score[j]); } printf("%.2f\t%.2f\n", students[i].totalScore, students[i].avgScore); return; } } printf("未找到学号为%s的学生\n", target); } // 按姓名查询学生排名及其各科考试成绩 void searchStudentByName(struct Student students[], int n, char target[]) { int rank = -1; for (int i = 0; i < n; i++) { if (strcmp(students[i].name, target) == 0) { rank = i + 1; printf("%d\t%s\t", rank, students[i].id); for (int j = 0; j < 6; j++) { printf("%.2f\t", students[i].score[j]); } printf("%.2f\t%.2f\n", students[i].totalScore, students[i].avgScore); return; } } printf("未找到姓名为%s的学生\n", target); } ```

相关推荐

最新推荐

recommend-type

C语言结构体(struct)常见使用方法(细节问题)

主要介绍了C语言结构体(struct)常见使用方法(细节问题),需要的朋友可以参考下
recommend-type

C语言 结构体和指针详解及简单示例

本文主要介绍C语言 结构体和指针,这里整理了相关资料,并附示例代码和实现结果,以便大家学习参考,希望能帮助学习C语言的朋友
recommend-type

详解C语言中结构体的自引用和相互引用

主要介绍了C语言中结构体的自引用和相互引用,详细解析了结构体中指针的指向情况,需要的朋友可以参考下
recommend-type

C语言程序设计+研究生复试+求职+面试题

汇总了计算机研究生复试有关C语言程序设计各章节简答题,使用了易于口头表达的语言进行了总结。包括C语言程序设计基本概念及各章节问题回答。可供研究生复试或相关专业岗位面试使用。如: 简述C语⾔采取了哪些措施提...
recommend-type

深入分析C语言中结构体指针的定义与引用详解

本篇文章是对C语言中结构体指针的定义与引用进行了详细的分析介绍,需要的朋友参考下
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

2. 通过python绘制y=e-xsin(2πx)图像

可以使用matplotlib库来绘制这个函数的图像。以下是一段示例代码: ```python import numpy as np import matplotlib.pyplot as plt def func(x): return np.exp(-x) * np.sin(2 * np.pi * x) x = np.linspace(0, 5, 500) y = func(x) plt.plot(x, y) plt.xlabel('x') plt.ylabel('y') plt.title('y = e^{-x} sin(2πx)') plt.show() ``` 运行这段
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。