#include
#include
#include
#include
#include
//#include
using namespace std;
string ID;//具有唯一性
class Person
{
protected:
string No; //学号
string Age;
char Name[20]; //姓名
char postcode[45]; //邮政编码
char address[60];//家庭住址
string Tel; //联系方式
Person *next;
public:
Person(string ID,char *Name,char *postcode, char *address,string Age,string Tel)
{
strcpy(this->Name,Name);
strcpy(this->postcode,postcode);
strcpy(this->address,address);
this->Tel=Tel;
this->No=ID;
this->Age=Age;
}
friend class Manage;
};
class Manage
{
private:
Person *person;
public:
Manage()
{
person=0;
Load();
}
~Manage()
{
Person *p;
p=person;
while(p)
{
p=p->next;
delete person;
person=p;
}
person=0;
}
void Find(char Name[20]);//按姓名查找
void Find(string ID);//按编号查找
void Add(); //添加加信息
void Delete(); //删除信息
void Modify(string ID); //修改信息
void Query(); //查询信息
void Look();//预览
void Save(); //保存数据
void Load(); //读入数据
void DesTory();
void Output(Person *p)
{
cout<<"\t\t学号: "<
};
void Manage::Add()
{
system("cls");
Person *p,*p2; //新结点指针
string No,Age,Tel;
char Name[20],postcode[45],address[60];
char c;
cout<<"\n** 新增学生通讯录 **\n";
//输入学生信息
cout<<"输入学号:\t";
cin>>No;
cout<
Person *p1;
p1=person;
while(p1)
{
if(p1->No==No)
{
break;
}
else
{
p1=p1->next;
}
}
if(p1!=NULL)
{
cout<<"该学号已存在,是否修改该学生信息(Y/N) "<
if(toupper(c)=='Y')
{
cout<<"该学生信息为:"<
cout<
return ;
}
else
return ;
}
}
cout<<"输入姓名:\t";
cin>>Name;
cout<
cin>>postcode;
cout<
cin>>address;
cout<
cin>>Age;
cout<
cin>>Tel;
cout<
p->next=0;
//学生结点加入链表
if(person) //若已经存在结点
{
p2=person;
while(p2->next) //查找尾结点
{
p2=p2->next;
}
p2->next=p; //连接
}
else //若不存在结点(表空)
{
person=p; //连接
}
system("cls");
cout<<"\t\t\t ***添加成功***\n"<
if(toupper(c)=='Y')
{
Add();
return ;
}
else
return ;
}
void Manage::Delete() //删除人员
{
system("cls");
char c;
string No;
cout<<"\n** 删除信息 **\n";
cout<<"输入要删除的学生ID:\t";
cin>>No;
cout<
Person *p1,*p2;
p1=person;
while(p1)
{
if(p1->No==No)
break;
else
{
p2=p1;
p1=p1->next;
}
}
//删除结点
if(p1!=NULL)//若找到结点,则删除
{
cout<<"所要删除的学生的信息如下:\n"<
cout<<"确定是否删除(Y/N): ";
cin>>c;
if(toupper(c)!='Y')
return;
// system("pause");
if(p1==person) //若要删除的结点是第一个结点
{
person=p1->next;
delete p1;
}
else //若要删除的结点是后续结点
{
p2->next=p1->next;
delete p1;
}
cout<<"\t\t***删除成功***\n";
cout<<"是否继续删除(Y/N) "<
if(toupper(c)=='Y')
{
Delete();
return ;
}
else
return ;
}
else //未找到结点
cout<<"未找到该学生!\n";
getch();
}
void Manage::Modify(string ID)
{
Person *p1;
char c;
p1=person;
while(p1)
{
if(p1->No==ID)
break;
else
{
p1=p1->next;
}
}
if(p1!=NULL)//若找到结点
{
system("cls");
cout<<"所要修改的学生的信息如下:\n"<
do
{
cout<<"1. 修改姓名 2. 修改邮编 3. 修改住址 4. 修改年龄 5.修改电话号码 6. 退出修改 \n"<
if(c!='6')
cout<<"请输入新的信息: ";
switch(c)
{
case '1': cin>>p1->Name; break;
case '2': cin>>p1->postcode; break;
case '3': cin>>p1->address; break;
case '4': cin>>p1->Age; break;
case '5': cin>>p1->Tel; break;
default: break;
}
}while(c!='6');
system("cls");
cout<<"\t ***修改成功***\n"<
if(toupper(c)=='Y')
{
cout<<"请输入要修改人员的ID: ";
cin>>ID;
cout<
return ;
}
else
return ;
}
else //未找到结点
cout<<"未找到该学生!\n";
getch();//暂停
}
void Manage::Look()
{
//设置字体颜色
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY |
FOREGROUND_RED | FOREGROUND_GREEN);
system("cls");
Person *p1;
int count=0;
char c;
p1=person;
while(p1)
{
cout<<"ID: "<
p1=p1->next;
}
if(count!=0)
{
cout<<"\n\t\t预览成功!!! \n"<
cin>>c;
if(toupper(c)=='Y')
{
Query();
return;
}
else
return ;
}
else
{
cout<<"尚未创建通讯录,是否创建(Y/N)"<
if(toupper(c)=='Y')
{
Add();
return;
}
else
return ;
}
}
void Manage::Save() //数据写入到文件
{
ofstream fPerson("Person.txt",ios::out);
char c;
cout<<"\n保存数据,是否继续?[Y/N]:";
cin>>c;
if(toupper(c)!='Y')
return;
Person *p=person;
while(p)
{
fPerson<
}
fPerson.close();
cout<<"\n保存成功...\n";
system("pause");
}
void Manage::Load() //数据读入
{
ifstream fPerson;
Person *p=person;
string No,Age,Tel;
char Name[20],postcode[45],address[65];
fPerson.open("person.txt",ios::in);
fPerson>>No>>Name>>postcode>>address>>Age>>Tel;
while(fPerson.good())
{
p=new Person(No,Name,postcode,address,Age,Tel);
p->next=0;
//员工结点加入链表
if(person) //若已经存在结点
{
Person *p2;
p2=person;
while(p2->next) //查找尾结点
{
p2=p2->next;
}
p2->next=p; //连接
}
else //若不存在结点(表空)
{
person=p; //连接
}
fPerson>>No>>Name>>postcode>>address>>Age>>Tel;
}
fPerson.close();
}
void Manage::Find(string ID)
{
Person *p1;
p1=person;
while(p1)
{
if(p1->No==ID)
break;
else
{
p1=p1->next;
}
}
if(p1!=NULL)
{
Output(p1);
}
else
cout<<"未找到该学生"<
void Manage::Find(char Name[20])
{
Person *p1;
int count=0;
p1=person;
while(p1)
{
if(strcmp(p1->Name,Name)==0)
{
count++;
Output(p1);
}
p1=p1->next;
}
if(count)
{
cout<<"\t查询成功!!!"<
else
cout<<"\n\t\t未找到该学生!!!\n"<
void Manage::Query()
{
char c;
string ID,Tel;
char Name[20];
do{
cout<<"1. 按学号查找 2. 按名字查找 3. 按电话号码查找 4.退出查找"<
// system("cls");
cout<
{
case '1': {
cout<<"输入学号 ID: ";
cin>>ID;
Find(ID);
}; break;
case '2': {
cout<<"输入姓名 Name: ";
cin>>Name;
Find(Name);
}; break;
case '3': {
cout<<"输入电话号码 Tel"<
Find(Tel);
};break;
case '4':break;
default: cout<<"输入有误 请重新输入!!!\n"<
}while(c!='1'&&c!='2'&&c!='3'&&c!='4');
cout<<"\t\t\t ***查找成功***\n"<
if(toupper(c)=='Y')
{
Query();
return ;
}
else
return ;
system("pause");
}
void Manage::DesTory()
{ //设置字体为红色
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY |
FOREGROUND_RED);
char c;
system("cls");
cout<<"\n\t\t\t** 清除信息 **\n";
cout<<"警告:\n 清除通讯录信息会导致您保存的信息完全消失!!!\n"<
if(toupper(c)!='Y')
return;
cout<<"请再次确认(Y/N)"<
if(toupper(c)!='Y')
return;
else
{
Person *p;
p=person;
while(p)
{
p=p->next;
delete person;
person=p;
}
person=0;
// ofstream fPerson("person.txt");
// fPerson.close();
}
system("pause");
}
int main(void)
{
Manage m;
int c;
do
{
//设置字体为蓝色
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY |
FOREGROUND_BLUE);
system("cls");
cout<<" =========================================="<
cout<<" =========================================="<
cin>>c;
switch(c)
{
case 1: m.Add(); break;
case 2: m.Delete();break;
case 3: {
system("cls");
cout<<"请输入要修改人员的ID: ";
cin>>ID;
cout<
};break;
case 4: {
system("cls");
m.Query();
}; break;
case 5: m.Save(); break;
case 6: m.DesTory(); break;
case 7: m.Look();break;
default: break;
}
}while(c!=0);
char s;
cout<<"\n★是否要保存您的所有操作(Y/N): "<
if(toupper(s)=='Y')
m.Save();
return 0;
}