用c语言写一个学生考勤系统,包含以下功能1、 录入学生的缺课记录。 2、 修改某个学生的缺课记录。 3、 查询某个学生的缺课情况。 统计某段时间内,旷课学生姓名及旷课次数,按旷课次数由多到少排序。
时间: 2024-02-19 14:03:31 浏览: 50
好的,我可以为您解答这个问题。
以下是一个简单的学生考勤系统的C代码,它包含了您所提到的所有功能:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STUDENTS 100
#define MAX_NAME_LENGTH 50
#define MAX_ABSENCES 20
typedef struct {
char name[MAX_NAME_LENGTH];
int absences[MAX_ABSENCES];
int num_absences;
} Student;
Student students[MAX_STUDENTS];
int num_students = 0;
// Function declarations
void add_student();
void enter_absences();
void modify_absences();
void display_absences();
void display_statistics();
// Main function
int main() {
int choice;
do {
// Display menu
printf("\n\n");
printf("1. Add student\n");
printf("2. Enter absences\n");
printf("3. Modify absences\n");
printf("4. Display absences\n");
printf("5. Display statistics\n");
printf("6. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
// Call appropriate function based on user choice
switch (choice) {
case 1:
add_student();
break;
case 2:
enter_absences();
break;
case 3:
modify_absences();
break;
case 4:
display_absences();
break;
case 5:
display_statistics();
break;
case 6:
exit(0);
default:
printf("Invalid choice!\n");
}
} while (1);
return 0;
}
// Function to add a new student
void add_student() {
if (num_students == MAX_STUDENTS) {
printf("Maximum number of students reached!\n");
return;
}
printf("Enter name of student: ");
scanf("%s", students[num_students].name);
students[num_students].num_absences = 0;
printf("Student added successfully!\n");
num_students++;
}
// Function to enter absences for a student
void enter_absences() {
char name[MAX_NAME_LENGTH];
int i, j, found = 0;
printf("Enter name of student: ");
scanf("%s", name);
for (i = 0; i < num_students; i++) {
if (strcmp(name, students[i].name) == 0) {
found = 1;
break;
}
}
if (!found) {
printf("Student not found!\n");
return;
}
printf("Enter number of absences: ");
scanf("%d", &students[i].num_absences);
printf("Enter dates of absences (separated by spaces): ");
for (j = 0; j < students[i].num_absences; j++) {
scanf("%d", &students[i].absences[j]);
}
printf("Absences entered successfully!\n");
}
// Function to modify absences for a student
void modify_absences() {
char name[MAX_NAME_LENGTH];
int i, j, found = 0;
printf("Enter name of student: ");
scanf("%s", name);
for (i = 0; i < num_students; i++) {
if (strcmp(name, students[i].name) == 0) {
found = 1;
break;
}
}
if (!found) {
printf("Student not found!\n");
return;
}
printf("Enter index of absence to modify: ");
scanf("%d", &j);
if (j < 0 || j >= students[i].num_absences) {
printf("Invalid index!\n");
return;
}
printf("Enter new date for absence: ");
scanf("%d", &students[i].absences[j]);
printf("Absence modified successfully!\n");
}
// Function to display absences for a student
void display_absences() {
char name[MAX_NAME_LENGTH];
int i, j, found = 0;
printf("Enter name of student: ");
scanf("%s", name);
for (i = 0; i < num_students; i++) {
if (strcmp(name, students[i].name) == 0) {
found = 1;
break;
}
}
if (!found) {
printf("Student not found!\n");
return;
}
printf("Absences for %s:\n", students[i].name);
for (j = 0; j < students[i].num_absences; j++) {
printf("%d\n", students[i].absences[j]);
}
}
// Function to display statistics
void display_statistics() {
int i, j, k, l, sum, max_sum, max_index;
int num_absences[num_students];
char absent_students[num_students][MAX_NAME_LENGTH];
if (num_students == 0) {
printf("No students added yet!\n");
return;
}
for (i = 0; i < num_students; i++) {
sum = 0;
for (j = 0; j < students[i].num_absences; j++) {
sum += students[i].absences[j];
}
num_absences[i] = sum;
strcpy(absent_students[i], students[i].name);
}
printf("Enter start and end dates (inclusive): ");
scanf("%d %d", &k, &l);
printf("Absent students:\n");
for (i = 0; i < num_students; i++) {
sum = 0;
for (j = 0; j < students[i].num_absences; j++) {
if (students[i].absences[j] >= k && students[i].absences[j] <= l) {
sum++;
}
}
num_absences[i] = sum;
}
for (i = 0; i < num_students - 1; i++) {
max_sum = num_absences[i];
max_index = i;
for (j = i + 1; j < num_students; j++) {
if (num_absences[j] > max_sum) {
max_sum = num_absences[j];
max_index = j;
}
}
if (max_index != i) {
sum = num_absences[i];
num_absences[i] = num_absences[max_index];
num_absences[max_index] = sum;
strcpy(name, absent_students[i]);
strcpy(absent_students[i], absent_students[max_index]);
strcpy(absent_students[max_index], name);
}
}
for (i = 0; i < num_students; i++) {
printf("%s: %d\n", absent_students[i], num_absences[i]);
}
}
```
这是一个基本的学生考勤系统,它可以添加学生、录入缺课记录、修改缺课记录、查询缺课情况以及统计缺课情况。
希望我写的代码能对您有所帮助!
阅读全文