要求:
(1) 查看功能:选择此功能时,列出下列三类选择。
A 办公类 B 个人类 C 商务类 ,当选中某类时,显示出此类所有数据中的姓名和电话号码)
(2) 增加功能:能录入新数据(一个结点包括:姓名、电话号码、分类(可选项有: A 办公类 B 个人类 C 商务类)、电子邮件)。例如
小米 13589664454 商务类 chuny@126.com
当录入了重复的姓名和电话号码时,则提示数据录入重复并取消录入;当通信录中超过15条信息时,存储空间已满,不能再录入新数据;录入的新数据能按递增的顺序自动进行条目编号。
(3)拔号功能:能显示出通信录中所有人的姓名,当选中某个姓名时,屏幕上模拟打字机的效果依次显示出此人的电话号码中的各个数字,并伴随相应的拔号声音。
(4)修改功能:选中某个人的姓名时,可对此人的相应数据进行修改
(5)删除功能:选中某个人的姓名时,可对此人的相应数据进行删除,并自动调整后续条目的编号。
其它要求:
(1) 只能使用C++语言,源程序要有适当的注释,使程序容易阅读
(2) 至少采用文本菜单界面(如果能采用图形菜单界面更好)
(3) 学生可自动增加新功能模块
首先:
我们来创建一个虚基类Person,实际上也就是一个类,不过里面的成员函数都不实现。
#pragma once//防止头文件重复包含
#include <iostream>
using namespace std;
#include <string>
class Person//定义虚基类Person
{
public:
//展示成员信息
virtual void Show()=0;
virtual int getid()=0;
virtual string getname() = 0;
virtual int getnumber() = 0;
virtual void Show_name() = 0;
protected:
//人员姓名
string name;
//人员电话号码
int number;
//人员电子邮箱
string email;
};
接下来我们需要创建Person类的派生类,根据题目的要求有办公类,个人类和商务类,那我们就分别创出这三个类,并且以保护继承的方式继承Person类
#pragma once
#include "person.h"
class Officeperson:public Person
{
public:
Officeperson(string na,int nu,string em);
virtual void Show();
virtual int getid();
virtual string getname();
virtual int getnumber();
virtual void Show_name();
protected:
int id;
};
上面的是我写的办公类Officeperson,可以看出这里的成员函数和基类的也就是Person类的没有太多不同,不过这里需要具体实现它的功能,所以声明为虚函数,而基类中则是纯虚函数。
不过多了Officeperson类的构造函数和自己的成员变量id。
构造函数是为了实现后续添加功能的时候可以进行创建变量,id则是为了选择部门,后续会讲到。
#include "office.h"
Officeperson::Officeperson(string na, int nu, string em)
{
this->name = na;
this->number=nu;
this->email = em;
this->id = 1;
}
void Officeperson::Show()
{
cout << "办公类的 " << this->name
<< " 的电话号码是" << this->number << endl;
}
int Officeperson::getid()
{
return this->id;
}
string Officeperson::getname()
{
return this->name;
}
int Officeperson::getnumber()
{
return this->number;
}
void Officeperson::Show_name()
{
cout << this->name;
}
上面这是对Officeperson类的具体实现,相信大家应该没有问题
#pragma once
#include "person.h"
class Friend:public Person
{
public:
Friend(string na, int nu, string em);
virtual void Show();
virtual int getid();
virtual string getname();
virtual int getnumber();
virtual void Show_name();
protected:
int id;
};
上面是对个人类的声明
#include "friend.h"
Friend::Friend(string na, int nu, string em)
{
this->name = na;
this->number=nu;
this->email = em;
this->id = 2;
}
void Friend::Show()
{
cout << "个人类的 " << this->name
<< " 的电话号码是" << this->number << endl;
}
int Friend::getid()
{
return this->id;
}
string Friend::getname()
{
return this->name;
}
int Friend::getnumber()
{
return this->number;
}
void Friend::Show_name()
{
cout << this->name;
}
上面对个人类的具体实现
#pragma once
#include "person.h"
class Business:public Person
{
public:
Business(string na, int nu, string em);
virtual void Show();
virtual int getid();
virtual string getname();
virtual int getnumber();
virtual void Show_name();
protected:
int id;
};
上面是对商务类的声明
#include "business.h"
Business::Business(string na, int nu, string em)
{
this->name = na;
this->number=nu;
this->email = em;
this->id = 3;
}
void Business::Show()
{
cout << "商务类的 " << this->name
<< " 的电话号码是" << this->number << endl;
}
int Business::getid()
{
return this->id;
}
string Business::getname()
{
return this->name;
}
int Business::getnumber()
{
return this->number;
}
void Business::Show_name()
{
cout << this->name;
}
上面是对商务类的具体实现
接下来就是实现通讯录的功能了
我们需要创建一个管理通讯录的类,就把它叫做管理通讯录类吧,Workerperson类,先来看看它的声明
#pragma once
#include "business.h"
#include "friend.h"
#include "office.h"
class Workerperson
{
public:
Workerperson();//构造函数
void Show_menu();//展示菜单
void ExitSystem();//退出系统
void Add_Emp();//增加成员
void show_Emp();//显示成员
void Del_Emp();//删除成员
int Find(string);//查找成员
void Mod_Emp(); //修改职工
void dailing();//拨号
private:
int size;
Person** space;
};
接下来我们慢慢讲一下各个功能的实现,我这里的变量size是为了记录通讯录中的人数,space则是为了记录通讯录的开头地址,你可以把它当做打开通讯录的入口。
首先在主函数中做一个循环,让它可以出现菜单供我们选择:
#include <iostream>
using namespace std;
#include <string>
#include "workerperson.h"
int main()
{
//实例化对象
Workerperson wm;
int choice = 0; //用来存储用户的选项
while (true)
{
//调用展示菜单成员函数
wm.Show_menu();
cout << "请输入您的选择: " << endl;
cin >> choice; // 接受用户的选项
switch (choice)
{
case 0: //退出系统
wm.ExitSystem();
break;
case 1: //增加职工
wm.Add_Emp();
break;
case 2: //显示职工
wm.show_Emp();
break;
case 3: //删除职工
wm.Del_Emp();
break;
case 4: //修改职工
wm.Mod_Emp();
break;
case 5: //拨号
wm.dailing();
break;
default:
system("cls"); //清屏
break;
}
}
system("pause");
return 0;
}
经过了这个我们运行起来便可以根据用户的选择调用Workerperson类里面的功能了
第一个功能:菜单
void Workerperson::Show_menu()
{
cout << "********************************************" << endl;
cout << "************* 欢迎使用通信录!*************" << endl;
cout << "************* 0.退出管理程序 *************" << endl;
cout << "************* 1.增加成员信息 *************" << endl;
cout << "************* 2.显示成员信息 *************" << endl;
cout << "************* 3.删除成员 *************" << endl;
cout << "************* 4.修改成员信息 *************" << endl;
cout << "************* 5.拨号 *************" << endl;
cout << "********************************************" << endl;
cout << endl;
}
第二个:构造函数
Workerperson::Workerperson()
{
this->size = 0;
this->space = NULL;
}
第三个:添加成员
void Workerperson::Add_Emp()
{
if (this->size > 0)//判断原来通讯录中是否有人
{
int n = 0, newsize = 0;
cout << "请输入添加成员的个数" << endl;
cin >> n;
newsize = n + this->size;//原来的人数加上新增的人数
Person** newspace = new Person * [newsize];//开辟一个新的空间,空间中储存的是Person*,大小是newsize
//这个空间的首地址指向newspace
if (n > 0)//判断输入的新增人数是否正确
{
for (int j = 0; j < this->size; j++)//将原来储存的人员复制到新空间里
{
newspace[j] = this->space[j];
}
int choice = 0;
Person* ps1 = NULL;
int p = 1;
int i = this->size;
while (i < newsize)//继续添加你想添加的人数个数
{
cout << "请选择你要添加的第" << p << "个人的类别:" << endl;
cout << "1.办公类 2.个人类 3.商务类" << endl;
cout << "请输入你的选择->";
cin >> choice;
string name;
int number;
string email;
bool no=true;
cout << "请输入姓名->";
cin >> name;
for (int k = 0; k < this->size; k++)
{
int kk = 1;
if ((kk = name.compare(space[k]->getname())) == 0)//与空间里面的进行比较,防止姓名重复
{
cout << "姓名重复!" << endl;
no = false;
break;
}
}
if (no)
{
cout << "请输入电话号码->";
cin >> number;
for (int k = 0; k < this->size; k++)
{
if (number == space[k]->getnumber())//防止电话号码重复
{
cout << "电话号码重复!" << endl;
no = false;
break;
}
}
if (no)
{
cout << "请输入电子邮箱";
cin >> email;
switch (choice)//输入的数据没问题后,利用new创建不同类的空间,但可以用Person*来储存它的地址
{
case 1:ps1 = new Officeperson(name, number, email);
break;
case 2:ps1 = new Friend(name, number, email);
break;
case 3:ps1 = new Business(name, number, email);
break;
default:cout << "选择类型错误!" << endl;//选择错了重新选择实现
break;
}
newspace[i] = ps1;//放进新空间
i++;
p++;
}
}
}
delete[]this->space;//等完全添加完释放原来空间
this->space = newspace;//更新新空间地址
this->size = newsize;//更新通讯录人数
cout << "成功添加了" << n << "名成员" << endl;
//cout << "现在有" << this->size << "个人" << endl;//测试代码
}
}
else
{
//这个else不过是通讯录中原来没有成员的情况,大致相同
int n = 0, newsize = 0;
cout << "请输入添加成员的个数" << endl;
cin >> n;
newsize = n + this->size;
Person** newspace = new Person * [newsize];
if (n > 0)
{
int choice = 0;
Person* ps1 = NULL;
for (int i = 0; i < newsize; i++)
{
cout << "请选择你要添加的第" << i + 1 << "个人的类别:" << endl;
cout << "1.办公类 2.个人类 3.商务类" << endl;
cout << "请输入你的选择->";
cin >> choice;
string name;
int number;
string email;
cout << "请输入姓名->";
cin >> name;
cout << "请输入电话号码->";
cin >> number;
cout << "请输入电子邮箱";
cin >> email;
switch (choice)
{
case 1:ps1 = new Officeperson(name, number, email);
break;
case 2:ps1 = new Friend(name, number, email);
break;
case 3:ps1 = new Business(name, number, email);
break;
default:cout << "选择类型错误!" << endl;//选择错了重新选择实现
break;
}
newspace[i] = ps1;
}
this->space = newspace;
this->size = newsize;
cout << "成功添加了" << n << "名成员" << endl;
//cout << "现在有" << this->size << "个人" << endl;//测试代码
}
else
{
cout << "输入错误!" << endl;
}
}
system("pause");
system("cls");
}
第四个:显示成员
void Workerperson::show_Emp()
{
if (this->size == 0)
{
cout << "没有信息!" << endl;
}
else
{
cout << "请输入你想查看的类别" << endl;
cout << "1.办公类 2.个人类 3.商务类" << endl;
cout << "请输入你的选择->";
int choice = 0;
cin >> choice;
switch (choice)
{
case 1:
for (int i = 0; i < this->size; i++)
{
if (space[i]->getid() == 1)
space[i]->Show();
}
break;
case 2:
for (int i = 0; i < this->size; i++)
{
if (space[i]->getid() == 2)
space[i]->Show();
}
break;
case 3:
for (int i = 0; i < this->size; i++)
{
if (space[i]->getid() == 3)
space[i]->Show();
}
break;
default:
cout << "输入错误!" << endl;
break;
}
}
system("pause");
system("cls");
}
这个时候在虚基类中的id就派上用途了,可以将数据中不同类给区分开来,根据你想输出什么类
就给你输出什么类的人,方法是遍历一下就行然后调用不同类各自的成员函数Show来显示
因为space中储存的是不同类的指针,根据指针就可以找到对应的成员,及成员函数
第五个:查找人员
int Workerperson::Find(string na)
{
if (this->size == 0)
{
cout << "没有信息!" << endl;
}
else
{
for (int i = 0; i < this->size; i++)
{
if (na.compare(this->space[i]->getname()) == 0)
return i;
}
return -1;
}
}
这个只是做辅助作用的,它将会在其他功能中起到作用
第六个:删除人员
void Workerperson::Del_Emp()
{
if (this->size == 0)
{
cout << "没有信息!" << endl;
}
else
{
cout << "请输入你想要删除的人的名字" << endl;
string name1;
cin >> name1;
int position = this->Find(name1);
if (position < 0)
{
cout << "查无此人!" << endl;
}
else
{
delete space[position];
for (int i = position; i < this->size - 1; i++)
{
space[i] = space[i + 1];
}
this->size--;
cout << "删除成功!" << endl;//删除完最后一个空间还存放着指针
}
}
system("pause");
system("cls");
}
这里就需要用到Find函数,再进行删除
第七个:修改功能
void Workerperson::Mod_Emp()
{
if (this->size == 0)
{
cout << "没有信息!" << endl;
}
else
{
cout << "请输入你想要修改的人的名字" << endl;
string name2;
cin >> name2;
int position = this->Find(name2);
if (position < 0)
{
cout << "查无此人!" << endl;
}
else
{
string name;
int number;
string email;
cout << "请输入修改后的名字->";
cin >> name;
cout << "请输入修改后的电话号码->";
cin >> number;
cout << "请输入修改后的电子邮箱->";
cin >> email;
int xiabiao = space[position]->getid();
Person* ps = NULL;
switch (xiabiao)
{
case 1:ps = new Officeperson(name, number, email);
break;
case 2:ps = new Friend(name, number, email);
break;
case 3:ps = new Business(name, number, email);
break;
}
delete space[position];
space[position] = ps;
cout << "修改成功!" << endl;
}
}
system("pause");
system("cls");
}
第八个:拨号功能
void Workerperson::dailing()
{
if (this->size == 0)
{
cout << "没有信息!" << endl;
}
else
{
for (int i = 0; i < this->size; i++)
{
cout << "第" << i + 1 << "个人:";
space[i]->Show_name();
if ((i + 1) % 5 == 0)
cout << endl;
}
cout << endl;
cout << "请输入你要选择的姓名编号->";
int choice;
cin >> choice;
space[choice - 1]->Show_name();
cout << "的电话号码是->";
int number = space[choice - 1]->getnumber();
string number2 = to_string(number);
for (int i = 0; i < number2.size(); i++)
{
cout << number2[i];
Sleep(1000);
Beep(1209, 200);
Beep(697, 200);
}
}
system("pause");
system("cls");
}
第九个:退出功能
void Workerperson::ExitSystem()
{
cout << "请再次确认是否退出!" << endl;
cout << " 1.退出 0.返回" << endl;
cout << "请输入你的选择->";
int ps;
cin >> ps;
if (ps == 1)
exit(0);
system("cls");
}
后面的代码基本上问题不大,大家多多理解。因为这是分开写的,我在附一张图片
要是有不足之处,尽情指正。