用c语言写一个学生宿舍考勤代码
时间: 2024-09-06 10:08:16 浏览: 98
C语言课程设计——学生考勤管理系统.doc
5星 · 资源好评率100%
在C语言中编写一个简单的学生宿舍考勤系统代码,我们可以创建一个结构体表示学生信息,并设计函数来进行注册、签到和查询考勤记录。这里是一个简化的示例:
```c
#include <stdio.h>
#include <stdlib.h>
#define MAX_STUDENTS 10
// 宿舍学生结构体
typedef struct {
char name[50];
int id;
int attendance; // 初始假定签到次数为0
} Student;
// 学生列表
Student students[MAX_STUDENTS];
// 注册新学生
void register_student(char *name, int id) {
if (students_count < MAX_STUDENTS) { // 检查列表是否满
Student new_student = {name, id, 0};
students[students_count] = new_student;
printf("Student %s registered with ID %d\n", name, id);
students_count++;
} else {
printf("List is full, cannot register.\n");
}
}
// 签到
void check_in(int student_id) {
for (int i = 0; i < students_count; i++) {
if (students[i].id == student_id) {
students[i].attendance++;
printf("Student %s checked in (total attendances: %d)\n", students[i].name, students[i].attendance);
break;
}
}
}
// 查询考勤
void query_attendance(int student_id) {
for (int i = 0; i < students_count; i++) {
if (students[i].id == student_id) {
printf("Student %s has attended %d times\n", students[i].name, students[i].attendance);
return;
}
}
printf("Student not found.\n");
}
int main() {
int choice;
do {
printf("\n--\n");
printf("1. 注册新学生\n");
printf("2. 签到\n");
printf("3. 查询考勤\n");
printf("4. 退出\n");
scanf("%d", &choice);
switch (choice) {
case 1:
char name[50];
int id;
printf("Enter student's name and ID: ");
scanf("%s%d", name, &id);
register_student(name, id);
break;
case 2:
int student_id;
printf("Enter student ID to sign in: ");
scanf("%d", &student_id);
check_in(student_id);
break;
case 3:
int search_id;
printf("Enter student ID to check attendance: ");
scanf("%d", &search_id);
query_attendance(search_id);
break;
case 4:
printf("Exiting the system...\n");
break;
default:
printf("Invalid option, please try again.\n");
}
} while (choice != 4); // 如果用户选择4,则退出循环
return 0;
}
```
阅读全文