使用c语言,用到typedef,设计一个简单的学生成绩管理系统。设有学生成绩文件student.txt,每位学生成绩信息包含学号(num)、姓名(name)、性别(sex)、出生日期(birthday,包括年、月、日,分别表示为year,month,day)、3门功课的成绩(高数、C语言、大学物理)、总分和平均分。设计程序,要求从文件student.txt中读出学生成绩表,之后参对成绩表进行如下操作 (9)显示。即输出出所有成绩。
时间: 2023-07-10 11:23:28 浏览: 119
好的,我来给您提供一份简单的实现代码,如下所示:
```c
#include <stdio.h>
#define MAX_STUDENTS 100
typedef struct {
char num[10];
char name[20];
char sex[5];
int year;
int month;
int day;
int score[3];
int total;
float average;
} student;
int read_student_info(student students[]);
void display_all_students(student students[], int num_students);
int main() {
student students[MAX_STUDENTS];
int num_students = read_student_info(students);
printf("学生成绩表:\n");
display_all_students(students, num_students);
return 0;
}
int read_student_info(student students[]) {
int i = 0;
int num_students = 0;
FILE *fp;
fp = fopen("student.txt", "r");
if (fp == NULL) {
printf("打开文件失败!\n");
return 0;
}
while (fscanf(fp, "%s %s %s %d/%d/%d %d %d %d",
students[num_students].num,
students[num_students].name,
students[num_students].sex,
&students[num_students].year,
&students[num_students].month,
&students[num_students].day,
&students[num_students].score[0],
&students[num_students].score[1],
&students[num_students].score[2]) != EOF) {
students[num_students].total = students[num_students].score[0] + students[num_students].score[1] + students[num_students].score[2];
students[num_students].average = students[num_students].total / 3.0;
num_students++;
if (num_students >= MAX_STUDENTS) {
printf("已有太多学生信息!\n");
break;
}
}
fclose(fp);
return num_students;
}
void display_all_students(student students[], int num_students) {
int i;
for (i = 0; i < num_students; i++) {
printf("%-10s%-10s%-5s%4d/%02d/%02d%5d%5d%5d%5d%7.2f\n",
students[i].num,
students[i].name,
students[i].sex,
students[i].year,
students[i].month,
students[i].day,
students[i].score[0],
students[i].score[1],
students[i].score[2],
students[i].total,
students[i].average);
}
}
```
这份代码定义了一个结构体student,存储了每个学生的信息。read_student_info函数从文件中读取学生信息并计算总分和平均分,返回学生个数。display_all_students函数用于打印所有学生的成绩信息。在main函数中,首先调用read_student_info函数读取学生信息,之后调用display_all_students函数打印所有学生的成绩信息。
阅读全文