#include <iostream> #include <string> #include <vector> #include <algorithm> #include <iomanip> #include <fstream> using namespace std; const string filename = "text.txt";
class Human { protected: string name; int age; string gender; string Type; public: Human(string& name, int age, string& gender, string& type) : name(name), age(age), gender(gender), Type(type) {}
virtual void show(ostream& cout) { cout << setw(10) << name << setw(10) << age << setw(10) << gender; }
friend ostream& operator<<(ostream& cout, Human& human) { human.show(cout); return cout; }
bool operator<(Human& other) { return age < other.age; }
bool operator>(Human& other) { return age > other.age; }
string getName() { return name; }
int getAge() { return age; }
string getGender() { return gender; }
virtual string getOrg() { return ""; } string getType() { return Type; } };
class Student : public Human { private: string school; public: Student(string& name, int age, string& gender, string& type, string& school) : Human(name, age, gender, type), school(school) {}
void show(ostream& cout) override { Human::show(cout); cout << setw(15) << school; }
friend ostream& operator<<(ostream& cout, Student& student) { student.show(cout); return cout; }
string getOrg() override { return school; } };
class Worker : public Human { private: string company;
public: Worker(string& name, int age, string& gender, string& type, string& company) : Human(name, age, gender, type), company(company) {}
void show(ostream& cout) override { Human::show(cout); cout << setw(15) << company; }
friend ostream& operator<<(ostream& cout, Worker& worker) { worker.show(cout); return cout; }
string getOrg() override { return company; } };
template<typename T> class Vector { private: struct Node { T data; Node* next; Node(T& data) : data(data), next(nullptr) {} };
Node* head; Node* tail;
public: Vector() : head(nullptr), tail(nullptr) {}
void add(T element) { Node* newNode = new Node(element); if (tail != nullptr) { tail->next = newNode; } else { head = newNode; } tail = newNode; }
void dspAll() { cout << setw(10) << "姓名" << setw(10) << "年龄" << setw(10) << "性别" << setw(15) << "学校/公司" << endl; Node* Now = head; while (Now != nullptr) { cout << *(Now->data) << endl; Now = Now->next; } }
vector<T> tovec() { vector<T> vec; Node* Now = head; while (Now != nullptr) { vec.push_back(Now->data); Now = Now->next; } return vec; }
void addVec(vector<T>& vec) { clear(); for (auto& it : vec) { add(it); } }
void clear() { Node* Now = head; while (Now != nullptr) { Node* next = Now->next; delete Now; Now = next; } head = nullptr; tail = nullptr; }
bool remove(string& name) { Node* Now = head; Node* pre = nullptr; while (Now != nullptr) { if (Now->data->getName() == name) { if (pre != nullptr) { pre->next = Now->next; } else { head = Now->next; } if (Now == tail) { tail = pre; } delete Now->data; delete Now; return true; } pre = Now; Now = Now->next; } return false; } };
void addHuman(Vector<Human*>& humans) { string type; while (1) { cout << "输入 s 添加 学生, w 添加 打工人, -1结束添加: "; cin >> type; if (type == "-1") return; else if (type != "s" && type != "w") { cout << "无效的选择,请重新输入。" << endl; continue; } string name, gender, school, company; int age; cout << "输入姓名: "; cin >> name; cout << "输入年龄: "; cin >> age; cout << "输入性别: "; cin >> gender;
if (type == "s") { cout << "输入学校: "; cin >> school; humans.add(new Student(name, age, gender, type, school)); } else { cout << "输入公司: "; cin >> company; humans.add(new Worker(name, age, gender, type, company)); }
} }
void Dsp(Vector<Human*>& humans) { cout << "\n所有人信息:\n"; humans.dspAll(); }
void dspsorted(Vector<Human*>& humans) { auto vec = humans.tovec(); sort(vec.begin(), vec.end(), [](Human* a, Human* b) { return *a < *b; } ); Vector<Human*> sortedHumans; sortedHumans.addVec(vec); cout << "\n按年龄排序后的人信息:\n"; sortedHumans.dspAll(); }
void DeleteHuman(Vector<Human*>& humans) { string dname; cout << "\n输入要删除的人的姓名: "; cin >> dname;
if (humans.remove(dname)) { cout << "\n删除后的人信息:\n"; humans.dspAll(); } else { cout << "未找到该人!" << endl; } }
void savedata(Vector<Human*>& humans) { ofstream file(filename); if (file.is_open()) {
auto vec = humans.tovec(); for (auto& human : vec) { file << human->getType() << ' ' << human->getName() << ' ' << human->getAge() << ' ' << human->getGender() << ' ' << human->getOrg() << endl; } file.close(); cout << "数据已保存到 " << filename << endl; } else { cout << "无法打开文件 " << filename << endl; } }
void loaddate(Vector<Human*>& humans) { ifstream file(filename); if (file.is_open()) { humans.clear(); string type, name, gender, Org; int age; while (file >> type >> name >> age >> gender >> ws) { getline(file, Org); if (type == "s") { humans.add(new Student(name, age, gender, type, Org)); } else if (type == "w") { humans.add(new Worker(name, age, gender, type, Org)); } } file.close(); cout << "数据已从 " << filename << " 读取" << endl; } else { cout << "无法打开文件 " << filename << endl; } }
int main() { Vector<Human*> humans; int choice;
while (true) { cout << "-----------------------------------------------\n"; cout << "选择操作:\n"; cout << "1. 添加人\n"; cout << "2. 打印所有信息\n"; cout << "3. 按年龄排序后输出信息\n"; cout << "4. 删除一个人的信息\n"; cout << "5. 从文件加载数据\n"; cout << "6. 保存数据到文件\n"; cout << "7. 退出\n"; cout << "输入选择: "; cin >> choice;
switch (choice) { case 1: addHuman(humans); break; case 2: Dsp(humans); break; case 3: dspsorted(humans); break; case 4: DeleteHuman(humans); break; case 5: loaddate(humans); break; case 6: savedata(humans); break; case 7: return 0; default: cout << "无效的选择,请重新输入。" << endl; } }
return 0; }
|