使用c语言,设计一个简单的学生成绩管理系统。设有学生成绩文件student.txt,每位学生成绩信息包含学号(num)、姓名(name)、性别(sex)、出生日期(birthday,包括年、月、日,分别表示为year,month,day)、3门功课的成绩(高数、C语言、大学物理)、总分和平均分。设计程序,要求从文件student.txt中读出学生成绩表,之后参对成绩表进行如下操作: 添加。可添加一条或多条记录。
时间: 2023-07-10 21:19:57 浏览: 190
好的,我会给您提供一个简单的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], new_stu;
int main() {
FILE *fp;
int i, n;
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);
if(ch == 'y' || ch == 'Y') {
printf("Please enter the new student score information:\n");
printf("num: ");
scanf("%d", &new_stu.num);
printf("name: ");
scanf("%s", new_stu.name);
printf("sex: ");
scanf("%s", new_stu.sex);
printf("birthday(year, month, day): ");
scanf("%d %d %d", &new_stu.year, &new_stu.month, &new_stu.day);
printf("math: ");
scanf("%f", &new_stu.math);
printf("c_language: ");
scanf("%f", &new_stu.c_language);
printf("physics: ");
scanf("%f", &new_stu.physics);
new_stu.total = new_stu.math + new_stu.c_language + new_stu.physics;
new_stu.average = new_stu.total / 3;
stu[n] = new_stu;
n++;
fp = fopen("student.txt", "a");
if(fp == NULL) {
printf("File open failed!\n");
exit(1);
}
fprintf(fp, "%d %s %s %d %d %d %.2f %.2f %.2f %.2f %.2f\n", new_stu.num, new_stu.name, new_stu.sex, new_stu.year, new_stu.month, new_stu.day, new_stu.math, new_stu.c_language, new_stu.physics, new_stu.total, new_stu.average);
fclose(fp);
printf("The new student score information has been added successfully!\n");
}
else {
printf("No new student score information is added!\n");
}
return 0;
}
```
您可以将该代码保存为一个.c文件,然后用C语言编译器进行编译运行。当您运行该程序时,它会先读取文件student.txt中的学生成绩信息,并将其输出到屏幕上。然后,它会询问您是否要添加新的学生成绩信息。如果您选择“y”或“Y”,则会提示您输入新学生的信息,包括学号、姓名、性别、生日和3门科目的成绩。最后,程序会将新的学生成绩信息添加到文件student.txt中,并输出提示信息。如果您选择“n”或“N”,则程序不会添加新的学生成绩信息。
阅读全文