c语言学生考勤系统代码
时间: 2023-08-01 08:12:49 浏览: 102
以下是一个简单的 C 语言学生考勤系统的代码示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STUDENTS 100
#define MAX_NAME_LENGTH 50
struct Student {
char name[MAX_NAME_LENGTH];
int id;
int attendance;
};
void addStudent(struct Student students[], int *numStudents);
void printAttendance(struct Student students[], int numStudents);
void updateAttendance(struct Student students[], int numStudents);
int main() {
struct Student students[MAX_STUDENTS];
int numStudents = 0;
int choice;
while (1) {
printf("\n1. Add student\n");
printf("2. Print attendance\n");
printf("3. Update attendance\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
addStudent(students, &numStudents);
break;
case 2:
printAttendance(students, numStudents);
break;
case 3:
updateAttendance(students, numStudents);
break;
case 4:
exit(0);
default:
printf("\nInvalid choice. Try again.\n");
break;
}
}
return 0;
}
void addStudent(struct Student students[], int *numStudents) {
if (*numStudents == MAX_STUDENTS) {
printf("\nMaximum number of students reached.\n");
return;
}
struct Student newStudent;
printf("\nEnter student name: ");
scanf("%s", newStudent.name);
printf("Enter student ID: ");
scanf("%d", &newStudent.id);
newStudent.attendance = 0;
students[*numStudents] = newStudent;
(*numStudents)++;
printf("\nStudent added successfully.\n");
}
void printAttendance(struct Student students[], int numStudents) {
printf("\nName\tID\tAttendance\n");
for (int i = 0; i < numStudents; i++) {
printf("%s\t%d\t%d\n", students[i].name, students[i].id, students[i].attendance);
}
}
void updateAttendance(struct Student students[], int numStudents) {
int id;
printf("\nEnter student ID: ");
scanf("%d", &id);
int found = 0;
for (int i = 0; i < numStudents; i++) {
if (students[i].id == id) {
students[i].attendance++;
printf("\nAttendance updated successfully.\n");
found = 1;
break;
}
}
if (!found) {
printf("\nStudent not found.\n");
}
}
```
这个程序使用一个 `struct` 结构体来存储每个学生的姓名、ID 和出勤情况。主函数通过一个无限循环来提供用户菜单,用户可以选择添加学生、打印出勤情况或更新某个学生的出勤情况。
阅读全文