用c语言写一个学生考勤系统,包含以下功能1、 录入学生的缺课记录。 2、 修改某个学生的缺课记录。 3、 查询某个学生的缺课情况。 统计某段时间内,旷课学生姓名及旷课次数,按旷课次数由多到少排序。
时间: 2024-02-19 09:03:58 浏览: 77
好的,这是一个比较复杂的系统,需要用到文件读写功能和字符串处理函数等。以下是一个简单的实现,供参考:
```c
#include <stdio.h>
#include <string.h>
#define MAX_STUDENTS 100
#define MAX_NAME_LEN 20
#define MAX_DATE_LEN 10
typedef struct {
char name[MAX_NAME_LEN];
char date[MAX_DATE_LEN];
} AbsenceRecord;
typedef struct {
char name[MAX_NAME_LEN];
AbsenceRecord records[30]; // 最多记录30天的缺勤情况
int num_records;
} Student;
Student students[MAX_STUDENTS];
int num_students = 0;
// 向文件中写入所有学生的缺勤情况
void saveData() {
FILE *fp = fopen("data.txt", "w");
if (fp == NULL) {
printf("Error opening file!\n");
return;
}
for (int i = 0; i < num_students; i++) {
fprintf(fp, "%s %d\n", students[i].name, students[i].num_records);
for (int j = 0; j < students[i].num_records; j++) {
fprintf(fp, "%s %s\n", students[i].records[j].name, students[i].records[j].date);
}
}
fclose(fp);
}
// 从文件中读取所有学生的缺勤情况
void loadData() {
FILE *fp = fopen("data.txt", "r");
if (fp == NULL) {
printf("Error opening file!\n");
return;
}
while (!feof(fp)) {
char name[MAX_NAME_LEN];
int num_records;
fscanf(fp, "%s %d\n", name, &num_records);
strcpy(students[num_students].name, name);
students[num_students].num_records = num_records;
for (int i = 0; i < num_records; i++) {
char date[MAX_DATE_LEN];
fscanf(fp, "%s %s\n", students[num_students].records[i].name, date);
strcpy(students[num_students].records[i].date, date);
}
num_students++;
}
fclose(fp);
}
// 录入学生的缺课记录
void addRecord() {
char name[MAX_NAME_LEN];
printf("Enter student name: ");
scanf("%s", name);
int found = 0;
for (int i = 0; i < num_students; i++) {
if (strcmp(students[i].name, name) == 0) {
printf("Enter date (YYYY-MM-DD): ");
scanf("%s", students[i].records[students[i].num_records].date);
strcpy(students[i].records[students[i].num_records].name, name);
students[i].num_records++;
printf("Record added.\n");
found = 1;
break;
}
}
if (!found) {
strcpy(students[num_students].name, name);
printf("Enter date (YYYY-MM-DD): ");
scanf("%s", students[num_students].records[0].date);
strcpy(students[num_students].records[0].name, name);
students[num_students].num_records = 1;
num_students++;
printf("Record added.\n");
}
saveData();
}
// 修改某个学生的缺课记录
void editRecord() {
char name[MAX_NAME_LEN];
printf("Enter student name: ");
scanf("%s", name);
int found = 0;
for (int i = 0; i < num_students; i++) {
if (strcmp(students[i].name, name) == 0) {
printf("Enter date to edit (YYYY-MM-DD): ");
char date[MAX_DATE_LEN];
scanf("%s", date);
for (int j = 0; j < students[i].num_records; j++) {
if (strcmp(students[i].records[j].date, date) == 0) {
printf("Enter new date (YYYY-MM-DD): ");
scanf("%s", students[i].records[j].date);
printf("Record updated.\n");
found = 1;
break;
}
}
if (!found) {
printf("Record not found.\n");
}
break;
}
}
if (!found) {
printf("Student not found.\n");
}
saveData();
}
// 查询某个学生的缺课情况
void queryRecord() {
char name[MAX_NAME_LEN];
printf("Enter student name: ");
scanf("%s", name);
int found = 0;
for (int i = 0; i < num_students; i++) {
if (strcmp(students[i].name, name) == 0) {
printf("Absence records for %s:\n", name);
for (int j = 0; j < students[i].num_records; j++) {
printf("%s\n", students[i].records[j].date);
}
found = 1;
break;
}
}
if (!found) {
printf("Student not found.\n");
}
}
// 统计某段时间内旷课学生姓名及旷课次数,按旷课次数由多到少排序
void report() {
char start_date[MAX_DATE_LEN], end_date[MAX_DATE_LEN];
printf("Enter start date (YYYY-MM-DD): ");
scanf("%s", start_date);
printf("Enter end date (YYYY-MM-DD): ");
scanf("%s", end_date);
int num_absences[MAX_STUDENTS] = {0};
for (int i = 0; i < num_students; i++) {
for (int j = 0; j < students[i].num_records; j++) {
if (strcmp(students[i].records[j].date, start_date) >= 0 &&
strcmp(students[i].records[j].date, end_date) <= 0) {
num_absences[i]++;
}
}
}
// 冒泡排序
for (int i = 0; i < num_students - 1; i++) {
for (int j = 0; j < num_students - i - 1; j++) {
if (num_absences[j] < num_absences[j+1]) {
int temp = num_absences[j];
num_absences[j] = num_absences[j+1];
num_absences[j+1] = temp;
Student temp_student = students[j];
students[j] = students[j+1];
students[j+1] = temp_student;
}
}
}
printf("Absentee report from %s to %s:\n", start_date, end_date);
for (int i = 0; i < num_students; i++) {
if (num_absences[i] > 0) {
printf("%s: %d\n", students[i].name, num_absences[i]);
}
}
}
int main() {
loadData();
while (1) {
printf("Enter command (add/edit/query/report/exit): ");
char command[10];
scanf("%s", command);
if (strcmp(command, "add") == 0) {
addRecord();
} else if (strcmp(command, "edit") == 0) {
editRecord();
} else if (strcmp(command, "query") == 0) {
queryRecord();
} else if (strcmp(command, "report") == 0) {
report();
} else if (strcmp(command, "exit") == 0) {
break;
} else {
printf("Invalid command.\n");
}
}
return 0;
}
```
注意,这段代码中的数据持久化方式只是简单地将数据写入文件中,实际应用中可能需要使用数据库等更为高效的方式来存储数据。此外,代码中还有一些不足之处,比如没有对输入进行严格的校验等,需要根据实际需求进行改进。
阅读全文