使用c语言,设计一个简单的学生成绩管理系统。设有学生成绩文件student.txt,每位学生成绩信息包含学号(num)、姓名(name)、性别(sex)、出生日期(birthday,包括年、月、日,分别表示为year,month,day)、3门功课的成绩(高数、C语言、大学物理)、总分和平均分。设计程序,要求从文件student.txt中读出学生成绩表,之后参对成绩表进行如下操作 添加。可添加一条或多条记录。
时间: 2023-07-10 10:19:58 浏览: 74
学生成绩管理系统,c语言写的
好的,我已经回答过这个问题了,您可以参考上面的回答。这里再给您提供一下完整的代码,包括添加多条记录的功能:
```c
#include <stdio.h>
#include <stdlib.h>
struct student {
int num;
char name[20];
char sex[10];
int year, month, day;
float math, c_language, physics, total, average;
}stu[100];
int main() {
FILE *fp;
int i, n, m;
char ch;
fp = fopen("student.txt", "r");
if(fp == NULL) {
printf("File does not exist!\n");
exit(1);
}
n = 0;
while(!feof(fp)) {
fscanf(fp, "%d %s %s %d %d %d %f %f %f %f %f", &stu[n].num, stu[n].name, stu[n].sex, &stu[n].year, &stu[n].month, &stu[n].day, &stu[n].math, &stu[n].c_language, &stu[n].physics, &stu[n].total, &stu[n].average);
n++;
}
fclose(fp);
printf("The original student score information is as follows:\n");
printf("num name sex birthday math c_language physics total average\n");
for(i = 0; i < n; i++) {
printf("%d %s %s %d/%d/%d %.2f %.2f %.2f %.2f %.2f\n", stu[i].num, stu[i].name, stu[i].sex, stu[i].year, stu[i].month, stu[i].day, stu[i].math, stu[i].c_language, stu[i].physics, stu[i].total, stu[i].average);
}
printf("Do you want to add a new student score information? (y/n)\n");
scanf(" %c", &ch);
while(ch == 'y' || ch == 'Y') {
printf("Please enter the number of student score information you want to add:\n");
scanf("%d", &m);
printf("Please enter the new student score information:\n");
for(i = n; i < n + m; i++) {
printf("num: ");
scanf("%d", &stu[i].num);
printf("name: ");
scanf("%s", stu[i].name);
printf("sex: ");
scanf("%s", stu[i].sex);
printf("birthday(year, month, day): ");
scanf("%d %d %d", &stu[i].year, &stu[i].month, &stu[i].day);
printf("math: ");
scanf("%f", &stu[i].math);
printf("c_language: ");
scanf("%f", &stu[i].c_language);
printf("physics: ");
scanf("%f", &stu[i].physics);
stu[i].total = stu[i].math + stu[i].c_language + stu[i].physics;
stu[i].average = stu[i].total / 3;
}
n += m;
fp = fopen("student.txt", "a");
if(fp == NULL) {
printf("File open failed!\n");
exit(1);
}
for(i = n - m; i < n; i++) {
fprintf(fp, "%d %s %s %d %d %d %.2f %.2f %.2f %.2f %.2f\n", stu[i].num, stu[i].name, stu[i].sex, stu[i].year, stu[i].month, stu[i].day, stu[i].math, stu[i].c_language, stu[i].physics, stu[i].total, stu[i].average);
}
fclose(fp);
printf("The new student score information has been added successfully!\n");
printf("Do you want to add more student score information? (y/n)\n");
scanf(" %c", &ch);
}
if(ch == 'n' || ch == 'N') {
printf("No new student score information is added!\n");
}
return 0;
}
```
当您运行该程序时,它会先读取文件student.txt中的学生成绩信息,并将其输出到屏幕上。然后,它会询问您是否要添加新的学生成绩信息。如果您选择“y”或“Y”,则会提示您输入要添加的学生人数,然后提示您逐一输入每一位学生的信息。最后,程序会将新的学生成绩信息添加到文件student.txt中,并输出提示信息。如果您选择“n”或“N”,则程序不会添加新的学生成绩信息。
阅读全文