用C语言编写一个通讯录系统,集合数据结构双向链表知识

2025-01-02 20:54:04
推荐回答(1个)
回答1:

//类似//#include "stdafx.h"
#include
#include

#include
class stu
{
char name[20];
double age,homephone,telphone;
char sex;
public:
stu(){}
stu(char n[20],char se,double ag,double ho,double te)
{
strcpy(name, n);
age=ag;
homephone=ho;
telphone=te;
}
friend void main();
}; void main()
{
cout<<"请选择您需要的操作!"<cout<<"操作:"<cout<<"(0)通讯录录入"<cout<<"(1)增加人员"<cout<<"(2)删除人员"<cout<<"(3)修改数据"<cout<<"(4)显示记录"<cout<<"(5)退出"<cout<<"选择相关操作请输入相对的括号里的阿拉伯数字!"<stu *s[50];
int i=0;
int j=0;
bool flag2=0;
char p;
do
{
cin>>p;
if((p>='0'&&p<='5'))
flag2=1;
else
cout<<"指令错误!请重新输入:"<}while(flag2==0); switch(p)
{ case '0': //(0)通讯录录入
{
char name[20];
double age,homephone,telphone;
char sex,c;
do{ cout<<"请输入姓名:"<cin>>name;
cout<<"请输入性别:"<cin>>sex;
cout<<"请输入年龄:"<cin>>age;
cout<<"请输入家里的电话号码:"<cin>>homephone;
cout<<"请输入移动电话号码:"<cin>>telphone;
j++;
s[i]=new stu(name, sex, age, homephone , telphone);
i++;
cout<<"数据录入成功,想继续录入吗(y/n)"<cin>>c;
flag2=0;
do
{
if(c!='y'&&c!='n')
{
cout<<"指令错误!请重新输入!"<cin>>c;
}
else
flag2=1;
}while(flag2==0);
}while(c=='y');
break; }
////////////////////////////////////////////////////////////////////
case '1': //(1)增加人员(Add)
{
char name[20];
double age,homephone,telphone;
char sex,c;
do{ cout<<"请输入姓名:"<cin>>name;
cout<<"请输入性别:"<cin>>sex;
cout<<"请输入年龄:"<cin>>age;
cout<<"请输入家里的电话号码:"<cin>>homephone;
cout<<"请输入移动电话号码:"<cin>>telphone;
j++;
s[i]=new stu(name, sex, age, homephone , telphone);
i++;
cout<<"数据录入成功,想继续录入吗(y/n)"<cin>>c;
flag2=0;
do
{
if(c!='y'&&c!='n')
{
cout<<"指令错误!请重新输入!"<cin>>c;
}
else
flag2=1;
}while(flag2==0);
}while(c=='y');
break; } case '2': //(2)删除人员(Delete)
{
char name[20];bool flag3=0;char c;
do{
cout<<"请输入您要删除的学生姓名:"<cin>>name;
for(int h=0;h{
if(strcmp(name,s[h]->name)==0)
{
flag3=1;
i--;
do{
s[h]=s[h+1];
h++;
}while(h<=i);
}
}
if(flag3==0)
cout<<"您要求删除的对象本来就不存在!请检查输入的正确性!";
cout<<"要继续删除吗?(y/n)"<cin>>c;
if(c!='y'&&c!='n')
{
cout<<"指令错误!请重新输入!"<cin>>c;
}
}while(c=='y');
break; }
case '3': //(3)修改数据(Alter)
{
char name[20],se;double ag,ho,te;flag2=0;
char c;
do
{
cout<<"请输入您要修改的学生的姓名:"<cin>>name;
for(int h=0;h{
if(strcmp(name,s[h]->name)==0)
{
flag2=1;
cout<<"请输入性别:"<cin>>se;
cout<<"请输入年龄:"<cin>>ag;
cout<<"请输入家里的电话号码:"<cin>>ho;
cout<<"请输入移动电话号码:"<cin>>te;
s[h]->sex=se;
s[h]->age=ag;
s[h]->homephone=ho;
s[h]->telphone=te;
cout<<"数据修改成功!";
}
}
if(flag2==0)
{
cout<<"您要修改的学生本来就不存在!请检查重新输入!"<}
cout<<"想继续修改吗(y/n)"<cin>>c;
if(c!='y'&&c!='n')
{
cout<<"指令错误!请重新输入!"<cin>>c;
}
}while(c=='y');
break; }
case '4': //(4)显示记录(List)
{
cout<<"本系统所有通讯录的数据如下:"<if(i==0)
cout<<"管理系统中没有录入数据或者数据已经被删除!"<for(int k=0;k{
cout<name<<
"性别:"<<" "<sex<<"年龄:"<<" "<age
<<"家里的电话号码:"<<" "<homephone<<"移动电话号码:"
<<" "<telphone<}
break; } }
cout<<"您想继续进行其他操作吗?(y/n)"<bool flag4=0;
do
{
cin>>p;
if(p!='y'&&p!='n')
cout<<"指令错误!请重新输入!"<else
flag4=1;
}while(flag4==0); if(p=='y')
cout<<"请输入操作代码(0 通讯录录入\n1 增加人员(Add)\n2 删除人员(Delete)\n3 修改数据(Alter)\n4 显示记录(List)\n 5 退出(Exit))"<cin>>p;
for(int x=0;x{
delete s[x];
cout<<"删除所有成员!"<} }