给我个c语言代码,要求对一个班级学生成绩的管理,不仅能创建文件、输入班级成绩、保存文件,还能让一个班级成绩文件复制到另一个文件内容,并且能够查询成绩、修改成绩、显示所有的学生成绩。
时间: 2024-02-03 10:03:42 浏览: 53
可以在文件里面存储学生信息,可以建立并删除学生成绩,可以按班级查询学生成绩.zip
好的,以下是一个简单的C语言代码,可以对班级学生成绩进行管理:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct student {
char name[20];
int id;
float score;
} Student;
// 创建文件
void create_file(char *filename) {
FILE *fp = fopen(filename, "w");
if (fp == NULL) {
printf("Failed to create file!\n");
exit(1);
}
fclose(fp);
printf("File created successfully!\n");
}
// 输入班级成绩
void input_score(char *filename) {
FILE *fp = fopen(filename, "a");
if (fp == NULL) {
printf("Failed to open file!\n");
exit(1);
}
Student student;
printf("Please enter student information (name/id/score):\n");
scanf("%s %d %f", student.name, &student.id, &student.score);
fprintf(fp, "%s %d %.2f\n", student.name, student.id, student.score);
fclose(fp);
printf("Input student score successfully!\n");
}
// 保存文件
void save_file(char *filename) {
FILE *fp = fopen(filename, "r");
if (fp == NULL) {
printf("Failed to open file!\n");
exit(1);
}
char new_filename[30];
printf("Please enter the new file name: ");
scanf("%s", new_filename);
FILE *new_fp = fopen(new_filename, "w");
if (new_fp == NULL) {
printf("Failed to create file!\n");
exit(1);
}
char buffer[50];
while (fgets(buffer, 50, fp) != NULL) {
fputs(buffer, new_fp);
}
fclose(fp);
fclose(new_fp);
printf("File saved successfully!\n");
}
// 复制文件
void copy_file(char *filename) {
FILE *fp = fopen(filename, "r");
if (fp == NULL) {
printf("Failed to open file!\n");
exit(1);
}
char new_filename[30];
printf("Please enter the new file name: ");
scanf("%s", new_filename);
FILE *new_fp = fopen(new_filename, "w");
if (new_fp == NULL) {
printf("Failed to create file!\n");
exit(1);
}
char buffer[50];
while (fgets(buffer, 50, fp) != NULL) {
fputs(buffer, new_fp);
}
fclose(fp);
fclose(new_fp);
printf("File copied successfully!\n");
}
// 查询成绩
void query_score(char *filename) {
FILE *fp = fopen(filename, "r");
if (fp == NULL) {
printf("Failed to open file!\n");
exit(1);
}
int id;
printf("Please enter the student id: ");
scanf("%d", &id);
char buffer[50];
int found = 0;
while (fgets(buffer, 50, fp) != NULL) {
Student student;
sscanf(buffer, "%s %d %f", student.name, &student.id, &student.score);
if (id == student.id) {
printf("Name: %s, ID: %d, Score: %.2f\n", student.name, student.id, student.score);
found = 1;
break;
}
}
if (!found) {
printf("Student not found!\n");
}
fclose(fp);
}
// 修改成绩
void modify_score(char *filename) {
FILE *fp = fopen(filename, "r");
if (fp == NULL) {
printf("Failed to open file!\n");
exit(1);
}
int id;
printf("Please enter the student id: ");
scanf("%d", &id);
char buffer[50];
int found = 0;
FILE *new_fp = fopen("temp.txt", "w");
if (new_fp == NULL) {
printf("Failed to create file!\n");
exit(1);
}
while (fgets(buffer, 50, fp) != NULL) {
Student student;
sscanf(buffer, "%s %d %f", student.name, &student.id, &student.score);
if (id == student.id) {
printf("Please enter the new score: ");
scanf("%f", &student.score);
fprintf(new_fp, "%s %d %.2f\n", student.name, student.id, student.score);
found = 1;
} else {
fprintf(new_fp, "%s %d %.2f\n", student.name, student.id, student.score);
}
}
fclose(fp);
fclose(new_fp);
remove(filename);
rename("temp.txt", filename);
if (found) {
printf("Score modified successfully!\n");
} else {
printf("Student not found!\n");
}
}
// 显示所有学生成绩
void display_score(char *filename) {
FILE *fp = fopen(filename, "r");
if (fp == NULL) {
printf("Failed to open file!\n");
exit(1);
}
char buffer[50];
while (fgets(buffer, 50, fp) != NULL) {
Student student;
sscanf(buffer, "%s %d %f", student.name, &student.id, &student.score);
printf("Name: %s, ID: %d, Score: %.2f\n", student.name, student.id, student.score);
}
fclose(fp);
}
int main() {
char filename[30];
printf("Please enter the file name: ");
scanf("%s", filename);
create_file(filename);
int choice;
do {
printf("\nMenu:\n");
printf("1. Input score\n");
printf("2. Save file\n");
printf("3. Copy file\n");
printf("4. Query score\n");
printf("5. Modify score\n");
printf("6. Display score\n");
printf("0. Exit\n");
printf("Please enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
input_score(filename);
break;
case 2:
save_file(filename);
break;
case 3:
copy_file(filename);
break;
case 4:
query_score(filename);
break;
case 5:
modify_score(filename);
break;
case 6:
display_score(filename);
break;
case 0:
printf("Goodbye!\n");
break;
default:
printf("Invalid choice!\n");
break;
}
} while (choice != 0);
return 0;
}
```
这个程序可以完成你所要求的所有功能,你可以根据需要进行修改和完善。
阅读全文