服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C/C++ - C++实现教职工信息管理系统

C++实现教职工信息管理系统

2022-10-25 13:28陆鳴笙 C/C++

这篇文章主要为大家详细介绍了C++实现教职工信息管理系统,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了C++实现教职工信息管理系统的具体代码,供大家参考,具体内容如下

一.问题描述

一个小公司包含四类人员:经理,技术人员,销售人员和销售经理,各类人员的工资计算方法如下:经理:固定月薪(8000);技术人员:月薪按技术等级(1~8)(1600+等级*300);销售人员:按提成(4%*销售额);销售经理:底薪+提成(1500+0.2%*总销售额);设计一个管理程序,实现对各类人员的信息输入,修改和显示。

二 .基本要求

(1)使用面向对象编程思想编写开发过程中需要用到的类,比如:设计Person类:编号,姓名,岗位,工资,成员函数可设一个计算月薪的纯虚函数;另外再设计四个针对四类人员的类均继承 Person;添加相应的派生类数据成员和函数,经理和销售经理可以没有新的数据成员,计算月薪即可; 技术人员添加技术等级数据成员,销售人员添加数据成员:销售额。还需设计一个Manage 类来完成各种操作。人员数组 vector,数据类型为基类指针。

(2)需要使用菜单功能显示添加人员(输入),修改信息,浏览信息,按姓名查找,月薪排序。

(3)为了设计简洁,假定经理和销售经理都只能有一个;用文本编辑器编辑一个文本文件(总数 20 人以上)包含各类人员的信息;并且在程序中能修改保存。

基本流程图

C++实现教职工信息管理系统

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
#include<iostream>
#include<vector>
#include<string>
#include<cstdlib>
#include<windows.h>
#include<iomanip>
#include<fstream>
#include <algorithm>
#define filename "student.txt"
using namespace std; 
class Person
{
public:
    Person(string, string, int = 0);//构造函数 
    double virtual pay_salary() = 0; //借用虚函数进行工资初始化 
    void  virtual show();         //显示信息 
    bool operator<(const Person*&) const;    //重载<比较薪水大小用于排序 
    static int num; //定义静态变量,自动赋予员工编号 
    int Number;   //编号 
    double Salary;//工资 
    string Name;//姓名 
    string Department;//部门 
    int c;//技术级 
};
bool Person::operator<(const Person*& obj) const//函数重载<,用于比较薪水 
{
    return this->Salary > obj->Salary;
}
Person::Person(string name1, string work1, int c1) //构造函数的实现 
{
    c = c1;
    Number = num++;
    Name = name1;
    Department = work1;
}
int Person::num = 1;//编号从1开始 
void  Person::show() {
    cout<<"-----------------------------------"<<endl;
    cout <<right<<setw(3)<<Number<<setw(10)<<Name<<setw(11)<<Department<<setw(8)<<Salary; //setw()控制输出宽度 
    
}
class Manager :public Person //经理类,继承person类 
{
public:
    Manager(string, string, int);//构造函数 
    double pay_salary();//计算工资函数 
    void show();//显示经理的信息 
};
Manager::Manager(string name1, string post1, int c1) :Person(name1, post1, c1) //构造函数 的实现 
{
    pay_salary();
}
double Manager::pay_salary()//计算经理的工资 
 {
    Salary = 8000;
    return Salary;
}
void Manager::show() //显示经理的信息 
{
    cout<<"-----------------------------------"<<endl;
    cout <<right<<setw(3)<<Number<<setw(10)<<Name<<setw(9)<<Department<<setw(10)<<Salary; 
}
class SaleManager :public Person//销售经理类,继承person类 
 {
public:
    SaleManager(string, string, int);//构造函数 
    double pay_salary();//计算销售经理的工资 
    void show();//显示销售经理的信息 
};
SaleManager::SaleManager(string name1, string post1, int c1) :Person(name1, post1, c1)//构造函数 的实现 
 {
    pay_salary();
}
double SaleManager::pay_salary() //计算经理的工资,基本工资 1500元 
{
    Salary = 1500;
    return Salary;
}
void SaleManager::show() //显示销售经理的信息 
{
    cout<<"-----------------------------------"<<endl;
    cout <<right<<setw(3)<<Number<<setw(10)<<Name<<setw(11)<<Department<<setw(8)<<Salary; 
}
class Salesman :public Person//销售人员类,继承Person类 
 {
public:
    Salesman(string, string, int);//构造函数 
    int salevolume;//销售额
    double pay_salary();//计算销售人员的工资 
    void show();//显示销售人员的信息 
};
Salesman::Salesman(string name1, string post1, int sv) :Person(name1, post1, sv)//构造函数的实现 
{
    salevolume = sv;
    pay_salary();
}
double Salesman::pay_salary()//计算销售人员工资,4%×销售额 
 {
    Salary = 0.04 * salevolume;
    return Salary;
}
void Salesman::show() //显示销售人员的信息 
{
    cout<<"-----------------------------------"<<endl;
    cout <<right<<setw(3)<<Number<<setw(10)<<Name<<setw(11)<<Department<<setw(8)<<Salary; 
}
class Technician :public Person //技术人员类,继承Person类 
{
public:
    Technician(string, string, int);//技术等级为继承来的参数c 
    double pay_salary();//计算技术人员的工资 
    void show();//显示技术人员的所有信息 
};
Technician::Technician(string name1, string post1, int rank1) :Person(name1, post1, rank1) 
{
    pay_salary();
}
double Technician::pay_salary() //计算技术人员的工资,技术等级×300+1600 
{
    Salary = 1600 + 300 * c;
    return Salary;
}
void Technician::show()//显示所有技术人员的信息 
 {
     cout<<"-----------------------------------"<<endl;
    cout <<right<<setw(3)<<Number<<setw(10)<<Name<<setw(11)<<Department<<setw(8)<<Salary; 
}
class Manage//管理类 
{
public:
    void Menu() { salevolume = 0; }//菜单函数 
    int salevolume;//总销售额 
    vector<Person*> Ma;//vector数组,存放Person类的对象指针 
    void add(Person*);//添加人员信息 
    void alter(string);//删除人员信息 
    void addtofile();//写入文件 
    void show();//显示所有信息 
    void show1();//按月薪降序 
    Person* find(string&);//查找人员信息 
};
Person* Manage::find(string& name1) { //查找
    for (vector<Person*>::iterator iter = Ma.begin(); iter != Ma.end(); iter++) {
        if ((*iter)->Name == name1) {
            return *iter;
        }
    }
    return NULL;
}
void Manage::alter(string name1) { //删除
    for (vector<Person*>::iterator iter = Ma.begin(); iter != Ma.end(); iter++) {
        if ((*iter)->Name == name1) {
            Ma.erase(iter);
            return;
        }
    }
    cout << "查无此人" << endl;
}
void Manage::add(Person* people) //添加 
{
    if (people->Department == "销售人员") {
        salevolume += ((Salesman*)people)->salevolume;
    }
    Ma.push_back(people);
}
void Manage::addtofile()//写入文件 
 {
    ofstream outfile(filename);//打开文件写入 
    for (vector<Person*>::iterator iter = Ma.begin(); iter != Ma.end(); iter++) {
        outfile << (*iter)->Department << " " << (*iter)->Name << " ";
        if ((*iter)->c == 0) outfile << endl;
        else outfile << (*iter)->c << endl;
    }
    outfile.close();//关闭 
}
bool cmp(Person* x, Person* y) { //比较薪水
    return x->Salary > y->Salary;
}
 
void Manage::show() {
    for (vector<Person*>::iterator iter = Ma.begin(); iter != Ma.end(); iter++) {
        if ((*iter)->Department == "销售经理") {
            (*iter)->Salary = salevolume * 0.002 +1500;
            break;
        }
    }
    sort(Ma.begin(), Ma.end(), cmp);//薪水大小排序
    for (vector<Person*>::iterator iter = Ma.begin(); iter != Ma.end(); iter++) {
        (*iter)->show();
        cout << endl;
    }
}
void readfile(Manage& obj)//读取文件 
 {
    FILE* fp;
    fp = fopen(filename, "r");//打开文件,只读 
    if (fp == NULL) {
        cout << "未找到人员名单" << endl;
        return;
    }
    while (!feof(fp)) {
        char post[20];
        char Name[20];
        int c;   //销售额或技术等级
        fscanf(fp, "%s%s%d", post, Name,&c);
        if (!strcmp(post, "经理")) { //文件中为经理的人的信息先填入
            Person* peo = new Manager(Name, post, 0);
            obj.add(peo);
        }
        else if (!strcmp(post, "技术人员")) {
            Person* peo = new Technician(Name, post, c);
            obj.add(peo);
        }
        else if (!strcmp(post, "销售人员")) {
            Person* peo = new Salesman(Name, post, c);
            obj.add(peo);
        }
        else if (!strcmp(post, "销售经理")) {
            Person* peo = new SaleManager(Name, post, 0);
            obj.add(peo);
        }
    }
    fclose(fp);//关闭文件 
}
void Manage::show1()//对vector数组进行读取 
{
    for (vector<Person*>::iterator iter = Ma.begin(); iter != Ma.end(); iter++) {
        (*iter)->show();
        cout << endl;
    }
}
int main(){
    int x;
    Manage T;
    readfile(T);
    while(1){
        cout<< "    ———————————————————————————————" << endl
            << "    |       公司人事管理系统        |" << endl
            << "    ———————————————————————————————" << endl
            << "    |         1.添加员工           |" << endl
            << "    |         2.修改信息           |" << endl
            << "    |         3.按姓名查找         |" << endl
            << "    |         4.显示所有信息       |" << endl
            << "    |         5.按月薪降序排序     |" << endl
            << "    |         0.保存并退出程序     |" << endl
            << "    ———————————————————————————————" << endl;        
        cout<< "请选择->";
        cin >> x;
        switch (x) {
        case 1: {
            while (1) {
                int n;
                string Name;
                cout << "请输入姓名:" ;
                cin >> Name;
                cout << "请输入人员岗位(1.经理 2.技术人员 3. 销售人员 4.销售经理):"
                cin >> n;
                if (n == 1) {
                    Person* peo = new Manager(Name, "经理", 0);
                    T.add(peo);
                    cout << "添加成功" << endl << endl << endl;
                    break;
                }
                else if (n == 2) {
                    while (1) {
                        int rank = 0;
                        cout << "请输入技术等级(1~8):" ;
                        cin >> rank;
                        if (rank > 8 || rank < 1) {
                            cout << "输入错误,请在1~8之间输入:" ;
                        }
                        else {
                            Person* peo = new Technician(Name, "技术人员", rank);
                            T.add(peo);
                            break;
                        }
                    }
                    cout << "添加成功" << endl << endl << endl;
                    break;
                }
                else if (n == 3) {
                    int sales = 0;
                    cout << "请输入销售额:" << endl;
                    cin >> sales;
                    Person* peo = new Salesman(Name, "销售人员", sales);
                    T.add(peo);
                    cout << "添加成功" << endl << endl << endl;
                    break;
                }
                else if (n == 4) {
                    Person* peo = new SaleManager(Name, "销售经理", 0);
                    T.add(peo);
                    cout << "添加成功" << endl << endl << endl;
                    break;
                }
                else {
                    cout << "输入错误,请重新输入:" << endl;
                }
            }
         system("pause");}
                break;
        case 2: {
            string Name;
            int n = 0;
            cout << "请输入姓名:" ;
            cin >> Name;
            Person* peo = T.find(Name);
            if (peo == NULL) {
                cout << "        查无此人" << endl << endl << endl;
                break;
            }
            peo->show();
            if (peo->Department == "经理") {
                cout << "    经理无法修改" << endl;
            }
            else if (peo->Department == "技术人员") {
                int rank = 0;
                while (1) {
                    cout <<endl<< "请输入技术等级(1~8):" << endl;
                    cin >> rank;
                    if (rank > 8 || rank < 1) {
                        cout << "等级输入错误,请重新输入" << endl;
                    }
                    else break;
                }
                T.alter(Name);
                peo = new Technician(Name, "技术人员", rank);
                T.add(peo);
                cout << "修改成功!" << endl;
                
            }
            else if (peo->Department == "销售人员") {
                int sales = 0;
                cout <<endl<< "请输入销售额:" << endl;
                cin >> sales;
                T.alter(Name);
                peo = new Salesman(Name, "销售人员", sales);
                T.add(peo);
                cout << "    修改成功!" << endl;
                
            }
            else if (peo->Department == "销售经理") {
                cout << "   销售经理无法修改" << endl;
            }
            else {
                cout << "输入错误" << endl;
            }
        }system("pause");
                break;
        case 3: {
            string Name;
            int n = 0;
            cout << "请输入所查找人的姓名:";
            cin >> Name;
            Person* peo = T.find(Name);
            if (peo == NULL) {
                cout << "查无此人" << endl;
                system("cls");
                break;
            }
            cout<<"-----------------------------------"<<endl;
            cout <<left<<setw(8)<<"序 号"<<setw(9)<<"姓 名"<<setw(10)<<"岗 位"<<setw(10)<<"工 资"<< endl; 
            peo->show();
            cout<<endl<<"-----------------------------------"<<endl;
            cout << endl;
            system("pause");
            
        }
                break;
        case 4: { 
            cout<<"-----------------------------------"<<endl;
            cout <<left<<setw(8)<<"序 号"<<setw(9)<<"姓 名"<<setw(10)<<"岗 位"<<setw(10)<<"工 资"<< endl; 
            T.show1();
            cout<<"-----------------------------------"<<endl;
            system("pause");
            system("cls");
        }
                break;
        case 5: { 
            cout<<"-----------------------------------"<<endl;
            cout <<left<<setw(8)<<"序 号"<<setw(9)<<"姓 名"<<setw(10)<<"岗 位"<<setw(10)<<"工 资"<< endl; 
                T.show();
        }
            system("pause");
                break;
        case 0:
            T.addtofile();
            exit(0);
        default:
            cout << "输入错误请重新输入" << endl;
            break;
        }
    }
    return 0;
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/cjl1831050185/article/details/109231213

延伸 · 阅读

精彩推荐