学生考勤系统c语言代码
时间: 2023-09-16 10:07:55 浏览: 99
以下是一个简单的学生考勤系统的C语言代码,仅供参考:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STUDENTS 100
#define MAX_NAME_LENGTH 20
typedef struct {
char name[MAX_NAME_LENGTH];
int id;
int attendance_count;
} Student;
int find_student(Student students[], int count, int id) {
for (int i = 0; i < count; i++) {
if (students[i].id == id) {
return i;
}
}
return -1;
}
void record_attendance(Student students[], int count) {
int id;
printf("Enter student ID: ");
scanf("%d", &id);
int index = find_student(students, count, id);
if (index == -1) {
printf("Student not found.\n");
return;
}
students[index].attendance_count++;
printf("Attendance recorded for %s.\n", students[index].name);
}
void print_summary(Student students[], int count) {
printf("Attendance Summary:\n");
for (int i = 0; i < count; i++) {
printf("%s: %d\n", students[i].name, students[i].attendance_count);
}
}
int main() {
Student students[MAX_STUDENTS];
int count = 0;
int choice;
do {
printf("1. Add student\n");
printf("2. Record attendance\n");
printf("3. Print attendance summary\n");
printf("4. Quit\n");
printf("Enter choice: ");
scanf("%d", &choice);
switch (choice) {
case 1: {
if (count >= MAX_STUDENTS) {
printf("Maximum number of students reached.\n");
break;
}
Student student;
printf("Enter student name: ");
scanf("%s", student.name);
printf("Enter student ID: ");
scanf("%d", &student.id);
student.attendance_count = 0;
students[count] = student;
count++;
printf("Student added.\n");
break;
}
case 2:
record_attendance(students, count);
break;
case 3:
print_summary(students, count);
break;
case 4:
printf("Goodbye!\n");
break;
default:
printf("Invalid choice.\n");
break;
}
printf("\n");
} while (choice != 4);
return 0;
}
```
此代码实现了以下功能:
- 添加学生(姓名和ID)
- 记录考勤
- 打印考勤汇总信息
- 退出系统
请注意,此代码仅作为参考,实际应用中可能需要更复杂的实现。
阅读全文