#include<iostream.h>
#include<string.h>
#include<fstream.h>
#include<iomanip.h>
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<<"请选择您需要的操作!"<<endl;
cout<<"操作:"<<endl;
cout<<"(0)通讯录录入"<<endl;
cout<<"(1)增加人员"<<endl;
cout<<"(2)删除人员"<<endl;
cout<<"(3)修改数据"<<endl;
cout<<"(4)显示记录"<<endl;
cout<<"(5)退出"<<endl;
cout<<"选择相关操作请输入相对的括号里的阿拉伯数字!"<<endl;
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<<"指令错误!请重新输入:"<<endl;
}while(flag2==0);
switch(p)
{
case '0': //(0)通讯录录入
{
char name[20];
double age,homephone,telphone;
char sex,c;
do{
cout<<"请输入姓名:"<<endl;
cin>>name;
cout<<"请输入性别:"<<endl;
cin>>sex;
cout<<"请输入年龄:"<<endl;
cin>>age;
cout<<"请输入家里的电话号码:"<<endl;
cin>>homephone;
cout<<"请输入移动电话号码:"<<endl;
cin>>telphone;
j++;
s[i]=new stu(name, sex, age, homephone , telphone);
i++;
cout<<"数据录入成功,想继续录入吗(y/n)"<<endl;
cin>>c;
flag2=0;
do
{
if(c!='y'&&c!='n')
{
cout<<"指令错误!请重新输入!"<<endl;
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<<"请输入姓名:"<<endl;
cin>>name;
cout<<"请输入性别:"<<endl;
cin>>sex;
cout<<"请输入年龄:"<<endl;
cin>>age;
cout<<"请输入家里的电话号码:"<<endl;
cin>>homephone;
cout<<"请输入移动电话号码:"<<endl;
cin>>telphone;
j++;
s[i]=new stu(name, sex, age, homephone , telphone);
i++;
cout<<"数据录入成功,想继续录入吗(y/n)"<<endl;
cin>>c;
flag2=0;
do
{
if(c!='y'&&c!='n')
{
cout<<"指令错误!请重新输入!"<<endl;
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<<"请输入您要删除的学生姓名:"<<endl;
cin>>name;
for(int h=0;h<i;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)"<<endl;
cin>>c;
if(c!='y'&&c!='n')
{
cout<<"指令错误!请重新输入!"<<endl;
cin>>c;
}
}while(c=='y');
break; }
case '3': //(3)修改数据(Alter)
{
char name[20],se;double ag,ho,te;flag2=0;
char c;
do
{
cout<<"请输入您要修改的学生的姓名:"<<endl;
cin>>name;
for(int h=0;h<i;h++)
{
if(strcmp(name,s[h]->name)==0)
{
flag2=1;
cout<<"请输入性别:"<<endl;
cin>>se;
cout<<"请输入年龄:"<<endl;
cin>>ag;
cout<<"请输入家里的电话号码:"<<endl;
cin>>ho;
cout<<"请输入移动电话号码:"<<endl;
cin>>te;
s[h]->sex=se;
s[h]->age=ag;
s[h]->homephone=ho;
s[h]->telphone=te;
cout<<"数据修改成功!";
}
}
if(flag2==0)
{
cout<<"您要修改的学生本来就不存在!请检查重新输入!"<<endl;
}
cout<<"想继续修改吗(y/n)"<<endl;
cin>>c;
if(c!='y'&&c!='n')
{
cout<<"指令错误!请重新输入!"<<endl;
cin>>c;
}
}while(c=='y');
break; }
case '4': //(4)显示记录(List)
{
cout<<"本系统所有通讯录的数据如下:"<<endl;
if(i==0)
cout<<"管理系统中没有录入数据或者数据已经被删除!"<<endl;
for(int k=0;k<i;k++)
{
cout<<k+1<<" "<<"姓名:"<<" "<<s[k]->name<<
"性别:"<<" "<<s[k]->sex<<"年龄:"<<" "<<s[k]->age
<<"家里的电话号码:"<<" "<<s[k]->homephone<<"移动电话号码:"
<<" "<<s[k]->telphone<<endl;
}
break; } }
cout<<"您想继续进行其他操作吗?(y/n)"<<endl;
bool flag4=0;
do
{
cin>>p;
if(p!='y'&&p!='n')
cout<<"指令错误!请重新输入!"<<endl;
else
flag4=1;
}while(flag4==0);
if(p=='y')
cout<<"请输入操作代码(0 通讯录录入/1 增加人员(Add)/2 删除人员(Delete)/3 修改数据(Alter)/4 显示记录(List)/ 5 退出(Exit))"<<endl;
cin>>p;
for(int x=0;x<i;x++)
{
delete s[x];
cout<<"删除所有成员!"<<endl;
}
}
回答者:little_goblin - 初入江湖 三级 7-11 09:35
评价已经被关闭 目前有 1 个人评价
好
100% (1) 不好
0% (0)
对最佳答案的评论 共 1 条
看不懂
评论者: hanrenhui88 - 试用期 一级
其他回答 共 1 条
我自己前不久编写的一个通讯录,呵呵.
#include"stdio.h"
#include"stdlib.h"
#include"string.h"
struct student
{
int no; /*学号*/
char name[20]; /*姓名*/
char acdamic[20]; /*院系*/
char major[20]; /*专业*/
char province[20]; /*籍贯*/
char address[40]; /*家庭地址*/
long phone; /*联系电话*/
struct student *next;
};
struct student *input();
void print(struct student *h);
struct student *insert(struct student *h);
struct student *del(struct student *h);
void find4(struct student *h);
void find3(struct student *h);
void find2(struct student *h);
void find1(struct student *h);
struct student *head=NULL;
char ch,*menu[]={"------------------通讯录菜单------------",
"1.----------建立学生通讯录---------------",
"2.----------输出全部学生通讯录---------------",
"3.----------增加的学生的个数---------------",
"4.----------删除指定学号的学生--------------",
"5.----------按系别查找学生信息-------------",
"6.----------按专业查找学生信息-------------",
"7.----------按姓名查找学生信息-------------",
"8.----------按学号查找学生信息--------------",
"9.----------退出通讯录-----------------------"};
struct student *input() /*输入函数*/
{
int n;
printf("请输入你要建立的学生通讯录的学生个数:\n");
scanf("%d",&n);
struct student *h=NULL,*p,*q;
int i;
for(i=1;i<=n;i++)
{
printf("请依次输入第%d个学生的学号,姓名,系别,专业,籍贯,家庭住址和联系电话:\n",i);
p=(struct student *)malloc(sizeof(struct student));
if(p==NULL)
{
printf("内存不足!\n");
exit(0);
}
scanf("%d%s%s%s%s%s%ld",&p->no,p->name,p->acdamic,p->major,p->province,p->address,&p->phone);
if(i==1)h=p;
else q->next=p;
q=p;
}
q->next=NULL;
return h;
}
void print(struct student *h) /*输出函数*/
{
struct student *p=h;
while(p)
{
printf("学生信息:\n%d,%s,%s,%s,%s,%s,%ld\n",p->no,p->name,p->acdamic,p->major,p->province,p->address,p->phone);
p=p->next;
}
}
struct student *insert(struct student *h) /*增加学生信息*/
{
int n;
printf("请输入你要增加的学生的个数:\n");
scanf("%d",&n);
struct student *p,*r;
int i;
for(i=1;i<=n;i++)
{
r=(struct student *)malloc(sizeof(struct student));
printf("请输入第%d个你要插入的学生的信息:\n",i);
scanf("%d%s%s%s%s%s%ld",&r->no,r->name,r->acdamic,r->major,r->province,r->address,&r->phone);
p=h;
h=r;
r->next=p;
}
return h;
}
struct student *del(struct student *h) /*按学号进行删除*/
{
int n;
printf("请输入要删除的学生的学号:\n");
scanf("%d",&n);
struct student *p,*q;
if(h=NULL)printf("empty list!\n");
else
{
p=h;
while(p->no!=n&&p->next)
{q=p;p=p->next;}
if(p->no==n)
{
if(h==p)h=p->next;
else q->next=p->next;
}
else printf("%d is not found!\n");
}
return h;
}
void find1(struct student *h) /*按院系查找学生信息*/
{
char s[40];
printf("请输入要查找的学生的院系:\n");
scanf("%s",s);
struct student *p;
if(h==NULL)printf("empty list!\n");
else
{
p=h;
while(p)
{
if(strcmp(p->acdamic,s)==0)
printf("学生信息:\n%d %s %s %s %s %s %ld\n",p->no,p->name,p->acdamic,p->major,p->province,p->address,p->phone);
p=p->next;
}
}
}
void find2(struct student *h) /*按专业查找*/
{
char s[20];
printf("请输入要查找的学生的专业名:\n");
scanf("%s",s);
struct student *p;
if(h==NULL)printf("empty list!\n");
else
{
p=h;
while(p)
{
if(strcmp(p->major,s)==0)
printf("学生信息:\n%d %s %s %s %s %s %ld\n",p->no,p->name,p->acdamic,p->major,p->province,p->address,p->phone);
p=p->next;
}
}
}
void find3(struct student *h) /*按姓名查找*/
{
char ss[10];
printf("请输入要查找的学生的姓名:\n");
scanf("%s",ss);
struct student *p;
if(h==NULL)printf("empty list!\n");
else
{
p=h;
while(p)
{
if(strcmp(p->name,ss)==0)
printf("学生信息:\n%d %s %s %s %s %s %ld\n",p->no,p->name,p->acdamic,p->major,p->province,p->address,p->phone);
p=p->next;
}
}
}
void find4(struct student *h) /*按学号查找*/
{
int num;
printf("请输入要查找的学生的学号:\n");
scanf("%d",&num);
struct student *p;
if(h==NULL)printf("empty list!\n");
else
{
p=h;
while(p)
{
if(p->no==num)
printf("学生信息:\n%d %s %s %s %s %s %ld\n",p->no,p->name,p->acdamic,p->major,p->province,p->address,p->phone);
else
printf("没有你要查找的学生信息!\n");
p=p->next;
}
}
}
int menu_select()
{
int i,s;
char c[3];
for(i=0;i<10;i++)
printf("%s\n",menu[i]);
do
{
scanf("%s",c);
s=atoi(c);
}while(s<0||s>9);
return s;
}
main()
{
for(;;)
{
switch(menu_select())
{
case 1:head=input();break;
case 2:print(head);break;
case 3:head=insert(head);break;
case 4:head=del(head);break;
case 5:find1(head);break;
case 6:find2(head);break;
case 7:find3(head);break;
case 8:find4(head);break;
case 9:exit(0);
}
}
}
温馨提示:答案为网友推荐,仅供参考