赞
踩
1、实验简介
【实验内容的简要说明,具体说明实验完成的功能和性能要求】
设计的通讯录可以实现1.添加联系人 2.显示联系人 删除联系人 4.查找联系人
5.清空联系人 6.修改联系人 7 .退出联系人的七大功能。性能比较快捷;
2、程序框架
3、关键代码实现
3.1 函数名1。Menu
void Menu()
{
cout << "~~~~~~~~~~~~~~~~~~~~~~~~" << endl;
cout << "
cout << "
cout << "
cout << "
cout << "
cout << "
cout << "
cout << " " << endl;
cout << "~~~~~~~~~~~~~~~~~~~~~~~~" << endl;
}
用了一些字符使其更加美观
3.2 函数名2:addPerson
void addPerson(Addressbooks *abs) //添加联系人
{
//判断通讯录是否已满
if (abs->size >= MAX) {
cout << "通讯录已满,请删除一些联系人" << endl;
return;
}
else {
//添加具体联系人
//姓名
string Name;
cout << "请输入姓名" << endl;
cin >> Name;
abs->personArray[abs->size].m_name = Name;
//性别
cout << "请输入性别" << endl;
cout << "1--男" << endl;
cout << "2--女" << endl;
int a = 0;
do
{
int Sex;
cin >> Sex;
if (Sex == 1 || Sex == 2)
{
if (Sex == 1)
{
abs->personArray[abs->size].m_age = Sex;
break;
}
else
{
abs->personArray[abs->size].m_age = Sex;
break;
}
}
else {
cout << "输入错误,请重新输入" << endl;
a = 1;
}
} while (a = 1);
//年龄
cout << "请输入年龄" << endl;
int Age = 0;
cin >> Age;
abs->personArray[abs->size].m_age = Age;
//电话
cout << "请输入电话号码" << endl;
string Phone;
cin >> Phone;
abs->personArray[abs->size].m_phone = Phone;
//住址
cout << "请输入住址" << endl;
string Address ;
cin >> Address;
abs->personArray[abs->size].m_address = Address;
cout << "添加联系人成功!!!!" << endl;
abs->size++;
system("pause");
system("cls");//实现清屏的操作
}
}
3.3 函数名3:showPerson
void showPerson(Addressbooks *abs)
{
//判断通讯录人数是否为零
if (abs->size >= MAX)
{
cout << "通讯录人数已满,请进行删除操作" << endl;
}
else
{
for (int i = 0; i <= abs->size; i++)
{
cout << abs->personArray[i].m_name << "\t";
if (abs->personArray[i].m_sex = 1)
{
cout << "男" << "\t";
}
else {
cout << "女" << "\t";
}
cout << abs->personArray[i].m_sex << "\t";
cout << abs->personArray[i].m_age << "\t";
cout << abs->personArray[i].m_phone << "\t";
cout << abs->personArray[i].m_address << endl;
}
}
}
3.4 函数名4:textbooks
void textbooks(Addressbooks *abs,string name)//判断有没有联系人在里面
{
for (int i = 0; i <= abs->size; i++)
{
if (abs->personArray[i].m_name !=name)
{
cout << "没有找到该联系人" << endl;
}
}
}
3.5 函数名5:deletePerson
void deletePerson(Addressbooks *abs)
{
if (abs->size == 0)//判断通讯录人数是否为零
{
cout << "通讯录人数为0,请进行添加操作" << endl;
}
else
{
cout << "请再次输入要删除的人名" << endl;
string name;
cin >> name;
for (int i = 0; i <= abs->size; i++)
{
if (abs->personArray[i].m_name == name)
{
abs->personArray[i] = abs->personArray[i + 1];
}
abs->size--;
}
}
system("pause");
system("cls");//实现清屏的操作
}
3.6 函数名6:Findperson
void Findperson(Addressbooks *abs, string rname)
{
cout << "请输入要查找的人的姓名" << endl;
for (int i = 0; i <= abs->size; i++)
{
cin >> rname;
if (abs->personArray[i].m_name == rname)
{
cout << "已找到该联系人为第" << i << "位" << endl;
cout << "是否进行操作?" << endl;
cout << "是---1" << endl;
cout << "否---2" << endl;
int a = 0;
cin>> a;
if (a = 1)
{
cout << abs->personArray[i].m_name << "\t";
if (abs->personArray[i].m_sex = 1)
{
cout << "男" << "\t";
}
else {
cout << "女" << "\t";
}
cout << abs->personArray[i].m_sex << "\t";
cout << abs->personArray[i].m_age << "\t";
cout << abs->personArray[i].m_phone << "\t";
cout << abs->personArray[i].m_address << endl;
system("pause");
system("cls");//实现清屏的操作
}
}
else
{
cout<< "查无此人" << endl;
system("pause");
system("cls");//实现清屏的操作
}
}
}
3.7 函数名7: DeleteALLperson
void DeleteALLperson(Addressbooks *abs)
{
abs->size = 0;
cout << "通讯录已清空" << endl;
system("pause");
system("cls");
}
3.8 函数名8:fixPerson
oid fixPerson(Addressbooks *abs, string name)
{
cin >> name;
int x = 0;
for (x = 0; x <= abs->size; x++)
{
if (abs->personArray[x].m_name == name)
{
int b;
cout << "已找到该联系人为第" << x << "位" << endl;
cout << "是否进行操作?" << endl;
cout << "是---1" << endl;
cout << "否---2" << endl;
cout << "请选择修改的属性------" << endl;
cout << "姓名---1" << endl;
cout << "性别---2" << endl;
cout << "年龄---3" << endl;
cout << "电话号码---4" << endl;
cout << "地址---5" << endl;
cin >> b;
if (b <= 5)
{
switch (b)
{
case 1:
{
cout << "请输入修改后的姓名" << endl;
string Fixedname;
cin >> Fixedname;
abs->personArray[abs->size].m_name = Fixedname;
break;
}
case 2:
{
cout << "请输入修改后的性别" << endl;
char Fixedsex;
cin >> Fixedsex;
abs->personArray[abs->size].m_sex = Fixedsex;
break;
}
case 3:
{
cout << "请输入修改后的年龄" << endl;
int Fixedage;
cin >> Fixedage;
abs->personArray[abs->size].m_age = Fixedage;
break;
}
case 4:
{
cout << "请输入修改后的电话号码" << endl;
string Fixedphone;
cin >> Fixedphone;
abs->personArray[abs->size].m_phone = Fixedphone;
break;
}
case 5:
{
cout << "请输入修改后的地址" << endl;
string Fixedaddress;
cin >> Fixedaddress;
abs->personArray[abs->size].m_address = Fixedaddress;
break;
}
}
}
else
{
cout << "未找到该联系人!!!" << endl;
}
}
}
}
4、不足
【实验程序哪些函数功能还有缺陷或不足,或者程序架构有不足,或者性能还有待提高、或者代码不和规范等等,所有你自己对程序不满意的地方】
打开的速度比较慢,时间复杂度比较高。
5、心得体会
【所有你在实验中的感受和想和老师说的话都可以放在这里,篇幅不限】
通过学习通讯录,我又更加体会到了语法知识的重要性对运行效率的影响,我对数据结构和算法有了更多的认识和充分的了解。
.对项目进行性能测试,再做代码的时间复杂度、空间复杂度分析,会让我们通过不停的反思,去思考另一种算法的方式,在写代码的时候,要注意批注和注释的重要性,有的时候我就会因为读不懂自己的代码而花很多的时间又重新去看一遍。
附录 完整代码:
- #include <iostream>
- #include <string>
- #define MAX 100
- using namespace std;
- struct person//联系人的结构体
- {
- string m_name;
- char m_sex;
- int m_age;
- string m_phone;
- string m_address;
- int m_size;
- };
- void Menu()
- {
- cout << "~~~~~~~~~~~~~~~~~~~~~~~~" << endl;
- cout << " $$$$$1.添加联系人$$$$$ " << endl;
- cout << " $$$$$2.显示联系人$$$$$ " << endl;
- cout << " $$$$$3.删除联系人$$$$$ " << endl;
- cout << " $$$$$4.查找联系人$$$$$ " << endl;
- cout << " $$$$$5.清空联系人$$$$$ " << endl;
- cout << " $$$$$6.修改联系人$$$$$ " << endl;
- cout << " $$$$$0.退出联系人$$$$$ " << endl;
- cout << " " << endl;
- cout << "~~~~~~~~~~~~~~~~~~~~~~~~" << endl;
- }
- struct Addressbooks
- {
- //通讯录中保存的联系人数组
- struct person personArray[MAX];
- //通讯录中保存的记录的联系人个数
- int size=0;
- };
- void addPerson(Addressbooks *abs) //添加联系人
- {
- //判断通讯录是否已满
- if (abs->size >= MAX) {
- cout << "通讯录已满,请删除一些联系人" << endl;
- return;
- }
- else {
- //添加具体联系人
- //姓名
- string Name;
- cout << "请输入姓名" << endl;
- cin >> Name;
- abs->personArray[abs->size].m_name = Name;
- //性别
- cout << "请输入性别" << endl;
- cout << "1--男" << endl;
- cout << "2--女" << endl;
- int a = 0;
- do
- {
- int Sex;
- cin >> Sex;
- if (Sex == 1 || Sex == 2)
- {
- if (Sex == 1)
- {
-
- abs->personArray[abs->size].m_age = Sex;
- break;
- }
- else
- {
-
- abs->personArray[abs->size].m_age = Sex;
- break;
- }
-
- }
- else {
- cout << "输入错误,请重新输入" << endl;
- a = 1;
- }
- } while (a = 1);
- //年龄
- cout << "请输入年龄" << endl;
- int Age = 0;
- cin >> Age;
- abs->personArray[abs->size].m_age = Age;
- //电话
- cout << "请输入电话号码" << endl;
- string Phone;
- cin >> Phone;
- abs->personArray[abs->size].m_phone = Phone;
- //住址
- cout << "请输入住址" << endl;
- string Address ;
- cin >> Address;
- abs->personArray[abs->size].m_address = Address;
- cout << "添加联系人成功!!!!" << endl;
- abs->size++;
- system("pause");
- system("cls");//实现清屏的操作
- }
-
- }
- void showPerson(Addressbooks *abs)
- {
- //判断通讯录人数是否为零
- if (abs->size >= MAX)
- {
- cout << "通讯录人数已满,请进行删除操作" << endl;
- }
- else
- {
- for (int i = 0; i <= abs->size; i++)
- {
- cout << abs->personArray[i].m_name << "\t";
- if (abs->personArray[i].m_sex = 1)
- {
- cout << "男" << "\t";
- }
- else {
- cout << "女" << "\t";
- }
- cout << abs->personArray[i].m_sex << "\t";
- cout << abs->personArray[i].m_age << "\t";
- cout << abs->personArray[i].m_phone << "\t";
- cout << abs->personArray[i].m_address << endl;
-
- }
- }
- }
- void textbooks(Addressbooks *abs,string name)//判断有没有联系人在里面
- {
-
- for (int i = 0; i <= abs->size; i++)
- {
-
- if (abs->personArray[i].m_name !=name)
- {
- cout << "没有找到该联系人" << endl;
- }
- }
- }
- void deletePerson(Addressbooks *abs)
- {
-
-
- if (abs->size == 0)//判断通讯录人数是否为零
- {
- cout << "通讯录人数为0,请进行添加操作" << endl;
- }
- else
- {
- cout << "请再次输入要删除的人名" << endl;
- string name;
- cin >> name;
- for (int i = 0; i <= abs->size; i++)
- {
-
- if (abs->personArray[i].m_name == name)
- {
- abs->personArray[i] = abs->personArray[i + 1];
- }
- abs->size--;
-
- }
- }
- system("pause");
- system("cls");//实现清屏的操作
- }
- void Findperson(Addressbooks *abs, string rname)
- {
- cout << "请输入要查找的人的姓名" << endl;
- for (int i = 0; i <= abs->size; i++)
- {
-
- cin >> rname;
- if (abs->personArray[i].m_name == rname)
- {
- cout << "已找到该联系人为第" << i << "位" << endl;
- cout << "是否进行操作?" << endl;
- cout << "是---1" << endl;
- cout << "否---2" << endl;
- int a = 0;
- cin>> a;
- if (a = 1)
- {
- cout << abs->personArray[i].m_name << "\t";
- if (abs->personArray[i].m_sex = 1)
- {
- cout << "男" << "\t";
- }
- else {
- cout << "女" << "\t";
- }
- cout << abs->personArray[i].m_sex << "\t";
- cout << abs->personArray[i].m_age << "\t";
- cout << abs->personArray[i].m_phone << "\t";
- cout << abs->personArray[i].m_address << endl;
- system("pause");
- system("cls");//实现清屏的操作
- }
-
- }
- else
- {
- cout<< "查无此人" << endl;
- system("pause");
- system("cls");//实现清屏的操作
- }
- }
- }
- void DeleteALLperson(Addressbooks *abs)
- {
- abs->size = 0;
- cout << "通讯录已清空" << endl;
- system("pause");
- system("cls");
- }
- void fixPerson(Addressbooks *abs, string name)
- {
- cin >> name;
- int x = 0;
- for (x = 0; x <= abs->size; x++)
- {
- if (abs->personArray[x].m_name == name)
- {
- int b;
- cout << "已找到该联系人为第" << x << "位" << endl;
- cout << "是否进行操作?" << endl;
- cout << "是---1" << endl;
- cout << "否---2" << endl;
- cout << "请选择修改的属性------" << endl;
- cout << "姓名---1" << endl;
- cout << "性别---2" << endl;
- cout << "年龄---3" << endl;
- cout << "电话号码---4" << endl;
- cout << "地址---5" << endl;
- cin >> b;
- if (b <= 5)
- {
- switch (b)
- {
- case 1:
- {
- cout << "请输入修改后的姓名" << endl;
- string Fixedname;
- cin >> Fixedname;
- abs->personArray[abs->size].m_name = Fixedname;
- break;
- }
- case 2:
- {
- cout << "请输入修改后的性别" << endl;
- char Fixedsex;
- cin >> Fixedsex;
- abs->personArray[abs->size].m_sex = Fixedsex;
- break;
- }
- case 3:
- {
- cout << "请输入修改后的年龄" << endl;
- int Fixedage;
- cin >> Fixedage;
- abs->personArray[abs->size].m_age = Fixedage;
- break;
- }
-
- case 4:
- {
- cout << "请输入修改后的电话号码" << endl;
- string Fixedphone;
- cin >> Fixedphone;
- abs->personArray[abs->size].m_phone = Fixedphone;
- break;
- }
-
- case 5:
- {
- cout << "请输入修改后的地址" << endl;
- string Fixedaddress;
- cin >> Fixedaddress;
- abs->personArray[abs->size].m_address = Fixedaddress;
- break;
- }
-
-
- }
- }
-
-
- else
- {
- cout << "未找到该联系人!!!" << endl;
- }
- }
- }
- }
- int main()
- {
- Addressbooks abs;
- while (1)
- {
- Menu();
- int select = 0;
- cin >> select;
- switch (select)
- {
- case 1:
- {
- addPerson(&abs);
- break;
- }
- case 2:
- {
- showPerson(&abs);
- system("pause");
- system("cls");
- break;
- }
- case 3:
- {
- cout << "请输入要删除的人名" << endl;
- string FINDname;
- cin >> FINDname;
- textbooks(&abs, FINDname);
- deletePerson(&abs);
- break;
- }
- case 4:
- {
- string Findname;
- Findperson(&abs, Findname);
-
- break;
- }
- case 5:
- {
- DeleteALLperson(&abs);
- break;
- }
- case 6:
-
- {
- string Fixname;
- cin >> Fixname;
- fixPerson(&abs, Fixname);
- break;
- }
- case 0:
- {
- cout << "欢迎下次使用" << endl;
- system("pause");
- return 0;
- break;
-
-
-
- system("pause");
- }
- }
-
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。