关于#c++#的问题,如何解决?

c++
设计人员信息管理系统,能够实现人员信息的添加、修改、删除和查询等功能。利用文件实现信息的读写。


#define FILENAME "职工信息.mxy"
#include<iostream>
#include<fstream>
#include<string>
#include<algorithm>
using namespace std;

class Staff
{
public:
    virtual void ShowInfo() = 0;
    virtual string GetWork() = 0;
    int m_id;
    string m_tel;
    int m_salary;
    string m_name;
    string m_sex;
    int m_type;
};

class Boss :public Staff
{
public:
    Boss(int id, string name, string sex, string tel, int type, int salary);
    void ShowInfo();
    string GetWork();
};

class Manager :public Staff
{
public:
    Manager(int id, string name, string sex, string tel, int type, int salary);
    void ShowInfo();
    string GetWork();
};

class Employee :public Staff
{
public:
    Employee(int id, string name, string sex, string tel, int type, int salary);
    void ShowInfo();
    string GetWork();
};

class StaffManagement
{
public:
    StaffManagement();
    void Menu();
    void Exit();
    bool CheckId(int n);
    void AddEmployee();
    void Save();
    bool CheckFile;
    int GetFileSize();
    void InitFile();
    void ShowEmployee();
    int GetRank(int n);
    void NameSearch(string name);
    void DeleteEmployee();
    void ModifyEmployee();
    void SearchEmployee();
    void SortEmployee();
    void DestoryedInfo();
    int P_n;
    Staff**P_arr;
    ~StaffManagement();
};

enum
{
    EXIT,
    ADD,
    DISPLAY,
    DELETE,
    MODIFY,
    SEARCH,
    SAVE,
    SORT,
    DESTORY
};
Boss::Boss(int id, string name, string sex, string tel, int type, int salary)
{
    this->m_id = id;
    this->m_name = name;
    this->m_type = type;
    this->m_sex = sex;
    this->m_tel = tel;
    this->m_salary = salary;
}
void Boss::ShowInfo()
{
    cout << "职工编号:" << this->m_id
        << "\t职工姓名:" << this->m_name
        << "\t职工性别:" << this->m_sex
        << "\t职工电话:" << this->m_tel
        << "\t职工薪资:" << this->m_salary
        << "\t职工岗位:" << this->GetWork()
        << "\t岗位职责:管理公司所有事务" << endl;
}
string Boss::GetWork()
{
    return string("老板");
}

Manager::Manager(int id, string name, string sex, string tel, int type, int salary)
{
    this->m_id = id;
    this->m_name = name;
    this->m_type = type;
    this->m_sex = sex;
    this->m_tel = tel;
    this->m_salary = salary;
}
void Manager::ShowInfo()
{
    cout << "职工编号:" << this->m_id
        << "\t职工姓名:" << this->m_name
        << "\t职工性别:" << this->m_sex
        << "\t职工电话:" << this->m_tel
        << "\t职工薪资:" << this->m_salary
        << "\t职工岗位:" << this->GetWork()
        << "\t岗位职责:完成老板布置的任务,并下发任务给员工" << endl;
}
string Manager::GetWork()
{
    return string("经理");
}

Employee::Employee(int id, string name, string sex, string tel, int type, int salary)
{
    this->m_id = id;
    this->m_name = name;
    this->m_type = type;
    this->m_sex = sex;
    this->m_tel = tel;
    this->m_salary = salary;
}
void Employee::ShowInfo()
{
    cout << "职工编号:" << this->m_id
        << "\t职工姓名:" << this->m_name
        << "\t职工性别:" << this->m_sex
        << "\t职工电话:" << this->m_tel
        << "\t职工薪资:" << this->m_salary
        << "\t职工岗位:" << this->GetWork()
        << "\t岗位职责:完成经理布置的任务" << endl;
}
string Employee::GetWork()
{
    return string("员工");
}

StaffManagement::StaffManagement()
{
    ifstream in;
    in.open(FILENAME, ios::in | ios::binary);
    if (!in.is_open())
    {
        this->CheckFile = true;
        this->P_arr = NULL;
        this->P_n = 0;
        in.close();
        return;
    }
    char ret;
    in >> ret;
    if (in.eof())
    {
        this->CheckFile = true;
        this->P_arr = NULL;
        this->P_n = 0;
        in.close();
        return;
    }
    this->P_n = this->GetFileSize();
    this->P_arr = new Staff*[this->P_n];
    this->InitFile();
}

void StaffManagement::Menu()
{
    cout << "******************************************************" << endl;
    cout << "*************    欢迎使用职工管理系统!     ***********" << endl;
    cout << "*************       0.退出管理系统         ***********" << endl;
    cout << "*************       1.添加职工信息         ***********" << endl;
    cout << "*************       2.显示职工信息         ***********" << endl;
    cout << "*************       3.删除职工信息         ***********" << endl;
    cout << "*************       4.修改职工信息         ***********" << endl;
    cout << "*************       5.查找职工信息         ***********" << endl;
    cout << "*************       6.保存职工信息         ***********" << endl;
    cout << "*************       7.职工排序             ***********" << endl;
    cout << "*************       8.清空所有职工信息     ***********" << endl;
    cout << "******************************************************" << endl;
}

void StaffManagement::Exit()
{
    cout << "欢迎下次使用" << endl;
    StaffManagement::Save();
    system("pause");
    exit(0);
}

bool StaffManagement::CheckId(int n)
{
    for (int i = 0; i < this->P_n; i++)
    {
        if (this->P_arr[i]->m_id == n)
            return false;
    }
    return true;
}

void StaffManagement::AddEmployee()
{
    int add_n;
    while (true)
    {
        cout << "请输入添加员工的数量" << endl;
        cin >> add_n;
        if (add_n > 0)
        {
            int newsize = this->P_n + add_n;
            Staff**newspace = new Staff*[newsize];
            if (!this->P_arr)
            {
                for (int i = 0; i < this->P_n; i++)
                    newspace[i] = this->P_arr[i];
            }
            for (int i = 0; i < add_n; i++)
            {
                int id;
                string name;
                string sex;
                string tel;
                int salary;
                int choice;
                while (true)
                {
                    cout << "请输入第" << i + 1 << "个新职工的编号:" << endl;
                    cin >> id;
                    if (!this->CheckId(id))
                    {
                        cout << "职工编号重复,请重新输入" << endl;
                    }
                    else
                        break;
                }
                cout << "请输入第" << i + 1 << "个新职工的姓名:" << endl;
                cin >> name;
                cout << "请输入第" << i + 1 << "个新职工的性别:" << endl;
                cin >> sex;
                cout << "请输入第" << i + 1 << "个新职工的电话:" << endl;
                cin >> tel;
                cout << "请输入第" << i + 1 << "个新职工的薪资:" << endl;
                cin >> salary;
                cout << "请选择以下职工岗位:" << endl
                    << "1.员工" << endl
                    << "2.经理" << endl
                    << "3.老板" << endl;
                cin >> choice;
                Staff*staff = NULL;
                switch (choice)
                {
                case 1:
                    staff = new Employee(id, name,sex,tel,choice,salary);
                    break;
                case 2:
                    staff = new Manager(id, name, sex, tel,choice, salary);
                    break;
                case 3:
                    staff = new Boss(id, name, sex, tel, choice, salary);
                    break;
                default:
                    cout << "输入无效,请从新输入" << endl;
                    break;
                }
                newspace[this->P_n + i] = staff;
            }
            delete[] this->P_arr;
            this->P_arr = newspace;
            this->P_n = newsize;
            this->CheckFile = false;
            cout << "成功添加" << add_n << "名新职工" << endl;
            this->Save();
            system("pause");
            system("cls");
            this->Menu();
            break;
        }
        else
            cout << "输入无效,请求重新输入" << endl;
    }
}

void StaffManagement::Save()
{
    ofstream out;
    out.open(FILENAME, ios::out | ios::binary);
    for (int i = 0; i < this->P_n; i++)
    {
        out << this->P_arr[i]->m_id << " "
            << this->P_arr[i]->m_name << " "
            << this->P_arr[i]->m_sex << " "
            << this->P_arr[i]->m_tel << " "
            << this->P_arr[i]->m_salary << " "
            << this->P_arr[i]->m_type << endl;
    }
    out.close();
    cout << "保存成功" << endl;
}

int StaffManagement::GetFileSize()
{
    ifstream in;
    in.open(FILENAME, ios::in | ios::binary);
    int count = 0, id, type ,salary;
    string name, sex, tel;
    while (in >> id && in >> name && in >> sex && in >> tel && in && in >> salary&&in>>type)
    {
        count++;
    }
    return count;
}

void StaffManagement::InitFile()
{
    ifstream in;
    in.open(FILENAME, ios::in | ios::binary);
    int index = 0, id, type,salary;
    string name, sex, tel;
    while (in >> id && in >> name && in >> sex && in >> tel && in >> salary && in >> type )
    {
        Staff*staff = NULL;
        switch (type)
        {
        case 1:
            staff = new Employee(id, name, sex, tel, type, salary);
            break;
        case 2:
            staff = new Manager(id, name, sex, tel, type, salary);
            break;
        case 3:
            staff = new Boss(id, name, sex, tel, type, salary);
            break;
        default:
            cout << "初始化错误" << endl;
            break;  
        }
        this->P_arr[index] = staff;
        index++;
    }
}

void StaffManagement::ShowEmployee()
{
    for (int i = 0; i < this->P_n; i++)
        this->P_arr[i]->ShowInfo();
    system("pause");
    system("cls");
    this->Menu();
}

int StaffManagement::GetRank(int n)
{
    int index = -1;
    for (int i = 0; i < this->P_n; i++)
    {
        if (this->P_arr[i]->m_id == n)
        {
            index = i;
            break;
        }
    }
    return index;
}

void StaffManagement::NameSearch(string name)
{
    for (int i = 0; i < this->P_n; i++)
    {
        if (this->P_arr[i]->m_name == name)
            this->P_arr[i]->ShowInfo();
    }
}

void StaffManagement::DeleteEmployee()
{
    if (!this->P_n)
        cout << "删除失败,文件不存在或文件内容为空" << endl;
    else
    {
        cout << "请输入职工编号" << endl;
        int n;
        cin >> n;
        int index = this->GetRank(n);
        if (index == -1)
            cout << "职工信息不存在" << endl;
        else
        {
            for (int i = 0; i < this->P_n - 1; i++)
                this->P_arr[i] = this->P_arr[i + 1];
        }
        this->P_n--;
        this->Save();
        cout << "删除成功" << endl;
    }
}

void StaffManagement::ModifyEmployee()
{
    if (!this->P_n)
        cout << "修改失败,文件不存在或文件内容为空" << endl;
    else
    {
        cout << "请输入职工编号" << endl;
        int n;
        cin >> n;
        int index = this->GetRank(n);
        if (index == -1)
            cout << "职工信息不存在" << endl;
        else
        {
            delete this->P_arr[index];
            int newid, newtype, newsalary;
            string newname, newsex, newtel;
            while (true)
            {
                cout << "请输入新的职工编号" << endl;
                cin >> newid;
                if (!this->CheckId(newid))
                    cout << "职工编号重复,请重新输入" << endl;
                else
                    break;
            }
            cout << "请输入新的职工姓名:" << endl;
            cin >> newname;
            cout << "请输入新的职工职工性别:" << endl;
            cin >> newsex;
            cout << "请输入新的职工电话:" << endl;
            cin >> newtel;
            cout << "请输入新的职工薪资:" << endl;
            cin >> newsalary;
            cout << "请选择以下职工岗位:" << endl
                << "1.员工" << endl
                << "2.经理" << endl
                << "3.老板" << endl;
            cin >> newtype;
            Staff*staff = NULL;
            switch (newtype)
            {
            case 1:
                staff = new Employee(newid, newname,newsex,newtel, newtype,newsalary);
                break;
            case 2:
                staff = new Manager(newid, newname, newsex, newtel, newtype, newsalary);
                break;
            case 3:
                staff = new Boss(newid, newname, newsex, newtel, newtype, newsalary);
                break;
            default:
                cout << "输入无效,请从新输入" << endl;
                break;
            }
            this->P_arr[index] = staff;
            this->Save();
            cout << "修改成功" << endl;
        }
    }
    system("pause");
    system("cls");
    this->Menu();
}

void StaffManagement::SearchEmployee()
{
    if (!this->P_n)
        cout << "文件不存在或文件为空" << endl;
    else
    {
        while (true)
        {
            cout << "请选择查找方式:" << endl
                << "1.职工编号" << endl
                << "2.职工姓名" << endl;
            int choice;
            cin >> choice;
            if (choice == 1)
            {
                cout << "请输入职工编号:" << endl;
                int id;
                cin >> id;
                if (this->GetRank(id) == -1)
                    cout << "无此职工信息" << endl;
                else
                {
                    cout << "查找成功,职工信息如下:" << endl;
                    this->P_arr[this->GetRank(id)]->ShowInfo();
                }
                break;
            }
            else if (choice == 2)
            {
                cout << "请输入职工姓名" << endl;
                string name;
                cin >> name;
                cout << "查找结果如下:" << endl;
                this->NameSearch(name);
                break;
            }
            else
                cout << "选择无效,请重新选择" << endl;
        }
    }
    system("pause");
    system("cls");
    this->Menu();
}

void StaffManagement::SortEmployee()
{
    if (!this->P_n)
        cout << "文件不存在或文件为空" << endl;
    else
    {
        while (true)
        {
            cout << "请选择排序方式:" << endl
                << "1.按职工编号升序" << endl
                << "2.按职工编号降序" << endl
                << "3.按职工姓名升序" << endl
                << "4.按职工职工降序" << endl
                << "5.按职工电话升序" << endl
                << "6.按职工电话降序" << endl
                << "7.按职工薪资升序" << endl
                << "8.按职工薪资降序" << endl;
            int choice;
            cin >> choice;
            for (int i = 0; i < this->P_n; i++)
            {
                int minormax = i;
                for (int j = i + 1; j < this->P_n; j++)
                {
                    switch (choice)
                    {
                    case 1:
                        if (this->P_arr[minormax]->m_id > this->P_arr[j]->m_id)
                            minormax = j;
                        break;
                    case 2:
                        if (this->P_arr[minormax]->m_id < this->P_arr[j]->m_id)
                            minormax = j;
                        break;
                    case 3:
                        if (this->P_arr[minormax]->m_name.compare(P_arr[j]->m_name) > 0)
                            minormax = j;
                        break;
                    case 4:
                        if (this->P_arr[minormax]->m_name.compare(P_arr[j]->m_name) < 0)
                            minormax = j;
                        break;
                    case 5:
                        if (this->P_arr[minormax]->m_tel.compare(P_arr[j]->m_tel)>0)
                            minormax = j;
                    case 6:
                        if (this->P_arr[minormax]->m_tel.compare(P_arr[j]->m_tel)<0)
                            minormax = j;
                        break;
                    case 7:
                        if (this->P_arr[minormax]->m_salary > this->P_arr[j]->m_salary)
                            minormax = j;
                        break;
                    case 8:
                        if (this->P_arr[minormax]->m_salary < this->P_arr[j]->m_salary)
                            minormax = j;
                        break;
                    default:
                        cout << "选择无效,请重新输入" << endl;
                        choice = 0;
                        break;
                    }
                }
                if (i != minormax)
                {
                    Staff*temp = this->P_arr[i];
                    this->P_arr[i] = this->P_arr[minormax];
                    this->P_arr[minormax] = temp;
                }
            }
            if (choice)
            {
                cout << "排序完成,结果如下>:" << endl;
                this->Save();
                this->ShowEmployee();
                break;
            }
        }
    }
}

void StaffManagement::DestoryedInfo()
{
    cout << "确认清除所有信息?" << endl
        << "确认请输入1" << endl
        << "返回请输入其它任意数字" << endl;
    int choice;
    cin >> choice;
    if (choice == 1)
    {
        ofstream out(FILENAME, ios::trunc);//如果文件存在则删除文件
        out.close();
        if (this->P_arr)
        {
            for (int i = 0; i < this->P_n; i++)
            {
                delete this->P_arr[i];
                this->P_arr[i] = NULL;
            }
            delete[]this->P_arr;
            this->P_n = 0;
            this->P_arr = NULL;
            this->CheckFile = true;
            cout << "所有信息已被清除!" << endl;
        }
    }
    else
    {
        system("cls");
        this->Menu();
        return;
    }
    system("pause");
    system("cls");
    this->Menu();
}

StaffManagement::~StaffManagement(){}
void operate()
{
    StaffManagement sm;
    sm.Menu();
    int choice;
    do
    {
        cout << "请选择:" << endl;
        cin >> choice;
        switch (choice)
        {
        case EXIT:
            sm.Exit();
            break;
        case ADD:
            sm.AddEmployee();
            break;
        case DISPLAY:
            sm.ShowEmployee();
            break;
        case DELETE:
            sm.DeleteEmployee();
            break;
        case MODIFY:
            sm.ModifyEmployee();
            break;
        case SEARCH:
            sm.SearchEmployee();
            break;
        case SAVE:
            sm.Save();
            break;
        case SORT:
            sm.SortEmployee();
            break;
        case DESTORY:
            sm.DestoryedInfo();
            break;
        default:
            cout << "输入无效,请重新输入" << endl;
            break;
        }
    } while (choice);
}

int main()
{
    operate();
    return 0;
}

标准的练习作业,网上一大堆现成代码啊