#include<iostream>
#include<string>
#include<cstring>
#include<algorithm>
#include<fstream>
using namespace std;
typedef struct
{
int number;//序号
string name;//姓名
string phone;//电话
string qq;//QQ
string email;//电子邮箱
string city;//城市
string zipcode;//邮编
}ElemType;//通讯录数据元素
typedef struct Node
{
ElemType data;
struct Node* next;
}Node, * LinkList;//链表
void Read(LinkList& L);//读取功能
void Save(LinkList& L);//保存功能
void Init(LinkList& L);//链表的初始化
void Add(LinkList& L);//添加功能
void Delete(LinkList& L);//删除功能
void Find(const LinkList& L);//查找功能
void Revise(LinkList& L);//修改功能
void Sort(LinkList& L);//排序功能
void menu(LinkList& L);//界面功能
void Read(LinkList& L)//读取功能
{
Node* p = L;
ifstream infile("E://应用题11/1.txt", ios::in);
int len = 0;
infile >> len;
ElemType tem;
while(len--)
{
infile>>tem.number>>tem.name>>tem.phone>>tem.qq>>tem.email>>tem.city>>tem.zipcode;
Node* t = new Node;
t->data = tem;
t->next = NULL;
p->next = t;
p = p->next;
}
infile.close();
}
void Save(LinkList& L)//保存功能
{
Node* t = L, * cnt = L;
ofstream outfile("E://应用题11/1.txt", ios::out);
int len = 0;
while(cnt->next)
{
len++;
cnt = cnt->next;
}
outfile<<len<<endl;
while(t)
{
if(t != L)
outfile << t->data.number << "\t" << t->data.name << "\t" << t->data.phone << "\t" << t->data.qq << "\t" << t->data.email << "\t\t" << t->data.city << "\t" << t->data.zipcode << endl;
t = t->next;
}
outfile.close();
}
void Init(LinkList& L)//初始化链表
{
L = new Node;
L->next = NULL;
}
void Add(LinkList& L)//添加功能
{
system("cls");//清屏
cout << "\t\t\t添加联系人功能" << endl;
Node* t = L;
int i = 1, flag = 1;
while(t->next)
{
i++;
t = t->next;
}
while (flag)
{
Node* p = new Node;
cout<<"\t\t\t输入序号:";
cin>> p->data.number;
cout<< "\t\t\t输入姓名:";
cin>> p->data.name;
cout<< "\t\t\t输入电话:";
cin>> p->data.phone;
cout<< "\t\t\t输入QQ:";
cin>>p->data.qq;
cout<< "\t\t\t输入电子邮箱:";
cin>>p->data.email;
cout << "\t\t\t输入城市:";
cin >> p->data.city;
cout << "\t\t\t输入邮编:";
cin >> p->data.zipcode;
p->data.number= i++;
p->next = NULL;
t->next = p;
t = t->next;
Save(L);
cout << "\t\t\t成功添加一次,选择是否继续本功能(1 是 0 否)" << endl;
cout << "\t\t\t请选择【0-1】:";
cin >> flag;
}
}
void Delete(LinkList& L)//删除功能
{
system("cls");//清屏
cout << "\t\t\t删除联系人功能" << endl;
int c;
Node* p = L, * t = NULL;
ElemType tem;
cout<< "\t\t\t1.按姓名删除" << endl;
cout<< "\t\t\t2.返回主菜单" << endl;
cout<< "\t\t\t请选择【1-2】:";
cin>>c;
if(c == 1)
{
int flag = 0;
cout << "\t\t\t请输入待删除联系人的姓名:";
cin >> tem.name;
while (p->next)
{
t = p->next;
if (t->data.name == tem.name)
{
cout<< "\t\t\t待删除联系人信息如下:" << endl;
cout<< "\t\t\t编号\t" << "姓名\t" << "电话\t\t" << "QQ\t" << "电子邮箱\t" << "城市\t" << "邮编" << endl;
cout<< "\t\t\t" << t->data.number << "\t" << t->data.name << "\t" << t->data.phone << "\t" << t->data.qq << "\t" << t->data.email << "\t\t" << t->data.city << "\t" << t->data.zipcode << endl;
flag = 1;
break;
}
p = p->next;
}
if (flag == 0)
cout << "\t\t\t该通讯录中无此人!" << endl;
else
{
cout << "\t\t\t确认删除?(1 是 0 否)" << endl;
cout << "\t\t\t请选择【0-1】:";
cin >> c;
if (c == 0);
else if (c == 1)
{
p->next = t->next;
delete t;
cout << "\t\t\t删除成功!" << endl;
Save(L);
}
}
cout << "\n\t\t\t";
system("pause");
Delete(L);
}
else if (c == 2) return;
}
void Find(const LinkList& L)//查询功能
{
system("cls");//清屏
cout << "\t\t\t查询联系人功能" << endl;
int c;
Node* t = L;
ElemType tem;
cout << "\t\t\t1.按编号查询" << endl;
cout << "\t\t\t2.按姓名查询" << endl;
cout << "\t\t\t3.返回主菜单" << endl;
cout << "\t\t\t请选择【1-3】:";
cin >> c;
if (c == 1)
{
int flag = 0;
cout << "\t\t\t请输入待查询联系人的编号:";
cin >> tem.number;
while (t->next)
{
t = t->next;
if (t->data.number == tem.number)
{
cout << "\t\t\t待查询联系人信息如下:" << endl;
cout<< "\t\t\t编号\t" << "姓名\t" << "电话\t\t" << "QQ\t" << "电子邮箱\t" << "城市\t" << "邮编" << endl;
cout<< "\t\t\t" << t->data.number << "\t" << t->data.name << "\t" << t->data.phone << "\t" << t->data.qq << "\t" << t->data.email << "\t\t" << t->data.city << "\t" << t->data.zipcode << endl;
flag = 1;
break;
}
}
if (flag == 0) cout << "\t\t\t该通讯录中无此人!" << endl;
cout << "\n\t\t\t";
system("pause");
Find(L);
}
else if (c == 2)
{
int flag = 0;
cout << "\t\t\t请输入待查询联系人的姓名:";
cin >> tem.name;
while (t->next)
{
t = t->next;
if (t->data.name == tem.name)
{
cout << "\t\t\t待查询联系人信息如下:" << endl;
cout<< "\t\t\t编号\t" << "姓名\t" << "电话\t\t" << "QQ\t" << "电子邮箱\t" << "城市\t" << "邮编" << endl;
cout<< "\t\t\t" << t->data.number << "\t" << t->data.name << "\t" << t->data.phone << "\t" << t->data.qq << "\t" << t->data.email << "\t\t" << t->data.city << "\t" << t->data.zipcode << endl;
flag = 1;
break;
}
}
if (flag == 0) cout << "\t\t\t该通讯录中无此人!" << endl;
cout << "\n\t\t\t";
system("pause");
Find(L);
}
else if (c == 3)
{
return;
}
}
void Revise(LinkList& L)//修改功能
{
system("cls");
cout << "\t\t\t修改联系人功能" << endl;
int c;
Node* t = L;
ElemType tem;
cout << "\t\t\t1.按姓名修改" << endl;
cout << "\t\t\t2.返回主菜单" << endl;
cout << "\t\t\t请选择【1-2】:";
cin >> c;
if (c == 1)
{
int flag = 0;
cout << "\t\t\t请输入待修改联系人的姓名:";
cin >> tem.name;
while (t->next)
{
t = t->next;
if (t->data.name == tem.name)
{
cout << "\t\t\t待修改联系人信息如下:" << endl;
cout<< "\t\t\t编号\t" << "姓名\t" << "电话\t\t" << "QQ\t" << "电子邮箱\t" << "城市\t" << "邮编" << endl;
cout<< "\t\t\t" << t->data.number << "\t" << t->data.name << "\t" << t->data.phone << "\t" << t->data.qq << "\t" << t->data.email << "\t\t" << t->data.city << "\t" << t->data.zipcode << endl;
flag = 1;
break;
}
}
if (flag == 0) cout << "\t\t\t该通讯录中无此人!" << endl;
else
{
ElemType tem;
cout << "\t\t\t输入修改后的联系人信息:" << endl;
cout << "\t\t\t输入姓名:";
cin >> tem.name;
cout << "\t\t\t输入电话:";
cin >> tem.phone;
cout << "\t\t\t输入QQ:";
cin >> tem.qq;
cout << "\t\t\t输入电子邮箱:";
cin >> tem.email;
cout << "\t\t\t输入城市:";
cin >> tem.city;
cout << "\t\t\t输入邮编:";
cin >> tem.zipcode;
tem.number = t->data.number;
cout << "\t\t\t确认修改?(1 是 0 否)" << endl;
cout << "\t\t\t请选择【0-1】:";
cin >> c;
if (c == 0);
else if (c == 1)
{
t->data = tem;
cout << "\t\t\t修改成功!" << endl;
Save(L);
}
}
cout << "\n\t\t\t";
system("pause");
Delete(L);//删除修改前的内容
}
else if (c == 2) return;
}
void Print(const LinkList& L)//显示功能
{
system("cls");//清屏
cout << "\t\t\t显示功能" << endl;
Node* t = L->next;
cout << "\t\t\t序号\t" << "姓名\t"<< "电话\t\t"<< "QQ\t" << "电子邮箱\t" << "城市\t" << "邮编" << endl;
while (t)
{
cout<< "\t\t\t" << t->data.number << "\t" << t->data.name << "\t" << t->data.phone << "\t" << t->data.qq << "\t" << t->data.email << "\t\t" << t->data.city << "\t" << t->data.zipcode << endl;
t = t->next;
}
cout << "\t\t\t";
system("pause");
}
bool compare(const ElemType& t1, const ElemType& t2)//排序中sort函数参数
{
return t1.name < t2.name;
}
void Sort(LinkList& L)//排序功能
{
system("cls");//清屏
cout << "\t\t\t排序功能" << endl;
Node* p = L;
cout << "\t\t\t1.按姓名排序" << endl;
cout << "\t\t\t2.返回主菜单" << endl;
cout << "\t\t\t请选择【1-2】:";
int a = 0, i = 0;
int c;
cin>>c;
while (p->next)
{
p = p->next;
a++;
}
ElemType* arr = new ElemType[a];
p = L;
while (p->next)
{
p = p->next;
arr[i++] = p->data;
}
if(c==1)
{
sort(arr, arr + a, compare);
}
else if(c==2) return;
p = L, i = 0;
while (p->next)
{
p = p->next;
p->data = arr[i++];
p->data.number = i;
}
cout << "\t\t\t对通讯录进行排序并整理如下:" << endl;
Node* t = L->next;
cout << "\t\t\t序号\t" << "姓名\t" <<"电话\t\t" <<"QQ\t" << "电子邮箱\t" << "城市\t" << "邮编" << endl;
while (t)
{
cout<< "\t\t\t" << t->data.number << "\t" << t->data.name << "\t" << t->data.phone << "\t" << t->data.qq << "\t" << t->data.email << "\t\t" << t->data.city << "\t" << t->data.zipcode << endl;
t = t->next;
}
Save(L);
cout << "\n\t\t\t";
system("pause");
Sort(L);
}
void menu(LinkList& L)//界面功能
{
char c;
system("cls");
cout << "\t\t\t通讯录管理系统" << endl;
cout << "\t\t\t你可以进行以下操作:" << endl;
cout << "\t\t\t 1.添加联系人 " << endl;
cout << "\t\t\t 2.删除联系人 " << endl;
cout << "\t\t\t 3.查找联系人 " << endl;
cout << "\t\t\t 4.修改联系人 " << endl;
cout << "\t\t\t 5.通讯录显示 " << endl;
cout << "\t\t\t 6.通讯录排序 " << endl;
cout << "\t\t\t 7.退出 " << endl;
cout << "\t\t\t请选择【1-7】:";
cin >> c;
switch (c)
{
case '1':
Add(L);
menu(L);
break;
case '2':
Delete(L);
menu(L);
break;
case '3':
Find(L);
menu(L);
break;
case '4':
Revise(L);
menu(L);
break;
case '5':
Print(L);
menu(L);
break;
case '6':
Sort(L);
menu(L);
break;
case '7':
exit(0);
default:
menu(L);
}
}
int main()//主函数
{
LinkList L;
Init(L);
Read(L);
menu(L);
return 0;
}
南邮快抄
数据结构-----通讯录管理系统的设计
最新推荐文章于 2024-01-05 09:23:06 发布