求个c语言通讯录代码,要求如下
3个回答
展开全部
#include<iostream>
#include<string>
#include<cstring>
using namespace std;
int show();
int add();
int lookup();
int print();
int isFull(int a);
int totalnumber;
int counter=0;
struct ren
{
int xuhao;
char xingming[20];
char haoma[12];
} user[100];
int main()
{ cout<<"please enter the total number to save:";
cin>>totalnumber;
show();
return 0;
}
int show()
{
cout<<"what do you want to do?"<<endl;
cout<<"1.Add a new contact."<<endl;
cout<<"2.Look up a existing number."<<endl;
cout<<"3.Print all contacts."<<endl;
cout<<"4.Exit."<<endl;
int i;
cin>>i;
if(i==1)
add();
else if(i==2)
lookup();
else if(i==3)
print();
else if(i==4)
return 0;
}
int add()
{
cout<<"enter a name:";
char name[20];
cin>>name;
cout<<"enter the phone number:";
char number[12];
cin>>number;
user[counter].xuhao=counter;
strcpy(user[counter].xingming,name);
strcpy(user[counter].haoma,number);
cout<<"the new contact is saved!"<<endl;
++counter;
isFull(counter);
show();
return 0;
}
int lookup()
{
int j=0;
cout<<"please enter the name to look up:";
char name[20];
cin>>name;
for(int i=0;i<counter;++i)
{
if(strcmp(user[i].xingming,name)==0)
{
cout<<user[i].xuhao+1<<" "<<user[i].xingming<<" "<<user[i].haoma<<endl;
j=1;
break;
}
}
if(j==1)
show();
else
{
cout<<"not find."<<endl;
show();
}
return 0;
}
int print()
{
cout<<"the total contacts are:"<<endl;
for(int i=0;i<counter;++i)
cout<<user[i].xuhao+1<<" "<<user[i].xingming<<" "<<user[i].haoma<<endl;
cout<<"enter OK to go back."<<endl;
string answer;
cin>>answer;
if(answer=="OK"||answer=="ok")
show();
else
return 0;
}
int isFull(int a)
{
if(a==totalnumber)
{
cout<<"the number of contacts reachs the totalnumber."<<endl;
cout<<"do you want to print the total contacts?(Y/N)"<<endl;
string j;
cin>>j;
if(j=="Y"||j=="y")
print();
else
show();
}
return 0;
}
注:1.名字不要超过19个字符,2.第一个选择输入前边的序号(1或2或3或4)就可以,
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询