请用C语言编写一个学生考勤系统
时间: 2023-11-13 07:03:03 浏览: 147
学生考勤系统(C语言)
好的,我可以帮您解答这个问题。以下是一个简单的学生考勤系统的 C 语言代码示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STUDENTS 50
struct student {
int id;
char name[20];
int present;
};
int num_students = 0;
struct student students[MAX_STUDENTS];
void add_student();
void mark_attendance();
void display_attendance();
int main() {
int choice;
while (1) {
printf("\n---------------------\n");
printf("1. Add student\n");
printf("2. Mark attendance\n");
printf("3. Display attendance\n");
printf("4. Exit\n");
printf("---------------------\n\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
add_student();
break;
case 2:
mark_attendance();
break;
case 3:
display_attendance();
break;
case 4:
exit(0);
default:
printf("Invalid choice!\n");
}
}
return 0;
}
void add_student() {
if (num_students >= MAX_STUDENTS) {
printf("Maximum students reached!\n");
return;
}
struct student s;
s.id = num_students + 1;
printf("Enter student name: ");
scanf("%s", s.name);
s.present = 0;
students[num_students++] = s;
printf("Student added successfully!\n");
}
void mark_attendance() {
int id;
printf("Enter student ID: ");
scanf("%d", &id);
for (int i = 0; i < num_students; i++) {
if (students[i].id == id) {
students[i].present = 1;
printf("Attendance marked for student '%s'!\n", students[i].name);
return;
}
}
printf("Invalid student ID!\n");
}
void display_attendance() {
printf("ID\tName\tAttendance\n");
printf("------------------------------\n");
for (int i = 0; i < num_students; i++) {
printf("%d\t%s\t", students[i].id, students[i].name);
if (students[i].present) {
printf("Present\n");
} else {
printf("Absent\n");
}
}
}
```
这个例子中,我们使用结构体存储每个学生的信息,包括 ID、姓名以及出勤情况。通过添加学生、标记出勤和显示出勤等功能来实现一个简单的学生考勤系统。
阅读全文