用c++写出学院考勤管理系统
时间: 2023-07-19 17:18:21 浏览: 139
以下是一个C++实现的学院考勤管理系统示例代码:
```cpp
#include <iostream>
#include <cstring>
#define MAX_STUDENTS 100
#define MAX_NAME_LENGTH 50
#define MAX_ID_LENGTH 20
using namespace std;
struct student {
char name[MAX_NAME_LENGTH];
char id[MAX_ID_LENGTH];
int presence_count;
};
void add_student(student *students, int &student_count) {
if (student_count >= MAX_STUDENTS) {
cout << "The maximum number of students has been reached." << endl;
return;
}
student new_student;
cout << "Enter student name: ";
cin >> new_student.name;
cout << "Enter student ID: ";
cin >> new_student.id;
new_student.presence_count = 0;
students[student_count] = new_student;
student_count++;
cout << "Student added successfully." << endl;
}
void mark_presence(student *students, int student_count) {
char id[MAX_ID_LENGTH];
cout << "Enter student ID: ";
cin >> id;
for (int i = 0; i < student_count; i++) {
if (strcmp(students[i].id, id) == 0) {
students[i].presence_count++;
cout << "Presence marked for student " << students[i].name << "." << endl;
return;
}
}
cout << "No student found with ID " << id << "." << endl;
}
void print_report(student *students, int student_count) {
cout << "Student ID\tName\tPresence Count" << endl;
for (int i = 0; i < student_count; i++) {
cout << students[i].id << "\t" << students[i].name << "\t" << students[i].presence_count << endl;
}
}
int main() {
student students[MAX_STUDENTS];
int student_count = 0;
int choice;
while (true) {
cout << endl << "1. Add student" << endl;
cout << "2. Mark presence" << endl;
cout << "3. Print report" << endl;
cout << "4. Exit" << endl;
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
add_student(students, student_count);
break;
case 2:
mark_presence(students, student_count);
break;
case 3:
print_report(students, student_count);
break;
case 4:
exit(0);
default:
cout << "Invalid choice." << endl;
}
cin.clear();
cin.ignore(10000, '\n');
}
return 0;
}
```
该程序与之前提供的C语言示例的实现思路类似,但使用了C++的特性,如结构体和字符串处理函数。程序中还添加了输入缓冲区的清除操作,以避免用户输入无效字符。
阅读全文