#include <iostream>
#include <fstream>
#include <list>
#include <algorithm>
#include <string>
using namespace std;
const string BAIJIAXING[] = { "赵","钱","孙","李","周","吴","郑","王","冯",
"陈","褚","卫","蒋","沈","韩","杨","朱","秦",
"尤","许","何","吕","施","张","孔","曹","严",
"华","金","魏","陶","姜","戚","谢","邹","喻" };
class Student {
public:
// 默认构造
Student(): Name("学生"),Age(18),Id(0),Late_number(0){}
// 获取方法
string &Get_Name() { return this->Name; } // 获取学生姓名
int &Get_Age() { return this->Age; } // 获取学生年龄
int &Get_Id() { return this->Id; } // 获取学生学号
int &Get_Late() { return this->Late_number; } // 获取学生迟到次数
// 设置方法
void Set_Name(string Name_) { this->Name = Name_; } // 设置学生姓名
void Set_Age(int Age_) { this->Age = Age; } // 获取学生年龄
void Set_Id(int Id) { this->Id = Id; } // 获取学生学号
void Set_Late(int Late) { this->Late_number = Late; } // 获取学生迟到次数
private:
string Name; // 姓名
int Age;// 年龄
int Id; // 学号
int Late_number; // 考勤的类型应该有哪些!我就只写个迟到次数了
};
// 回调 排序规则
bool Mycompare(Student &v1, Student &v2) {
return v1.Get_Late() > v2.Get_Late();
}
class Late{
public:
Late(int val) {
for (int i = 0; i < val; i++) {
Student s;
s.Set_Name((s.Get_Name()) += BAIJIAXING[rand() % 35]); // 初始化学生姓名
s.Set_Id(i); // 初始化学号
Student_list.push_back(s);
}
}
void Student_sort() { // 排序
// 按照迟到次数,从大到小排
Student_list.sort(Mycompare);
}
void Student_Add(int Id, int val) { // 增加迟到次数
for (list<Student>::iterator i = Student_list.begin(); i != Student_list.end(); i++) {
if (i->Get_Id() == Id) {
i->Set_Late(i->Get_Late() += val);
}
}
}
bool Student_Delete(int Id, int val) { // 删除迟到次数
for (list<Student>::iterator i = Student_list.begin(); i != Student_list.end(); i++) {
if (i->Get_Id() == Id) {
if ((i->Get_Late() - val) < 0) {
return false;
}
else {
return true;
}
}
}
}
int Student_Seek(int Id) { //查询迟到次数
for (list<Student>::iterator i = Student_list.begin(); i != Student_list.end(); i++) {
if (i->Get_Id() == Id) {
return i->Get_Late();
}
}
}
bool Student_Keep() { // 保存资料
ofstream s_("D:\\Student.txt", ios::out);
if (s_) {
for (list<Student>::iterator i = Student_list.begin(); i != Student_list.end(); i++) {
s_ << "学生: " << i->Get_Name() << endl;
s_ << "学号: " << i->Get_Id() << endl;;
s_ << "年龄: " << i->Get_Age() << endl;
s_ << "迟到次数: " << i->Get_Late() << endl;
s_ << "=====================================" << endl;
}
return true;
}
else {
return false;
}
s_.close();
}
private:
list<Student> Student_list;
};
// 业务层
void Fun0() {
cout << "\n" << endl;
cout << "\t\t" << "欢迎使用学生考勤系统" << endl;
cout << "\t\t" << "1. 增加学生迟到次数" << endl;
cout << "\t\t" << "2. 减少学生迟到次数" << endl;
cout << "\t\t" << "3. 查询学生迟到次数" << endl;
cout << "\t\t" << "4. 保存学生考勤信息" << endl;
cout << "\t\t" << "5. 退出学生考勤信息" << endl;
}
bool Fun2(int v1, int v2) {
return v1 < v2;
}
void Fun1() {
int Student_sum = 0;
cout << "请告诉我,本班有多少名学生: ";
cin >> Student_sum;
Late s(Student_sum);
// 这里我就当做都是正经用户了,就不判断值的非法性了。
int val;
while (true) {
Fun0();
cin >> val;
switch (val)
{
case 1:
{
system("cls");
int val, id;
cout << "请输入学生的学号和增加迟到的次数: ";
cin >> id >> val;
while (Fun2(Student_sum, id)) {
cout << "请正确输入学号: ";
cin >> id;
}
s.Student_Add(id, val);
cout << "操作成功!" << endl;
}
break;
case 2:
{
system("cls");
int val, id;
cout << "请输入学生的学号和要减少迟到的次数: ";
cin >> id >> val;
while (Fun2(Student_sum, id)) {
cout << "请正确输入学号: ";
cin >> id;
}
if (s.Student_Delete(id, val)) {
cout << "操作成功" << endl;
}
else {
cout << "操作失败" << endl;
}
}
break;
case 3:
{
system("cls");
int id, sum;
cout << "请输入欲查找学生的学号:";
cin >> id;
while (Fun2(Student_sum, id)) {
cout << "请正确输入学号: ";
cin >> id;
}
sum = s.Student_Seek(id);
cout << "学号:" << id << "学生 共迟到" << sum << "次!" << endl;
}
break;
case 4:
{
system("cls");
for (int i = 0; i < Student_sum; i++) {
if (s.Student_Keep()) {
cout << "操作成功" << endl;
}
else {
cout << "操作失败!" << endl;
}
}
}
break;
case 5: return; break;
default:
cout << "输入错误" << endl;
system("cls");
break;
}
}
}
int main() {
Fun1();
system("pause");
}