完成一个有以下功能的c语言代码(1) 录入每个学生的学号、姓名和各科考试成绩。 (2) 计算每门课程的总分和平均分。 (3) 计算每个学生的总分和平均分。 (4) 按每个学生的总分由高到低排出名次表。 (5) 按学号由小到大排出成绩表。 (6) 按姓名的字典顺序排出成绩表。 (7) 按学号查询学生排名极其考试成绩。 (8) 按姓名查询学生排名极其考试成绩。 (9) 按优秀(90~100)、良好(80~90)、中等(70~80)、及格(60~70)、不及格(0~59)
时间: 2024-01-21 21:04:10 浏览: 80
c代码-从键盘输入一个班(全班人数在15~20之间)学生某门课的成绩,当输入成绩为负值时,输入结束,分别实现下列功能: (1)录入每个学生的学号(学号:2016001~2016020)和考试成绩; (2)计算课程的总分和平均分;
5星 · 资源好评率100%
以下是一个简单的C语言代码实现以上功能,具体实现可能需要根据实际情况进行修改。
```c
#include <stdio.h>
#include <string.h>
#define MAX_NUM 100 // 最大学生数
#define MAX_NAME 20 // 最大姓名长度
#define SUBJECT_NUM 3 // 科目数
typedef struct {
char id[10]; // 学号
char name[MAX_NAME]; // 姓名
int score[SUBJECT_NUM]; // 成绩
int total_score; // 总分
float avg_score; // 平均分
int rank; // 排名
} student;
void input_students(student *students, int *num) {
printf("请输入学生数:");
scanf("%d", num);
for (int i = 0; i < *num; i++) {
printf("请输入学生信息:学号 姓名 成绩1 成绩2 成绩3\n");
scanf("%s %s %d %d %d", students[i].id, students[i].name, &students[i].score[0], &students[i].score[1], &students[i].score[2]);
students[i].total_score = students[i].score[0] + students[i].score[1] + students[i].score[2];
students[i].avg_score = students[i].total_score / 3.0;
}
}
void calc_subject_scores(student *students, int num, int *subject_total_score, float *subject_avg_score) {
memset(subject_total_score, 0, sizeof(int) * SUBJECT_NUM);
for (int i = 0; i < num; i++) {
for (int j = 0; j < SUBJECT_NUM; j++) {
subject_total_score[j] += students[i].score[j];
}
}
for (int i = 0; i < SUBJECT_NUM; i++) {
subject_avg_score[i] = subject_total_score[i] * 1.0 / num;
}
}
void calc_student_scores(student *students, int num) {
for (int i = 0; i < num; i++) {
students[i].total_score = 0;
for (int j = 0; j < SUBJECT_NUM; j++) {
students[i].total_score += students[i].score[j];
}
students[i].avg_score = students[i].total_score / 3.0;
}
}
void sort_students(student *students, int num) {
for (int i = 0; i < num; i++) {
students[i].rank = 1;
for (int j = 0; j < num; j++) {
if (students[j].total_score > students[i].total_score) {
students[i].rank++;
}
}
}
for (int i = 0; i < num; i++) {
for (int j = i + 1; j < num; j++) {
if (students[i].total_score < students[j].total_score) {
student temp = students[i];
students[i] = students[j];
students[j] = temp;
}
}
}
}
void sort_students_by_id(student *students, int num) {
for (int i = 0; i < num; i++) {
for (int j = i + 1; j < num; j++) {
if (strcmp(students[i].id, students[j].id) > 0) {
student temp = students[i];
students[i] = students[j];
students[j] = temp;
}
}
}
}
void sort_students_by_name(student *students, int num) {
for (int i = 0; i < num; i++) {
for (int j = i + 1; j < num; j++) {
if (strcmp(students[i].name, students[j].name) > 0) {
student temp = students[i];
students[i] = students[j];
students[j] = temp;
}
}
}
}
void print_students(student *students, int num) {
for (int i = 0; i < num; i++) {
printf("%s %s %d %d %d %d %f %d\n", students[i].id, students[i].name, students[i].score[0], students[i].score[1], students[i].score[2], students[i].total_score, students[i].avg_score, students[i].rank);
}
}
void search_student_by_id(student *students, int num, char *id) {
for (int i = 0; i < num; i++) {
if (strcmp(students[i].id, id) == 0) {
printf("%s %s %d %d %d %d %f %d\n", students[i].id, students[i].name, students[i].score[0], students[i].score[1], students[i].score[2], students[i].total_score, students[i].avg_score, students[i].rank);
return;
}
}
printf("未找到学号为%s的学生\n", id);
}
void search_student_by_name(student *students, int num, char *name) {
for (int i = 0; i < num; i++) {
if (strcmp(students[i].name, name) == 0) {
printf("%s %s %d %d %d %d %f %d\n", students[i].id, students[i].name, students[i].score[0], students[i].score[1], students[i].score[2], students[i].total_score, students[i].avg_score, students[i].rank);
return;
}
}
printf("未找到姓名为%s的学生\n", name);
}
void print_students_by_score_range(student *students, int num, int min_score, int max_score) {
for (int i = 0; i < num; i++) {
if (students[i].total_score >= min_score && students[i].total_score <= max_score) {
printf("%s %s %d %d %d %d %f %d\n", students[i].id, students[i].name, students[i].score[0], students[i].score[1], students[i].score[2], students[i].total_score, students[i].avg_score, students[i].rank);
}
}
}
int main() {
student students[MAX_NUM];
int num = 0;
int subject_total_score[SUBJECT_NUM];
float subject_avg_score[SUBJECT_NUM];
input_students(students, &num);
calc_subject_scores(students, num, subject_total_score, subject_avg_score);
calc_student_scores(students, num);
sort_students(students, num);
printf("按总分排名:\n");
print_students(students, num);
sort_students_by_id(students, num);
printf("按学号排序:\n");
print_students(students, num);
sort_students_by_name(students, num);
printf("按姓名排序:\n");
print_students(students, num);
char search_id[10];
printf("请输入要查询的学生学号:");
scanf("%s", search_id);
search_student_by_id(students, num, search_id);
char search_name[MAX_NAME];
printf("请输入要查询的学生姓名:");
scanf("%s", search_name);
search_student_by_name(students, num, search_name);
printf("成绩在80-90分之间的学生:\n");
print_students_by_score_range(students, num, 80, 90);
return 0;
}
```
阅读全文