用 c 语言在 vs2022 上写一个学生成绩管理系统要 200 行以上十个函数以上
时间: 2024-05-13 18:15:11 浏览: 73
学生成绩管理系统(C语言实现)
以下是一个简单的学生成绩管理系统,包含了十个函数以上,大约有200行代码。其中包括了学生信息的输入、输出,成绩的录入、查询、修改、删除等基本功能。
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STUDENT_NUM 100
#define NAME_LEN 20
typedef struct student {
char name[NAME_LEN];
int id;
float score;
} Student;
int menu();
void add_student(Student *students, int *num);
void print_student(const Student *students, int num);
void search_student(const Student *students, int num);
void modify_student(Student *students, int num);
void delete_student(Student *students, int *num);
void sort_student(Student *students, int num);
void save_student(const Student *students, int num);
void load_student(Student *students, int *num);
int main() {
Student students[MAX_STUDENT_NUM];
int num = 0;
int choice;
load_student(students, &num);
do {
choice = menu();
switch (choice) {
case 1:
add_student(students, &num);
break;
case 2:
print_student(students, num);
break;
case 3:
search_student(students, num);
break;
case 4:
modify_student(students, num);
break;
case 5:
delete_student(students, &num);
break;
case 6:
sort_student(students, num);
break;
case 7:
save_student(students, num);
break;
case 0:
printf("Goodbye!\n");
break;
default:
printf("Invalid choice, please try again!\n");
}
} while (choice != 0);
return 0;
}
int menu() {
int choice;
printf("Student Score Management System\n");
printf("--------------------------------\n");
printf("1. Add student\n");
printf("2. Print all students\n");
printf("3. Search student\n");
printf("4. Modify student\n");
printf("5. Delete student\n");
printf("6. Sort students\n");
printf("7. Save to file\n");
printf("0. Exit\n");
printf("Please enter your choice: ");
scanf("%d", &choice);
return choice;
}
void add_student(Student *students, int *num) {
if (*num >= MAX_STUDENT_NUM) {
printf("Maximum number of students reached!\n");
return;
}
printf("Please enter student name: ");
scanf("%s", students[*num].name);
printf("Please enter student ID: ");
scanf("%d", &students[*num].id);
printf("Please enter student score: ");
scanf("%f", &students[*num].score);
(*num)++;
}
void print_student(const Student *students, int num) {
if (num == 0) {
printf("No students found!\n");
return;
}
printf("Name\tID\tScore\n");
printf("-----------------\n");
for (int i = 0; i < num; i++) {
printf("%s\t%d\t%.2f\n", students[i].name, students[i].id, students[i].score);
}
}
void search_student(const Student *students, int num) {
char keyword[NAME_LEN];
printf("Please enter search keyword: ");
scanf("%s", keyword);
printf("Name\tID\tScore\n");
printf("-----------------\n");
for (int i = 0; i < num; i++) {
if (strstr(students[i].name, keyword) != NULL) {
printf("%s\t%d\t%.2f\n", students[i].name, students[i].id, students[i].score);
}
}
}
void modify_student(Student *students, int num) {
int id;
printf("Please enter student ID: ");
scanf("%d", &id);
for (int i = 0; i < num; i++) {
if (students[i].id == id) {
printf("Please enter new score: ");
scanf("%f", &students[i].score);
printf("Score updated successfully!\n");
return;
}
}
printf("Student not found!\n");
}
void delete_student(Student *students, int *num) {
int id;
int index = -1;
printf("Please enter student ID: ");
scanf("%d", &id);
for (int i = 0; i < *num; i++) {
if (students[i].id == id) {
index = i;
break;
}
}
if (index == -1) {
printf("Student not found!\n");
return;
}
for (int i = index; i < *num - 1; i++) {
students[i] = students[i + 1];
}
(*num)--;
printf("Student deleted successfully!\n");
}
void sort_student(Student *students, int num) {
for (int i = 0; i < num - 1; i++) {
for (int j = i + 1; j < num; j++) {
if (students[i].score < students[j].score) {
Student temp = students[i];
students[i] = students[j];
students[j] = temp;
}
}
}
printf("Sort completed!\n");
}
void save_student(const Student *students, int num) {
FILE *fp = fopen("students.txt", "w");
if (fp == NULL) {
printf("Failed to open file!\n");
return;
}
for (int i = 0; i < num; i++) {
fprintf(fp, "%s %d %.2f\n", students[i].name, students[i].id, students[i].score);
}
fclose(fp);
printf("Data saved to file successfully!\n");
}
void load_student(Student *students, int *num) {
FILE *fp = fopen("students.txt", "r");
if (fp == NULL) {
printf("File not found!\n");
return;
}
while (!feof(fp) && *num < MAX_STUDENT_NUM) {
fscanf(fp, "%s %d %f", students[*num].name, &students[*num].id, &students[*num].score);
(*num)++;
}
fclose(fp);
printf("Data loaded from file successfully!\n");
}
```
这个学生成绩管理系统使用了一个结构体 `Student` 来存储每个学生的信息,包括姓名、学号和成绩。其中用到了文件读写、数组排序等基本操作。
阅读全文