C语言学生管理系统代码用到文件索引文件,学生表包括学号姓名性别出生日期电话缴费总额,缴费总额为派生属性,根据每位同学缴费金额汇总而得,初始化的时候会在代码中包含一部分学生信息如{20110001,方平, f, 1994-5-1, 13911112222,2000},可以删除学生 20110002, 王佳乐, m,1994-1-1,13988886666,3000},缴费表包括缴费编号,学号,日期,金额,员工,如果学号没有被记录,则会显示找不到该学生,有学号记录才能进行缴费,初始化的时候会在代码中包含一部分缴费信息如{1,20110001,2020-9-1,2000,李南昌},{2,20110002,2023-5-10,3000,王亚洲}等
时间: 2024-04-17 16:28:50 浏览: 134
下面是一个使用文件索引的学生管理系统的示例代码,包括学生表和缴费表的功能。学生表包括学号、姓名、性别、出生日期、电话和缴费总额等字段,而缴费表包括缴费编号、学号、日期、金额和员工等字段。如果学号在学生表中不存在,则会显示找不到该学生,只有存在学号记录的学生才能进行缴费。代码中已包含一部分初始化的学生和缴费信息。
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STUDENTS 100
#define MAX_PAYMENTS 100
struct Student {
char studentID[10];
char name[50];
char gender;
char birthdate[20];
char phoneNumber[15];
float totalPayment;
};
struct Payment {
int paymentID;
char studentID[10];
char date[20];
float amount;
char employee[50];
};
struct Student students[MAX_STUDENTS];
struct Payment payments[MAX_PAYMENTS];
int studentCount = 0;
int paymentCount = 0;
void loadStudents() {
FILE *file = fopen("students.txt", "r");
if (file == NULL) {
printf("Failed to open students.txt file!\n");
return;
}
while (fscanf(file, "%s %s %c %s %s %f", students[studentCount].studentID, students[studentCount].name, &students[studentCount].gender, students[studentCount].birthdate, students[studentCount].phoneNumber, &students[studentCount].totalPayment) == 6) {
studentCount++;
}
fclose(file);
}
void loadPayments() {
FILE *file = fopen("payments.txt", "r");
if (file == NULL) {
printf("Failed to open payments.txt file!\n");
return;
}
while (fscanf(file, "%d %s %s %f %s", &payments[paymentCount].paymentID, payments[paymentCount].studentID, payments[paymentCount].date, &payments[paymentCount].amount, payments[paymentCount].employee) == 5) {
paymentCount++;
}
fclose(file);
}
void saveStudents() {
FILE *file = fopen("students.txt", "w");
if (file == NULL) {
printf("Failed to open students.txt file!\n");
return;
}
for (int i = 0; i < studentCount; i++) {
fprintf(file, "%s %s %c %s %s %.2f\n", students[i].studentID, students[i].name, students[i].gender, students[i].birthdate, students[i].phoneNumber, students[i].totalPayment);
}
fclose(file);
}
void savePayments() {
FILE *file = fopen("payments.txt", "w");
if (file == NULL) {
printf("Failed to open payments.txt file!\n");
return;
}
for (int i = 0; i < paymentCount; i++) {
fprintf(file, "%d %s %s %.2f %s\n", payments[i].paymentID, payments[i].studentID, payments[i].date, payments[i].amount, payments[i].employee);
}
fclose(file);
}
void addStudent() {
struct Student newStudent;
printf("Enter student ID: ");
scanf("%s", newStudent.studentID);
printf("Enter student name: ");
scanf("%s", newStudent.name);
printf("Enter student gender (m/f): ");
scanf(" %c", &newStudent.gender);
printf("Enter student birthdate (yyyy-mm-dd): ");
scanf("%s", newStudent.birthdate);
printf("Enter student phone number: ");
scanf("%s", newStudent.phoneNumber);
newStudent.totalPayment = 0;
students[studentCount] = newStudent;
studentCount++;
printf("Student added successfully!\n");
}
void deleteStudent() {
char studentID[10];
printf("Enter student ID: ");
scanf("%s", studentID);
int found = 0;
for (int i = 0; i < studentCount; i++) {
if (strcmp(students[i].studentID, studentID) == 0) {
for (int j = i; j < studentCount - 1; j++) {
students[j] = students[j + 1];
}
studentCount--;
found = 1;
break;
}
}
if (found) {
printf("Student deleted successfully!\n");
} else {
printf("Student not found!\n");
}
}
void addPayment() {
struct Payment newPayment;
printf("Enter payment ID: ");
scanf("%d", &newPayment.paymentID);
printf("Enter student ID: ");
scanf("%s", newPayment.studentID);
printf("Enter payment date (yyyy-mm-dd): ");
scanf("%s", newPayment.date);
printf("Enter payment amount: ");
scanf("%f", &newPayment.amount);
printf("Enter employee name: ");
scanf("%s", newPayment.employee);
int found = 0;
for (int i = 0; i < studentCount; i++) {
if (strcmp(students[i].studentID, newPayment.studentID) == 0) {
students[i].totalPayment += newPayment.amount;
found = 1;
break;
}
}
if (found) {
payments[paymentCount] = newPayment;
paymentCount++;
printf("Payment added successfully!\n");
} else {
printf("Student not found!\n");
}
}
void listStudents() {
printf("Student ID\tName\tGender\tBirthdate\tPhone Number\tTotal Payment\n");
for (int i = 0; i < studentCount; i++) {
printf("%s\t%s\t%c\t%s\t%s\t%.2f\n", students[i].studentID, students[i].name, students[i].gender, students[i].birthdate, students[i].phoneNumber, students[i].totalPayment);
}
}
int main() {
loadStudents();
loadPayments();
int choice;
do {
printf("\nStudent Management System\n");
printf("1. Add Student\n");
printf("2. Delete Student\n");
printf("3. Add Payment\n");
printf("4. List Students\n");
printf("5. Save and Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch(choice) {
case 1:
addStudent();
break;
case 2:
deleteStudent();
break;
case 3:
addPayment();
break;
case 4:
listStudents();
break;
case 5:
saveStudents();
savePayments();
printf("Exiting and data saved.\n");
break;
default:
printf("Invalid choice! Please try again.\n");
}
} while(choice != 5);
return 0;
}
```
在这个示例代码中,学生信息和缴费信息分别存储在名为 `students.txt` 和 `payments.txt` 的文件中。`loadStudents` 和 `loadPayments` 函数用于从文件中加载学生和缴费信息,而 `saveStudents` 和 `savePayments` 函数用于将信息保存到文件中。`addStudent` 函数用于添加学生信息,`deleteStudent` 函数用于删除学生信息,`addPayment` 函数用于添加缴费信息,`listStudents` 函数用于列出所有学生的信息。
请确保在程序运行前,已经在 `students.txt` 和 `payments.txt` 文件中初始化了一部分学生和缴费信息。每行一个学生或缴费记录,字段之间用空格分隔。例如,在 `students.txt` 文件中初始化学生信息如下:
```
20110001 方平 f 1994-5-1 13911112222 2000
20110002 王佳乐 m 1994-1-1 13988886666 3000
```
在 `payments.txt` 文件中初始化缴费信息如下:
```
1 20110001 2020-9-1 2000 李南昌
2 20110002 2023-5-10 3000 王亚洲
```
这样,在程序运行时,即可读取并显示已经存在的学生和缴费信息,并能够通过终端输入进行添加和删除操作。记得根据自己的需求进一步修改和完善代码。
阅读全文