#include<iostream>
#include<cstring>
#include<cstdlib>
#include<fstream>
#include<windows.h>
using namespace std;
class Message
{
public:
char name[20]; //名字
char adress[20]; //单位
char call[15]; //固定电话
char phone[15]; //移动电话
char unit[10]; //分类
char EMALL[20]; //E-mail
char QQ[15]; //QQ
Message *next;
};
class Booklist
{
public:
Booklist(); //构造函数
~Booklist(); //析构函数
void Input(); //输入函数
void Output(); //输出函数
void Delet(); //删除
void Insert(); //插入
void Find(); //查找
void Show(); //展示界面
void Amend(); //修改函数
void In(); //导入文件
void Out(); //导出文件
private:
Message *head;
};
Booklist::Booklist()//构造函数
{
head=new Message;//开辟空间
if(head==NULL)
{
cout<<"开辟失败"<<endl;
}
head->next=NULL;
}
Booklist::~Booklist()
{
free(head);//释放空间
}
void Booklist::Show()
{
system("cls");
cout<<"\t\t\t\t\t欢迎使用屈大帅管理系统\t\t\t\t\t"<<endl;
cout<<endl;
cout<<endl;
cout<<"\t\t\t\t\t\t请输入你的选择\t\t\t\t\t\t"<<endl;
cout<<"\t\t\t\t\t\t1.输入\t\t\t\t\t\t"<<endl;
cout<<"\t\t\t\t\t\t2.输出\t\t\t\t\t\t"<<endl;
cout<<"\t\t\t\t\t\t3.增加\t\t\t\t\t\t"<<endl;
cout<<"\t\t\t\t\t\t4.删除\t\t\t\t\t\t"<<endl;
cout<<"\t\t\t\t\t\t5.查找\t\t\t\t\t\t"<<endl;
cout<<"\t\t\t\t\t\t6.修改\t\t\t\t\t\t"<<endl;
cout<<"\t\t\t\t\t\t7.保存\t\t\t\t\t\t"<<endl;
cout<<"\t\t\t\t\t\t8.导出\t\t\t\t\t\t"<<endl;
cout<<"\t\t\t\t\t\t0.退出\t\t\t\t\t\t"<<endl;
}
void Booklist::Input()
{
Message *pre;
pre=new Message;
Message *p=head;
while(p->next!=NULL)
{
p=p->next;
}//找到尾节点
cout<<"请输入联系人的信息:"<<endl;
cout<<"姓名:";
cin>>pre->name;
cout<<"单位:";
cin>>pre->adress;
cout<<"call:";
cin>>pre->call;
cout<<"phone:";
cin>>pre->phone;
cout<<"分类:";
cin>>pre->unit;
cout<<"EMALL:";
cin>>pre->EMALL;c
cout<<"QQ:";
cin>>pre->QQ;
pre->next=NULL;//pre为尾节点
p->next=pre;
}
void Booklist::Output()
{
system("cls");
int s=1,count=0;
Message *p=head->next;
while(p!=NULL)
{
cout<<"第"<<s<<"个联系人信息如下"<<endl;
cout<<"姓名:"<<p->name<<" 单位:"<<p->adress<<" 固定电话:"<<p->call<<" 移动电话:"<<p->phone
<<" 分类:"<<p->unit<<" EMALL:"<<p->EMALL<<" QQ:"<<p->QQ<<endl;
p=p->next;
s++;
count++;
if(count%10==0)
{
cout<<"请按任意键翻页"<<endl;
system("pause");
system("cls");
}
}
}
void Booklist::Insert()
{
system("cls");
Message *pre;
pre=new Message;
Message *p=head;
int n;
while(p->next!=NULL)
{
p=p->next;
}
cout<<"请输入要添加的联系人信息:"<<endl;
cout<<"姓名:";
cin>>pre->name;
cout<<"单位:";
cin>>pre->adress;
cout<<"call:";
cin>>pre->call;
cout<<"phone:";
cin>>pre->phone;
cout<<"分类:";
cin>>pre->unit;
cout<<"EMALL:";
cin>>pre->EMALL;
cout<<"QQ:";
cin>>pre->QQ;
pre->next=NULL;
p->next=pre;
}
void Booklist::Delet()
{
system("cls");
char d_name[20];
Message *p=head;
Message *q;
q=p->next;
cout<<"请输入需要删除的联系人姓名:";
cin>>d_name;
while(q)//遍历
{
if (strcmp(q->name, d_name) == 0 )
{
p->next = q->next;//删除q节点,使原来指向q节点的指针直接指向q的下一个节点
free(q); //删除q节点
break;
}
else
{
p = p->next;
q = q->next;
}
}
}
void Booklist::Find()
{
system("cls");
int choice;
char c_name[20];
char c_adress[20];
char c_unit[20];
Message *p=head->next;
int flag=0;//flag用于判定是否有查询结果
cout<<"请选择要查询的方式"<<endl;
cout<<"1.姓名"<<endl;
cout<<"2.单位"<<endl;
cout<<"3.分类"<<endl;
cin>>choice;
switch(choice)
{
case 1:
cout<<"请输入要查询的姓名:";
cin>>c_name;
cout<<"查询结果如下"<<endl;
while(p!=NULL)
{
if(strcmp(p->name,c_name)==0)
{
cout<<"姓名:";
cout<<p->name<<endl;
cout<<"单位:";
cout<<p->adress<<endl;
cout<<"固定电话:";
cout<<p->call<<endl;
cout<<"移动手机:";
cout<<p->phone<<endl;
cout<<"分类:";
cout<<p->unit<<endl;
cout<<"EMALL:";
cout<<p->EMALL<<endl;
cout<<"QQ:";
cout<<p->QQ<<endl;
cout<<endl;
flag++;
}
p=p->next;
}
if(flag==0)
{
cout<<"无此信息!!"<<endl;
}
break;
case 2:
cout<<"请输入要查询的单位:";
cin>>c_adress;
cout<<"查询结果如下"<<endl;
while(p!=NULL)
{
if(strcmp(p->adress,c_adress)==0)
{
cout<<"姓名:";
cout<<p->name<<endl;
cout<<"单位:";
cout<<p->adress<<endl;
cout<<"固定电话:";
cout<<p->call<<endl;
cout<<"移动手机:";
cout<<p->phone<<endl;
cout<<"分类:";
cout<<p->unit<<endl;
cout<<"EMALL:";
cout<<p->EMALL<<endl;
cout<<"QQ:";
cout<<p->QQ<<endl;
cout<<endl;
flag++;
}
p=p->next;
}
if(flag==0)
{
cout<<"无此信息!!"<<endl;
}
break;
case 3:
cout<<"请输入要查询的分类:";
cin>>c_unit;
cout<<"查询结果如下"<<endl;
while(p!=NULL)
{
if(strcmp(p->unit,c_unit)==0)
{
cout<<"姓名:";
cout<<p->name<<endl;
cout<<"单位:";
cout<<p->adress<<endl;
cout<<"固定电话:";
cout<<p->call<<endl;
cout<<"移动手机:";
cout<<p->phone<<endl;
cout<<"分类:";
cout<<p->unit<<endl;
cout<<"EMALL:";
cout<<p->EMALL<<endl;
cout<<"QQ:";
cout<<p->QQ<<endl;
cout<<endl;
flag++;
}
p=p->next;
}
if(flag==0)
{
cout<<"无此信息!!"<<endl;
}
break;
}
}
void Booklist::Amend()
{
char A_name[20];
char A_adress[20];
char A_call[15];
char A_phone[15];
char A_unit[10];
char A_EMALL[20];
char A_QQ[15];
int amend=0;
Message *p;
p=head->next;
cout<<"请输入要修改信息的联系人姓名:";
cin>>A_name;
while(p)
{
if(strcmp(A_name,p->name)==0)
{
cout<<"该联系人信息如下"<<endl;
cout<<"姓名:";
cout<<p->name<<endl;
cout<<"单位:";
cout<<p->adress<<endl;
cout<<"固定电话:";
cout<<p->call<<endl;
cout<<"移动手机:";
cout<<p->phone<<endl;
cout<<"分类:";
cout<<p->unit<<endl;
cout<<"EMALL:";
cout<<p->EMALL<<endl;
cout<<"QQ:";
cout<<p->QQ<<endl;
cout<<endl;
cout<<"请输入要修改的信息"<<endl;
cout<<"姓名:";
cin>>A_name;
cout<<"单位:";
cin>>A_adress;
cout<<"call:";
cin>>A_call;
cout<<"phone:";
cin>>A_phone;
cout<<"分类:";
cin>>A_unit;
cout<<"EMALL:";
cin>>A_EMALL;
cout<<"QQ:";
cin>>A_QQ;
strcpy(p->name,A_name);//strcpy拷贝函数
strcpy(p->adress,A_adress);
strcpy(p->call,A_call);
strcpy(p->phone,A_phone);
strcpy(p->unit,A_unit);
strcpy(p->EMALL,A_EMALL);
strcpy(p->QQ,A_QQ);
amend++;
break;
}
else
{
p=p->next;
}
}
if(amend==0)
{
cout<<"查无此人!"<<endl;
}
}
void Booklist::In()
{
Message *p;
fstream file;
file.open("通讯录.txt",ios::out);
if(!file)
{
cout<<"打开文件失败!"<<endl;
exit(1);
}
p=head->next;
while(p!=NULL)
{
file<<p->name<<" "<<p->adress<<" "<<p->call<<" "<<p->phone<<" "<<p->unit<<" "<<p->EMALL<<" "<<p->QQ<<endl;
p=p->next;
}
file.close();
cout<<"保存成功!!"<<endl;
}
void Booklist::Out()
{
Message *q=head->next;
fstream file;
file.open("通讯录.txt",ios::in);
if(!file)
{
cout<<"文件打开失败!"<<endl;
}
while(q)
{
file>>q->name>>q->adress>>q->call>>q->phone>>q->unit>>q->EMALL>>q->QQ;
cout<<" 姓名:"<<q->name<<" 单位:"<<q->adress<<" 固定电话:"<<q->call<<" 移动手机:"<<q->phone<<" 分类:"<<q->unit<<" EMALL:"<<q->EMALL<<" QQ:"<<q->QQ<<endl;
q=q->next;
}
file.close();
}
int main()
{
Booklist L;
int choose;
int n;
int count=0;
int sum=0;
system("color B5");
for ( int k = 0; k < 15; k++)
{
cout<<" █";
Sleep(100);
}
cout<<endl;
while(sum==0)
{
L.Show();
cout<<endl;
cout<<"请输入选择:";
cin>>choose;
switch(choose)
{
case 1:
cout<<"请输入要记录的联系人个数:";
cin>>n;
for(int i=1;i<=n;i++)
{
cout<<"第"<<i<<"个联系人信息为"<<endl;
L.Input();
}
system("pause");
break;
case 2:
L.Output();
system("pause");
break;
case 3:
L.Insert();
cout<<"插入成功!!"<<endl;
system("pause");
break;
case 4:
L.Delet();
cout<<"删除成功!!"<<endl;
system("pause");
break;
case 5:
L.Find();
system("pause");
break;
case 6:
L.Amend();
system("pause");
break;
case 7:
L.In();
system("pause");
break;
case 8:
L.Out();
system("pause");
break;
case 0:
sum++;
cout<<"再见!!"<<endl;
break;
}
}
return 0;
}