用线性表实现通讯录管理
功能6有很大问题,实现不了功能,有时能输出,但只能输出一条信息(多条信息符合条件时);当无符合条件的信息时,没有输出相应信息
此外,地址输出字数不一时,表格对不齐,请问有什么改进的办法
万分感谢
//题目3——应用实验
#include<iostream>
using namespace std;
#include<string>
#define MAX 50
#include<iomanip>
#include<vector>
void menu()//展示菜单栏
{
cout << "--------------------------------------" << endl;
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;
}
struct DataType//通讯录要存储的信息
{
int ID;//编号
string name;//姓名
string ch;//性别
string phone;//电话
string addr;//地址
string classify;//分类
};
struct Data_s
{
struct DataType people[MAX];//通讯录能保存的最大人数
int length = 0;//通讯录中已有人数
};
void Assign(Data_s* Ds)
{
int ID_;//ID
cout << "请输入联系人ID:";
cin >> ID_;
Ds->people[Ds->length].ID = ID_;
string name_;//姓名
cout << "请输入联系人姓名:";
cin >> name_;
Ds->people[Ds->length].name = name_;
string ch_;//性别
cout << "请输入联系人性别:";
cin >> ch_;
Ds->people[Ds->length].ch = ch_;
string phone_;//电话
cout << "请输入联系人电话号码:";
cin >> phone_;
Ds->people[Ds->length].phone = phone_;
string addr_;//地址
cout << "请输入联系人地址:";
cin >> addr_;
Ds->people[Ds->length].addr = addr_;
int cla;//分类
cout << "请输入联系人分类(1——好友;2——同学;3——黑名单):";
while (true)
{
cin >> cla;
if (cla == 1 || cla == 2 || cla == 3)
{
if (cla == 1)
Ds->people[Ds->length].classify = "好友";
if (cla == 2)
Ds->people[Ds->length].classify = "同学";
if (cla == 3)
Ds->people[Ds->length].classify = "黑名单";
}
break;
}
}
void Assign(Data_s* Ds, int num)
{
int ID_;//ID
cout << "请输入联系人ID:";
cin >> ID_;
Ds->people[num].ID = ID_;
string name_;//姓名
cout << "请输入联系人姓名:";
cin >> name_;
Ds->people[num].name = name_;
string ch_;//性别
cout << "请输入联系人性别:";
cin >> ch_;
Ds->people[num].ch = ch_;
string phone_;//电话
cout << "请输入联系人电话号码:";
cin >> phone_;
Ds->people[num].phone = phone_;
string addr_;//地址
cout << "请输入联系人地址:";
cin >> addr_;
Ds->people[num].addr = addr_;
int cla;//分类
cout << "请输入联系人分类(1——好友;2——同学;3——黑名单):";
while (true)
{
cin >> cla;
if (cla == 1 || cla == 2 || cla == 3)
{
if (cla == 1)
Ds->people[num].classify = "好友";
if (cla == 2)
Ds->people[num].classify = "同学";
if (cla == 3)
Ds->people[num].classify = "黑名单";
}
}
}
void ADD(Data_s* Ds)
{
//首先判断通讯录是否已满
if (Ds->length == MAX)
cout << "通讯录已满" << endl;
cout << "请根据提示输入联系人相关信息" << endl;
Assign(Ds);
Ds->length++;
cout << "新联系人已添加成功!" << endl;
system("pause");
system("cls");//清屏
}
vector<int> existance(Data_s* Ds, int cla)//根据分类查找联系人是否存在,若存在,返回该联系人所属下标
{
string cla_;
if (cla == 1)
cla_ = "好友";
else if (cla == 2)
cla_ = "同学";
else if (cla == 3)
cla_ = "黑名单";
vector<int>v3;
for (int j = 0; j < Ds->length; j++)
{
vector<int>v2;
int k = 0;
if (Ds->people[j].classify == cla_)
{
v2.push_back(j);
k++;
}
return v2;
}
return v3;//查找失败
}
int existance(Data_s* Ds, string name)//根据姓名查找联系人是否存在,若存在,返回该联系人所属下标
{
for (int i = 0; i < Ds->length; i++)
{
if (Ds->people[i].name == name)
return i;
}
return -1;//查找失败
}
void ShowLine()
{
int j;
for (j = 0; j < 44; j++)
{
cout << "——";
}
cout << endl;
}
void DELETE(Data_s* Ds, int num)
{
DataType ds = Ds->people[num];
for (int i = 0; i < Ds->length - 1; i++)
Ds->people[i] = Ds->people[i + 1];
Ds->length--;
}
void ShowHeading()
{
//打印标题
ShowLine();
cout << "|\t\t\t\t\t" << "通讯录" << setw(40) << "\t\t\t\t\t\t|" << endl;
ShowLine();
cout << "|ID\t\t" << "|姓名\t" << "|性别\t"<< "|电话\t\t\t" << "|地址\t\t\t"<< "|分类\t" <<"|" << endl;
ShowLine();
}
void SHOW(Data_s* Ds)
{
if (Ds->length == 0)
{
cout << "通讯录为空,请添加联系人!" << endl;
system("pause");
system("cls");
return;
}
//打印标题
ShowHeading();
//输出内容
for (int i = 0; i < Ds->length; i++)
{
cout << "|" << Ds->people[i].ID << "\t\t|" << Ds->people[i].name << "\t|" << Ds->people[i].ch << "\t|" << Ds->people[i].phone
<< "\t\t|" << Ds->people[i].addr << "\t\t|" << Ds->people[i].classify << "\t|" << endl;
ShowLine();
}
system("pause");
system("cls");
}
void SHOW(Data_s* Ds, int cla)
{
if (cla)
{
cout << "该分类通讯录为空" << endl;
return;
}
//输出内容
cout << "|" << Ds->people[cla].ID << "\t\t|" << Ds->people[cla].name << "\t|" << Ds->people[cla].ch << "\t|"
<< Ds->people[cla].phone << "\t\t|" << Ds->people[cla].addr << "\t\t|" << Ds->people[cla].classify << "\t|" << endl;
ShowLine();
}
int main()
{
Data_s Ds;//创建通讯录
Ds.length = 0;
while (true)
{
int select = 0;//用户选择输入的数字
menu();//展示菜单栏
cin >> select;
switch (select)
{
case 1://添加联系人ADD
ADD(&Ds);
break;
case 2://删除联系人DELETE
{
cout << "请输入要删除的联系人姓名";
string name;
cin >> name;
int num;
num = existance(&Ds, name);
if (num == -1)
{
cout << "您要删除的联系人不存在" << endl;
}
else
{
DELETE(&Ds, num);
cout << name << "已被移除通讯录" << endl;
}
system("pause");
system("cls");
}
break;
case 3://查找联系人
{
cout << "请输入要查找的联系人姓名:";
string name;
cin >> name;
int num;
num = existance(&Ds, name);
if (num == -1)
{
cout << "您所查找的联系人不存在" << endl;
}
else
{
ShowHeading();
cout << "|" << Ds.people[num].ID<< "\t\t|" << Ds.people[num].name <<"\t|"<< Ds.people[num].ch <<"\t|" << Ds.people[num].phone
<< "\t\t|" << Ds.people[num].addr<<"\t\t|" << Ds.people[num].classify << "\t|" << endl;
ShowLine();
}
system("pause");
system("cls");
}
break;
case 4://更改通讯录ALTER
{
cout << "请输入要更改信息的联系人姓名:";
string name;
cin >> name;
int num;
num = existance(&Ds, name);
if (num == -1)
cout << "您所查找的联系人不存在" << endl;
else
{
Assign(&Ds, num);
cout << "已更改成功!" << endl;
}
system("pause");
system("cls");
}
break;
case 5://显示通讯录SHOW
SHOW(&Ds);
break;
case 6://按分类显示通讯录SHOW的重载
{
cout << "请输入想要显示的分类(1——好友;2——同学;3——黑名单):";
int cla;
cin >> cla;
vector<int>v;
v = existance(&Ds, cla);
for (int i = 0; i < v.capacity(); i++)
{
ShowHeading();
while ((v[i] + 1) != 0)
{
SHOW(&Ds, v[i]);
break;
}
system("pause");
system("cls");
}
}
break;
case 0://退出通讯录EXIT
cout << "您已退出通讯录" << endl;
system("pause");
return 0;
break;
}
}
return 0;
}