溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

怎么用C++代碼實(shí)現(xiàn)學(xué)校人員管理系統(tǒng)

發(fā)布時(shí)間:2022-03-17 08:55:18 來(lái)源:億速云 閱讀:170 作者:iii 欄目:開(kāi)發(fā)技術(shù)

這篇文章主要介紹了怎么用C++代碼實(shí)現(xiàn)學(xué)校人員管理系統(tǒng)的相關(guān)知識(shí),內(nèi)容詳細(xì)易懂,操作簡(jiǎn)單快捷,具有一定借鑒價(jià)值,相信大家閱讀完這篇怎么用C++代碼實(shí)現(xiàn)學(xué)校人員管理系統(tǒng)文章都會(huì)有所收獲,下面我們一起來(lái)看看吧。

題目要求

學(xué)校人員管理系統(tǒng)

1、建立一個(gè)學(xué)校類,在其中定義按照姓名增加人員的增加函數(shù),刪除人員的刪除的函數(shù)和查詢函數(shù)(以姓名為準(zhǔn))。
2、建立一個(gè)人員基類,要具有姓名和性別屬性,并有輸出函數(shù)(可定義為虛函數(shù))。
3、建立一個(gè)員工類和一個(gè)學(xué)生類,均由人員類繼承而來(lái)。要求:可以輸出員工類(學(xué)生類)對(duì)象的屬性(如:姓名、性別和工作證號(hào)碼或?qū)W生學(xué)號(hào)),分別寫(xiě)出它們的輸出函數(shù)的具體實(shí)現(xiàn)。
4、重載“>>”,實(shí)現(xiàn)用cin為員工類、學(xué)生類和在職學(xué)生類對(duì)象賦值。(選做)
5、編寫(xiě)main()主函數(shù),測(cè)試上述功能,并以菜單方式實(shí)現(xiàn)對(duì)各種人員的增加、刪除和查詢(以姓名為線索)。
6、程序應(yīng)采用多文件結(jié)構(gòu)。
7、vs2019編譯

實(shí)驗(yàn)源代碼

head.h頭文件函數(shù)聲明

/*頭文件Head.h,聲名所有需要的函數(shù)或者系統(tǒng)頭文件*/

//系統(tǒng)頭文件
#include<iostream>
#include<string>
using namespace std;

//全局變量
extern int p ;//保存人類個(gè)數(shù)
extern int Re;//保存當(dāng)前學(xué)生所在位數(shù)
extern int Rs;//保存當(dāng)前員工所在位數(shù)
extern int s ;//保存學(xué)生個(gè)數(shù)
extern int e ;//保存員工個(gè)數(shù)

//全局類數(shù)組的聲明
extern int maxEmployee;
extern int maxStudent;
extern Employee* employee;
extern Student* student;

//人類聲明
class Person {
private:
    string name;//姓名
    string sex;//性別
    int age;//年齡
    string status;//身份
    void init();//初始化函數(shù)
public:
    void setPerson();//調(diào)用初始化函數(shù)
    void setStatus(string);//設(shè)置身份
    string getName();//獲取人的名字
    string getStatus();//獲取人的身份
    static void setP(int);//當(dāng)前添加第幾個(gè)人
    static void setRs(int);//當(dāng)前添加第幾個(gè)學(xué)生
    static void setRe(int);//當(dāng)前添加第幾個(gè)員工
    static int getRe();//獲取Re
    static int getRs();//獲取Rs
    static int getP();//獲取p
    virtual void show();//虛函數(shù)
};

//學(xué)校類
class school {
private:
    //增加人員
    static bool addPerson();
    //刪除人員
    static bool deletePerson();
    //查詢?nèi)藛T
    static Person queryPerson();
public:
    static bool getAdd();
    static bool getDel();
    static Person getQue();
};

//學(xué)生類
class Student :public Person {
private:
    string StudentNumber;
    string StudentDormitory;
public:
    //構(gòu)造函數(shù)
    Student();
    // >>重載
    friend istream& operator >> (istream& i, Student& p);
    static int getTotal();
    static void setTotal(int);
    string getStuId();
    void show();
};

//員工類
class Employee : public Person {
private:
    string EmployeeNumber;
    string EmployeeDormitory;
public:
    Employee();
    friend istream& operator >> (istream& i, Employee& p);
    static int getTotal();
    static void setTotal(int);
    string getEpyId();
    void show();
};


//main 函數(shù)里的函數(shù)聲明
void view();
bool doSwitch(char n);
extern char n;
char Cin();

Remain.cpp文件實(shí)現(xiàn)頭文件

#include"Head.h"
//引用全局變量
int p = 0;
int s = 0;
int e = 0;
int Re = 0;
int Rs = 0;

int maxEmployee = 1000;
int maxStudent = 1000;
Employee* employee = new Employee[maxEmployee];
Student* student = new Student[maxStudent];
char n;

/*人類*/

//人初始化函數(shù)
void Person::init() {
    cout << "姓名:" << endl;
    cin >> name;

cout << "性別:" << endl;
    cin >> sex;

    cout << "年齡:" << endl;
    cin >> age;
}

//
void Person::setPerson() {
    init();
}

//
string Person::getName() {
    return name;
}

void Person::setP(int i) {
    p = p + i;
}
int Person::getP() {
    return p;
}

void Person::setStatus(string s) {
    status = s;
}

string Person::getStatus() {
    return status;
}

void Person::setRs(int j) {
    Rs = j;
}
void Person::setRe(int j) {
    Re = j;
}
int Person::getRs() {
    return Rs;
}
int Person::getRe() {
    return Re;
}

//人類show函數(shù)
void Person::show() {
    cout << "姓名:" << name << "\t"
        << "性別:" << sex << "\t"
        << "年齡:" << age << "\t"
        << "工作:" << status << "\t";
}


//員工

//空參構(gòu)造
Employee::Employee(){}

void Employee::show() {
    cout << "工號(hào):" << EmployeeNumber << "\t"
        << "宿舍:" << EmployeeDormitory << endl;
}

int Employee::getTotal() {
    return e;
}
void Employee::setTotal(int i) {
    e = e + i;
}
string Employee::getEpyId() {
    return EmployeeNumber;
}
//員工>>重載
istream& operator >> (istream& i, Employee& p) {
    p.setPerson();
    cout << "工號(hào):" << endl;
    i >> p.EmployeeNumber;
    cout << "宿舍:" << endl;
    i >> p.EmployeeDormitory;
    return i;
}

//學(xué)生
Student::Student(){}

void Student::show() {

    cout << "學(xué)號(hào):" << this->StudentNumber << "\t"
        << "宿舍:" << this->StudentDormitory << endl;
}

int Student::getTotal() {
    return s;
}
void Student::setTotal(int i) {
    s = s + i;
}
string Student::getStuId() {
    return StudentNumber;
}
//學(xué)生>>重載
istream& operator >> (istream& i, Student& p) {
    p.setPerson();
    cout << "學(xué)號(hào):" << endl;
    i >> p.StudentNumber;
    cout << "宿舍:" << endl;
    i >> p.StudentDormitory;
    return i;
}

/*添加
*/
bool school::addPerson() {
    int n;
    cin >> n;
    if (n != 1 && n != 2) return false;
    if (n == 2) {
        Student t;
        cin >> t;
        t.setStatus("學(xué)生");
        student[Student::getTotal()] = t;
        Student::setTotal(1);
        Person::setP(1);
        return true;
    }
    if (n == 1) {
        Employee e;
        cin >> e;
        e.setStatus("員工");
        employee[Employee::getTotal()] = e;        
        Employee::setTotal(1);
        Person::setP(1);
        return true;
    }
    return false;
}
bool school::getAdd() {
    return addPerson();
}

/*刪除員工和學(xué)生,刪除對(duì)象*/

bool delS() {
    cout << "請(qǐng)輸入你要?jiǎng)h除的學(xué)生學(xué)號(hào)!" << endl;
    string s;
    cin >> s;
    int Sl = Student::getTotal();
    for (int i = 0; i < Sl; i++) {
        if (s == student[i].getStuId()) {
            //所有數(shù)組元素前移
            for (int j = i; j < Sl; j++) {
                student[j] = student[j + 1];
            }
            Student::setTotal(-1);
            return true;
        }
    }
    return false;
}

bool delE() {
    cout << "請(qǐng)輸入你要?jiǎng)h除的員工工號(hào)!" << endl;
    string s;
    cin >> s;
    int El = Employee::getTotal();
    for (int i = 0; i < El; i++) {
        if (s == employee[i].getEpyId()) {
            //所有數(shù)組元素前移
            for (int j = i; j < El; j++) {
                employee[j] = employee[j + 1];
            }
            Employee::setTotal(-1);
            return true;
        }
    }
    return false;
}


bool school::deletePerson() {
    int n;
    cin >> n;
    if (n != 1 && n != 2) return false;
    if (n == 1) {
        return delS();
    }
    else return delE();
    return false;
}


bool school::getDel() {
    return deletePerson();
}

//查詢

Person school::queryPerson() {
    string s;
    cin >> s;
    Person p;
    for (int j = 0; j < Student::getTotal(); j++) {
        if (s == student[j].getName()) {
            student[j].setRs(j);
            p = (Person)student[j];
            break;
        }
    }
    for (int j = 0; j < Employee::getTotal(); j++) {
        if (s == employee[j].getName()) {
            employee[j].setRe(j);
            p=(Person)employee[j];
            break;
        }
    }
    return p;
}

Person school::getQue(){
    return queryPerson();
}

//菜單
bool doSwitch(char n) {
    if (n == '#')return false;
    switch (n)
    {
    case '1':
        cout << "----請(qǐng)說(shuō)明你要添加的人員種類1.員工,2.學(xué)生------" << endl;
        if (school::getAdd()) cout << "添加成功!" << endl;
        else cout << "添加失敗!" << endl;
        break;
    case '2':
        cout << "----請(qǐng)說(shuō)明你要?jiǎng)h除的人員種類1.學(xué)生,2.員工------" << endl;
        if (school::getDel())cout << "刪除成功!"<<endl;
        else cout << "刪除失敗" << endl;
        break;
    case '3':
        cout << "請(qǐng)輸入你要查詢?nèi)说拿郑?quot; << endl;
        Person p = school::getQue();
        string s = p.getStatus();
        if (s == "學(xué)生") {
            int j = p.getRs();
            Student s = student[j];
            Person p = (Person)s;
            p.show();
            s.show();
        }
        else if (s == "員工") {
            int j = p.getRe();
            Employee s = employee[j];
            Person p = (Person)s;
            p.show();
            s.show();
        }
        else cout << "找不到該人!" << endl;
        break;
    }
    return true;    
}

char Cin() {
    cin >> n;
    return n;
}
void view() {
    cout << "------------歡迎來(lái)到學(xué)校人員管理系統(tǒng)------------" << "\n"
        << "----------------請(qǐng)選擇以下選項(xiàng)-------------------" << "\n"
        << "----------------1.添加人員-----------------------" << "\n"
        << "----------------2.刪除人員-----------------------" << "\n"
        << "----------------3.查詢?nèi)藛T-----------------------" << "\n"
        << "-------------------#退出!--------------------" << endl;
}

Main.cpp撰寫(xiě)main函數(shù)

#include"Head.h"
int main() {
    while (true) {
        view(); 
        if (!doSwitch(Cin())) {
            delete[] student;
            delete[] employee;
            break;
        }        
    }
    return 0;
}

關(guān)于“怎么用C++代碼實(shí)現(xiàn)學(xué)校人員管理系統(tǒng)”這篇文章的內(nèi)容就介紹到這里,感謝各位的閱讀!相信大家對(duì)“怎么用C++代碼實(shí)現(xiàn)學(xué)校人員管理系統(tǒng)”知識(shí)都有一定的了解,大家如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

c++
AI