代码主体
#include< string>
#include< fstream>
#include< iostream>
using namespace std;
int number = 0 ;
typedef struct News
{
int num;
string name;
int age;
string tel;
string address;
string hooby;
} News;
int Choose ( )
{
int choice;
cout << "***************手机通信录管理系统************" << endl;
cout << " 1:根据编号查询" << endl;
cout << " 2:修改联系人" << endl;
cout << " 3:添加联系人" << endl;
cout << " 4:删除联系人" << endl;
cout << " 5:显示全部联系人" << endl;
cout << " 6:退出" << endl;
cout << "**********************************************" << endl;
cout << "请输入选项(1-6):" ;
cin >> choice;
system ( "cls" ) ;
return choice;
}
int SearchByNum ( News * a)
{
int i, x;
int SearchNum;
cout << "请输入联系人编号" ;
cin >> SearchNum;
for ( i = 0 ; i < number; i++ )
{
if ( a[ i] . num == SearchNum)
{
cout << "编号:" << a[ i] . num << " " << "姓名:" << a[ i] . name << " " << "年龄:" << a[ i] . age << " " << "电话:" << a[ i] . tel << " " << "地址:" << a[ i] . address << " " << "爱好:" << a[ i] . hooby << endl;
cout<< "查找结束,按任意键结束!" ;
cin>> x;
system ( "cls" ) ;
return i;
}
}
cout << "抱歉,查无此人!" << endl;
cout<< "查找结束,按任意键结束!" ;
cin>> x;
system ( "cls" ) ;
return - 1 ;
}
void EditName ( News * a)
{
int i, x;
string newname, oldname;
cout << "原名字:" ;
cin >> oldname;
cout << "修改后的名字:" ;
cin >> newname;
for ( i = 0 ; i < number; i++ )
{
if ( a[ i] . name == oldname)
{
a[ i] . name= newname;
cout << "编号:" << a[ i] . num << " " << "姓名:" << a[ i] . name << " " << "年龄:" << a[ i] . age << " " << "电话:" << a[ i] . tel << " " << "地址:" << a[ i] . address << " " << "爱好:" << a[ i] . hooby << endl;
}
}
cout<< "修改完毕,按任意键结束!" ;
cin>> x;
system ( "cls" ) ;
}
void AddMembers ( News * a)
{
int x;
int i = number;
cout << "编号:" ;
cin >> a[ i] . num;
cout << "姓名:" ;
cin >> a[ i] . name;
cout << "年龄:" ;
cin >> a[ i] . age;
cout << "电话:" ;
cin >> a[ i] . tel;
cout << "地址:" ;
cin >> a[ i] . address;
cout << "爱好:" ;
cin >> a[ i] . hooby;
number++ ;
system ( "cls" ) ;
cout<< "信息已保存,按任意键结束!" ;
cin>> x;
system ( "cls" ) ;
}
void DeleteMember ( News * a)
{
int i, j, x;
i = SearchByNum ( a) ;
if ( i > 0 )
{
for ( j = i; j < number - 1 ; j++ )
{
a[ j] = a[ j + 1 ] ;
}
number-- ;
cout << "该联系人已成功被移出您的通信录,按任意键结束!" << endl;
cin>> x;
system ( "cls" ) ;
}
}
void DisplayAll ( News * a)
{
int x;
for ( int i = 0 ; i < number; i++ )
{
cout << "第" << i + 1 << "个人:" << endl;
cout << "编号:" << a[ i] . num << " " << "姓名:" << a[ i] . name << " " << "年龄:" << a[ i] . age << " " << "电话:" << a[ i] . tel << " " << "地址:" << a[ i] . address << " " << "爱好:" << a[ i] . hooby << endl;
}
cout << "展示完毕,按任意键结束!" << endl;
cin>> x;
system ( "cls" ) ;
}
int main ( )
{
int x= 0 ;
News a[ 50 ] ;
while ( x!= 6 )
{
x= Choose ( ) ;
switch ( x)
{
case 1 : SearchByNum ( a) ; break ;
case 2 : EditName ( a) ; break ;
case 3 : AddMembers ( a) ; break ;
case 4 : DeleteMember ( a) ; break ;
case 5 : DisplayAll ( a) ; break ;
default : { cout << "选项为1-6,请重输!" << endl; }
}
}
return 0 ;
}
可以增删改,翻到这篇的人,你是在为考试和作业苦恼吗?如果我帮到了你,记得赞一下哈~