学生签到系统代码怎么写
时间: 2024-03-25 21:39:43 浏览: 14
学生签到系统的代码需要根据实际需求进行设计和编写,以下是一个简单的学生签到系统代码示例:
```
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Student {
public:
string name;
int id;
bool isPresent;
Student(string n, int i) {
name = n;
id = i;
isPresent = false;
}
};
class Course {
public:
string name;
vector<Student> students;
Course(string n) {
name = n;
}
void addStudent(string name, int id) {
Student s(name, id);
students.push_back(s);
}
void markPresent(int id) {
for (int i = 0; i < students.size(); i++) {
if (students[i].id == id) {
students[i].isPresent = true;
cout << students[i].name << " is present." << endl;
return;
}
}
cout << "Invalid ID." << endl;
}
void printAttendance() {
cout << "Attendance for " << name << ":" << endl;
for (int i = 0; i < students.size(); i++) {
cout << students[i].name << " - ";
if (students[i].isPresent) {
cout << "Present" << endl;
} else {
cout << "Absent" << endl;
}
}
}
};
int main() {
Course c("Math");
c.addStudent("Alice", 1001);
c.addStudent("Bob", 1002);
c.addStudent("Charlie", 1003);
c.markPresent(1002);
c.markPresent(1001);
c.printAttendance();
return 0;
}
```
上述代码使用了两个类,`Student` 和 `Course`,其中 `Student` 类表示一个学生,包含姓名、学号和是否出席三个属性;`Course` 类表示一个课程,包含课程名称和学生列表两个属性,以及添加学生、标记出席和打印出席情况三个方法。在 `main` 函数中,首先创建了一个名为 `Math` 的课程,并添加了三个学生。然后通过调用 `markPresent` 方法标记了两个学生出席。最后调用 `printAttendance` 方法打印出席情况。