2022级自动化专业某班共有15名学生,本学期共选修6门课程。要求主函数中存储这个班级所有学生的学号,姓名,班级,专业,性别,年龄, 6门课程的成绩(成绩均为百分制)。 要求程序在一个源程序文件采用10个函数实现以下几个功能: (1)输出功能:输出该班所有学生的信息;(2)计算班级里每名同学的平均成绩(平均成绩保留小数点两位有效数字)并存储到计算机,最终输出该班级所有学生的信息(包括学号、姓名、班级,专业,性别、年龄、6门课程的成绩、平均成绩) ;(3)查询功能:根据学生的姓名或者学号查询该生的信息并输出该学生的所有信息;
时间: 2023-11-16 16:31:02 浏览: 106
好的,以下是我为您提供的程序示例,其中包含了您所需的三个功能:
```c
#include <stdio.h>
#include <string.h>
#define MAX_STUDENTS 15
#define MAX_NAME_LENGTH 20
#define MAX_COURSE_NUM 6
typedef struct {
char id[10];
char name[MAX_NAME_LENGTH];
char className[10];
char major[20];
char gender[5];
int age;
double courses[MAX_COURSE_NUM];
double average;
} Student;
Student students[MAX_STUDENTS];
int findStudent(char *key) {
int i;
for (i = 0; i < MAX_STUDENTS; i++) {
if (strcmp(students[i].id, key) == 0 || strcmp(students[i].name, key) == 0) {
return i;
}
}
return -1;
}
void inputStudents() {
int i, j;
for (i = 0; i < MAX_STUDENTS; i++) {
printf("Enter information for student %d:\n", i+1);
printf("ID: ");
scanf("%s", students[i].id);
printf("Name: ");
scanf("%s", students[i].name);
printf("Class: ");
scanf("%s", students[i].className);
printf("Major: ");
scanf("%s", students[i].major);
printf("Gender: ");
scanf("%s", students[i].gender);
printf("Age: ");
scanf("%d", &students[i].age);
printf("Courses:\n");
for (j = 0; j < MAX_COURSE_NUM; j++) {
printf("Course %d: ", j+1);
scanf("%lf", &students[i].courses[j]);
}
}
}
void printStudents() {
int i, j;
printf("All students:\n");
for (i = 0; i < MAX_STUDENTS; i++) {
printf("ID: %s\n", students[i].id);
printf("Name: %s\n", students[i].name);
printf("Class: %s\n", students[i].className);
printf("Major: %s\n", students[i].major);
printf("Gender: %s\n", students[i].gender);
printf("Age: %d\n", students[i].age);
printf("Courses:\n");
for (j = 0; j < MAX_COURSE_NUM; j++) {
printf("Course %d: %.2lf\n", j+1, students[i].courses[j]);
}
printf("Average: %.2lf\n\n", students[i].average);
}
}
void calculateAverages() {
int i, j;
for (i = 0; i < MAX_STUDENTS; i++) {
double sum = 0.0;
for (j = 0; j < MAX_COURSE_NUM; j++) {
sum += students[i].courses[j];
}
students[i].average = sum / MAX_COURSE_NUM;
}
}
void searchStudent() {
char key[20];
printf("Enter student ID or name: ");
scanf("%s", key);
int index = findStudent(key);
if (index == -1) {
printf("Student not found!\n");
} else {
printf("ID: %s\n", students[index].id);
printf("Name: %s\n", students[index].name);
printf("Class: %s\n", students[index].className);
printf("Major: %s\n", students[index].major);
printf("Gender: %s\n", students[index].gender);
printf("Age: %d\n", students[index].age);
printf("Courses:\n");
int j;
for (j = 0; j < MAX_COURSE_NUM; j++) {
printf("Course %d: %.2lf\n", j+1, students[index].courses[j]);
}
printf("Average: %.2lf\n\n", students[index].average);
}
}
int main() {
inputStudents();
calculateAverages();
printStudents();
searchStudent();
return 0;
}
```
在这个程序中包含了 `Student` 结构体,其中存储了每个学生的信息。`inputStudents()` 函数用于输入所有学生信息,`printStudents()` 函数用于输出所有学生信息,`calculateAverages()` 函数用于计算每个学生的平均成绩,`searchStudent()` 函数用于查询指定学生的信息。您可以根据需要进行调整和修改。
阅读全文