速求c语言编简单通讯录源程序满足如下要求,

2024-12-18 07:51:15
推荐回答(1个)
回答1:

#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学号: "<No< cout<<"\t\t姓名: "<Name< cout<<"\t\t邮编地址: "<postcode< cout<<"\t\t家庭住址: "<address< cout<<"\t\t联系电话: "<Tel< cout< }
};
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) "< cin>>c;
if(toupper(c)=='Y')
{
cout<<"该学生信息为:"< Find(No);
cout< Modify(No);
return ;
}
else
return ;
}
}
cout<<"输入姓名:\t";
cin>>Name;
cout< cout<<"输入邮编地址:\t";
cin>>postcode;
cout< cout<<"输入家庭住址:\t";
cin>>address;
cout< cout<<"输入年龄:\t";
cin>>Age;
cout< cout<<"输入联系方式:\t";
cin>>Tel;
cout< p=new Person(No,Name,postcode,address,Age,Tel);
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"< cout<<"是否继续添加(Y/N) "< cin>>c;
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"< Output(p1);
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) "< cin>>c;
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"< Output(p1);
do
{
cout<<"1. 修改姓名 2. 修改邮编 3. 修改住址 4. 修改年龄 5.修改电话号码 6. 退出修改 \n"< cout<<"请选择(1-5)要修改的信息\n"< cin>>c;
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"< cout<<"是否继续修改(Y/N): "< cin>>c;
if(toupper(c)=='Y')
{
cout<<"请输入要修改人员的ID: ";
cin>>ID;
cout< Modify(ID);
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: "<No<<"\t姓名: "<Name< count++;
p1=p1->next;
}
if(count!=0)
{
cout<<"\n\t\t预览成功!!! \n"< cout<<"查询详细信息(Y/N): ";
cin>>c;
if(toupper(c)=='Y')
{
Query();
return;
}
else
return ;
}
else
{
cout<<"尚未创建通讯录,是否创建(Y/N)"< cin>>c;
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<No<<" "<Name<<" "<postcode<<" "<address<<" "<Age<<" "<Tel< p=p->next;
}
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查询成功!!!"< cout<<"\n共找到 "< }
else
cout<<"\n\t\t未找到该学生!!!\n"<}
void Manage::Query()
{
char c;
string ID,Tel;
char Name[20];
do{
cout<<"1. 按学号查找 2. 按名字查找 3. 按电话号码查找 4.退出查找"< cin>>c;
// system("cls");
cout< switch(c)
{
case '1': {
cout<<"输入学号 ID: ";
cin>>ID;
Find(ID);
}; break;
case '2': {
cout<<"输入姓名 Name: ";
cin>>Name;
Find(Name);
}; break;
case '3': {
cout<<"输入电话号码 Tel"< cin>>Tel;
Find(Tel);
};break;
case '4':break;
default: cout<<"输入有误 请重新输入!!!\n"< }
}while(c!='1'&&c!='2'&&c!='3'&&c!='4');
cout<<"\t\t\t ***查找成功***\n"< cout<<"是否继续查找(Y/N) "< cin>>c;
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"< cout<<"★是否决定清除通讯录信息(Y/N): "< cin>>c;
if(toupper(c)!='Y')
return;
cout<<"请再次确认(Y/N)"< cin>>c;
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<<" \t 1.新增通讯录 \t"< cout<<" \t 2.删除通讯录 \t"< cout<<" \t 3.修改通讯录 \t"< cout<<" \t 4.查询详细信息 \t"< cout<<" \t 5.保存数据 \t"< cout<<" \t 6.清除通讯录 \t"< cout<<" \t 7.预览通讯录 \t"<
cout<<" =========================================="< cout<<"0-退出\t请选择(1-7): ";
cin>>c;
switch(c)
{
case 1: m.Add(); break;
case 2: m.Delete();break;
case 3: {
system("cls");
cout<<"请输入要修改人员的ID: ";
cin>>ID;
cout< m.Modify(ID);
};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): "< cin>>s;
if(toupper(s)=='Y')
m.Save();
return 0;
}