考勤记录的添加、修改、删除等操作c++
时间: 2023-07-15 17:14:56 浏览: 178
以下是一个简单的考勤记录管理程序,包含添加、修改和删除操作:
```c++
#include <iostream>
#include <string>
#include <vector>
using namespace std;
struct Record {
string name; // 姓名
int id; // 学号
int attendance; // 出勤次数
};
vector<Record> records; // 存储所有考勤记录的数组
// 添加一条考勤记录
void addRecord() {
Record r;
cout << "请输入姓名:";
cin >> r.name;
cout << "请输入学号:";
cin >> r.id;
cout << "请输入出勤次数:";
cin >> r.attendance;
records.push_back(r);
cout << "考勤记录已添加" << endl;
}
// 修改一条考勤记录
void modifyRecord() {
int id;
cout << "请输入要修改的学号:";
cin >> id;
for (int i = 0; i < records.size(); i++) {
if (records[i].id == id) {
cout << "请输入新的姓名:";
cin >> records[i].name;
cout << "请输入新的出勤次数:";
cin >> records[i].attendance;
cout << "考勤记录已修改" << endl;
return;
}
}
cout << "未找到该学号的考勤记录" << endl;
}
// 删除一条考勤记录
void deleteRecord() {
int id;
cout << "请输入要删除的学号:";
cin >> id;
for (int i = 0; i < records.size(); i++) {
if (records[i].id == id) {
records.erase(records.begin() + i);
cout << "考勤记录已删除" << endl;
return;
}
}
cout << "未找到该学号的考勤记录" << endl;
}
// 显示所有考勤记录
void showRecords() {
cout << "姓名\t学号\t出勤次数" << endl;
for (int i = 0; i < records.size(); i++) {
cout << records[i].name << "\t" << records[i].id << "\t" << records[i].attendance << endl;
}
}
int main() {
while (true) {
cout << "请选择操作:" << endl;
cout << "1. 添加考勤记录" << endl;
cout << "2. 修改考勤记录" << endl;
cout << "3. 删除考勤记录" << endl;
cout << "4. 显示所有考勤记录" << endl;
cout << "5. 退出程序" << endl;
int choice;
cin >> choice;
switch (choice) {
case 1:
addRecord();
break;
case 2:
modifyRecord();
break;
case 3:
deleteRecord();
break;
case 4:
showRecords();
break;
case 5:
return 0;
default:
cout << "无效的选项" << endl;
break;
}
}
}
```
注意,这只是一个简单的示例程序,没有做输入合法性检查等处理。在实际应用中,需要根据具体情况进行扩展和优化。
阅读全文