考勤系统设计 功能:设计一考勤系统。考勤信息记录了学生的缺课情况,它包括:缺课日期、第几节课、课程名称、学生姓名、学生学号、缺课类型(迟到、早退、请假及旷课)。系统具有以下功能: 1、录入学生的缺课记录; 2、修改某个学生的缺课记录; 3、查询某个学生的缺课情况; 4、统计某段时间内,旷课学生姓名及旷课次数,按旷课次数由多到少排序; 5、统计某段时间内,有学生旷课的课程及旷课人次,按旷课人次由多到少排序; 6、系统以菜单方式工作。c语言
时间: 2023-07-10 17:03:58 浏览: 94
以下是一个简单的C语言考勤系统设计:
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STUDENTS 100 // 最多学生数
#define MAX_COURSES 10 // 最多课程数
#define MAX_ABSENCES 20 // 最多缺课记录数
struct Absence { // 缺课记录结构体
int date; // 缺课日期
int time; // 第几节课
char type[10]; // 缺课类型
};
struct Student { // 学生结构体
char name[20]; // 学生姓名
char id[20]; // 学生学号
struct Absence absences[MAX_ABSENCES]; // 缺课记录数组
int num_absences; // 缺课记录数
};
struct Course { // 课程结构体
char name[20]; // 课程名称
struct Student students[MAX_STUDENTS]; // 学生数组
int num_students; // 学生数
};
struct School { // 学校结构体
struct Course courses[MAX_COURSES]; // 课程数组
int num_courses; // 课程数
};
void add_absence(struct Absence *absence) { // 录入缺课记录
printf("请输入缺课日期:");
scanf("%d", &(absence->date));
printf("请输入第几节课:");
scanf("%d", &(absence->time));
printf("请输入缺课类型(迟到、早退、请假或旷课):");
scanf("%s", absence->type);
}
void add_student(struct Course *course) { // 录入学生信息
struct Student student;
printf("请输入学生姓名:");
scanf("%s", student.name);
printf("请输入学生学号:");
scanf("%s", student.id);
course->students[course->num_students] = student;
course->num_students++;
}
void add_course(struct School *school) { // 录入课程信息
struct Course course;
printf("请输入课程名称:");
scanf("%s", course.name);
school->courses[school->num_courses] = course;
school->num_courses++;
}
void add_absence_record(struct School *school) { // 录入缺课记录
char course_name[20], student_id[20];
printf("请输入课程名称:");
scanf("%s", course_name);
printf("请输入学生学号:");
scanf("%s", student_id);
for (int i = 0; i < school->num_courses; i++) {
if (strcmp(school->courses[i].name, course_name) == 0) { // 找到对应课程
for (int j = 0; j < school->courses[i].num_students; j++) {
if (strcmp(school->courses[i].students[j].id, student_id) == 0) { // 找到对应学生
struct Absence absence;
add_absence(&absence);
school->courses[i].students[j].absences[school->courses[i].students[j].num_absences] = absence;
school->courses[i].students[j].num_absences++;
printf("缺课记录已添加。\n");
return;
}
}
printf("未找到该学生。\n");
return;
}
}
printf("未找到该课程。\n");
}
void modify_absence_record(struct School *school) { // 修改缺课记录
char course_name[20], student_id[20];
printf("请输入课程名称:");
scanf("%s", course_name);
printf("请输入学生学号:");
scanf("%s", student_id);
for (int i = 0; i < school->num_courses; i++) {
if (strcmp(school->courses[i].name, course_name) == 0) { // 找到对应课程
for (int j = 0; j < school->courses[i].num_students; j++) {
if (strcmp(school->courses[i].students[j].id, student_id) == 0) { // 找到对应学生
int absence_index;
printf("请输入要修改的缺课记录编号(从1开始):");
scanf("%d", &absence_index);
if (absence_index > 0 && absence_index <= school->courses[i].students[j].num_absences) {
struct Absence absence;
add_absence(&absence);
school->courses[i].students[j].absences[absence_index - 1] = absence;
printf("缺课记录已修改。\n");
return;
}
printf("无效的缺课记录编号。\n");
return;
}
}
printf("未找到该学生。\n");
return;
}
}
printf("未找到该课程。\n");
}
void query_absences(struct School *school) { // 查询学生缺课情况
char course_name[20], student_id[20];
printf("请输入课程名称:");
scanf("%s", course_name);
printf("请输入学生学号:");
scanf("%s", student_id);
for (int i = 0; i < school->num_courses; i++) {
if (strcmp(school->courses[i].name, course_name) == 0) { // 找到对应课程
for (int j = 0; j < school->courses[i].num_students; j++) {
if (strcmp(school->courses[i].students[j].id, student_id) == 0) { // 找到对应学生
struct Student student = school->courses[i].students[j];
printf("学生姓名:%s\n", student.name);
printf("学生学号:%s\n", student.id);
printf("缺课记录:\n");
for (int k = 0; k < student.num_absences; k++) {
struct Absence absence = student.absences[k];
printf("%d\t%d\t%s\n", absence.date, absence.time, absence.type);
}
return;
}
}
printf("未找到该学生。\n");
return;
}
}
printf("未找到该课程。\n");
}
void sort_students_by_absences(struct School *school) { // 按旷课次数排序学生
char course_name[20];
printf("请输入课程名称:");
scanf("%s", course_name);
for (int i = 0; i < school->num_courses; i++) {
if (strcmp(school->courses[i].name, course_name) == 0) { // 找到对应课程
struct Student students[MAX_STUDENTS];
int num_students = 0;
for (int j = 0; j < school->courses[i].num_students; j++) {
int num_absences = 0;
for (int k = 0; k < school->courses[i].students[j].num_absences; k++) {
if (strcmp(school->courses[i].students[j].absences[k].type, "旷课") == 0) {
num_absences++;
}
}
if (num_absences > 0) {
students[num_students] = school->courses[i].students[j];
students[num_students].num_absences = num_absences;
num_students++;
}
}
for (int j = 0; j < num_students - 1; j++) { // 冒泡排序
for (int k = 0; k < num_students - j - 1; k++) {
if (students[k].num_absences < students[k + 1].num_absences) {
struct Student temp = students[k];
students[k] = students[k + 1];
students[k + 1] = temp;
}
}
}
printf("旷课学生姓名\t旷课次数\n");
for (int j = 0; j < num_students; j++) {
printf("%s\t%d\n", students[j].name, students[j].num_absences);
}
return;
}
}
printf("未找到该课程。\n");
}
void sort_courses_by_absences(struct School *school) { // 按旷课人次排序课程
int start_date, end_date;
printf("请输入起始日期:");
scanf("%d", &start_date);
printf("请输入截止日期:");
scanf("%d", &end_date);
struct Course courses[MAX_COURSES];
int num_courses = 0;
for (int i = 0; i < school->num_courses; i++) {
int num_absences = 0;
for (int j = 0; j < school->courses[i].num_students; j++) {
for (int k = 0; k < school->courses[i].students[j].num_absences; k++) {
if (strcmp(school->courses[i].students[j].absences[k].type, "旷课") == 0 && school->courses[i].students[j].absences[k].date >= start_date && school->courses[i].students[j].absences[k].date <= end_date) {
num_absences++;
}
}
}
if (num_absences > 0) {
courses[num_courses] = school->courses[i];
courses[num_courses].num_students = num_absences;
num_courses++;
}
}
for (int i = 0; i < num_courses - 1; i++) { // 冒泡排序
for (int j = 0; j < num_courses - i - 1; j++) {
if (courses[j].num_students < courses[j + 1].num_students) {
struct Course temp = courses[j];
courses[j] = courses[j + 1];
courses[j + 1] = temp;
}
}
}
printf("旷课课程名称\t旷课人次\n");
for (int i = 0; i < num_courses; i++) {
printf("%s\t%d\n", courses[i].name, courses[i].num_students);
}
}
void print_menu() { // 打印菜单
printf("1. 录入学生的缺课记录\n");
printf("2. 修改某个学生的缺课记录\n");
printf("3. 查询某个学生的缺课情况\n");
printf("4. 统计某段时间内,旷课学生姓名及旷课次数,按旷课次数由多到少排序\n");
printf("5. 统计某段时间内,有学生旷课的课程及旷课人次,按旷课人次由多到少排序\n");
printf("6. 退出\n");
}
int main() {
struct School school;
school.num_courses = 0;
int choice;
do {
print_menu();
printf("请选择操作:");
scanf("%d", &choice);
switch (choice) {
case 1:
add_absence_record(&school);
break;
case 2:
modify_absence_record(&school);
break;
case 3:
query_absences(&school);
break;
case 4:
sort_students_by_absences(&school);
break;
case 5:
sort_courses_by_absences(&school);
break;
case 6:
printf("谢谢使用!\n");
return 0;
default:
printf("无效的选择。\n");
}
} while (1);
}
```
这个考勤系统可以实现录入学生的缺课记录、修改某个学生的缺课记录、查询某个学生的缺课情况、统计某段时间内旷课学生姓名及旷课次数,以及统计某段时间内有学生旷课的课程及旷课人次,并且支持菜单方式工作。
阅读全文