#include#includeusing namespace std;
#define MAX 1000
//設計聯(lián)系人結構體
struct Person
{//姓名
string m_Name;
//性別
int m_Sex;
//年齡
int m_Age;
//電話
string m_Phone;
//住址
string m_Addr;
};
//設計通訊錄結構體
struct Addressbooks
{//通訊錄中保存的聯(lián)系人數(shù)組
Person personArray[MAX];
//通訊錄中當前記錄聯(lián)系人個數(shù)
int m_Size;
};
//菜單界面
void showMenu()
{cout<< "1、添加聯(lián)系人"<< endl;
cout<< "2、顯示聯(lián)系人"<< endl;
cout<< "3、刪除聯(lián)系人"<< endl;
cout<< "4、查找聯(lián)系人"<< endl;
cout<< "5、修改聯(lián)系人"<< endl;
cout<< "6、清空聯(lián)系人"<< endl;
cout<< "0、退出通訊錄"<< endl;
}
//所有功能
//1.添加聯(lián)系人
void addPerson(Addressbooks* abs)
{//判斷通訊錄是否已滿,滿了就不再添加
if (abs->m_Size == MAX)
{cout<< "通訊錄已滿,無法添加!"<< endl;
return;
}
else
{//添加具體聯(lián)系人
string name;
cout<< "請輸入姓名:";
cin >>name;
abs->personArray[abs->m_Size].m_Name = name;
int sex;
cout<< "請輸入性別:"< cin >>sex;
if (sex == 1 || sex == 2)
{ abs->personArray[abs->m_Size].m_Sex = sex;
break;
}
else
{ cout<< "請重新輸入"<>age;
abs->personArray[abs->m_Size].m_Age = age;
string phone;
cout<< "請輸入聯(lián)系電話:";
cin >>phone;
abs->personArray[abs->m_Size].m_Phone = phone;
string address;
cout<< "請輸入家庭住址:";
cin >>address;
abs->personArray[abs->m_Size].m_Addr = address;
abs->m_Size++;
cout<< "添加成功"<//判斷通訊錄中人數(shù)是否為0
if (abs->m_Size == 0)
{cout<< "當前記錄為空"<< endl;
}
else
{for (int i = 0; i< abs->m_Size; i++)
{ cout<< "姓名:"<< abs->personArray[i].m_Name<< " ";
cout<< "性別:"<< (abs->personArray[i].m_Sex==1?"男":"女")<< " ";
cout<< "年齡:"<< abs->personArray[i].m_Age<< " ";
cout<< "電話:"<< abs->personArray[i].m_Phone<< " ";
cout<< "住址:"<< abs->personArray[i].m_Addr<< endl;
}
}
system("pause");//按任意鍵繼續(xù)
system("cls");//清屏
}
//3.刪除聯(lián)系人
//3.1檢測聯(lián)系人是否存在
int isExist(Addressbooks *abs,string name)
{for (int i = 0; i< abs->m_Size; i++)
{if (abs->personArray[i].m_Name == name)
{ return i;//返回通訊錄中的數(shù)組下標
}
}
return -1;
}
//3.2刪除指定聯(lián)系人
void deletePerson(Addressbooks *abs)
{string name;
cout<< "請輸入您要刪除的聯(lián)系人"<< endl;
cin >>name;
int index = isExist(abs, name);//這里只傳abs的原因是abs就是代表了地址了
if(index==-1)
{cout<<"未查到此人"<for (int i = index; i< abs->m_Size - 1; i++)
{ //數(shù)據(jù)前移
abs->personArray[i] = abs->personArray[i + 1];
}
abs->m_Size--;
cout<< "刪除成功"<< endl;
}
system("pause");
system("cls");
}
//4.查找聯(lián)系人
void findPerson(Addressbooks* abs)
{string name;
cout<< "請輸入您要查找的聯(lián)系人"<< endl;
cin >>name;
int index = isExist(abs, name);
if ( index== -1)
{cout<< "找不到這個聯(lián)系人"<< endl;
}
else
{cout<< "姓名:"<< abs->personArray[index].m_Name<< " ";
cout<< "性別:"<< (abs->personArray[index].m_Sex == 1 ? "男" : "女")<< " ";
cout<< "年齡:"<< abs->personArray[index].m_Age<< " ";
cout<< "電話:"<< abs->personArray[index].m_Phone<< " ";
cout<< "住址:"<< abs->personArray[index].m_Addr<< endl;
}
system("pause");
system("cls");
}
//5.修改聯(lián)系人
void modifyPerson(Addressbooks* abs)
{string name;
cout<< "請輸入要修改的聯(lián)系人姓名"<< endl;
cin >>name;
int index = isExist(abs, name);
if (index == -1)
{cout<< "查無此人"<< endl;
}
else
{//修改
string name;
cout<< "請輸入姓名:";
cin >>name;
abs->personArray[index].m_Name = name;
int sex;
cout<< "請輸入性別:"<< endl;
cout<< "1---男"<< endl;
cout<< "2---女"<< endl;
while (true)
{ cin >>sex;
if (sex == 1 || sex == 2)
{ abs->personArray[index].m_Sex = sex;
break;
}
else
{ cout<< "請重新輸入"<< endl;
}
}
int age;
cout<< "請輸入年齡:";
cin >>age;
abs->personArray[index].m_Age = age;
string phone;
cout<< "請輸入聯(lián)系電話:";
cin >>phone;
abs->personArray[index].m_Phone = phone;
string address;
cout<< "請輸入家庭住址:";
cin >>address;
abs->personArray[index].m_Addr = address;
abs->m_Size= abs->m_Size;//只是修改,保存原來人數(shù)
cout<< "修改成功"<< endl;
}
system("pause");
system("cls");
}
//6.清空聯(lián)系人
void cleanPerson(Addressbooks* abs)
{int judge = 0;
cout<< "確認是否清空 1--是,2--否"<< endl;
cin >>judge;
if (judge == 2)
{cout<< "取消清空,返回菜單"<< endl;
}
else if(judge==1)
{abs->m_Size = 0; //邏輯清空
cout<< "通訊錄已清空"<< endl;
}
system("pause");
system("cls");
}
int main()
{//創(chuàng)建通訊里結構體變量
Addressbooks abs;
//初始化通訊錄中當前人員個數(shù)
abs.m_Size = 0;
int select = 0;
while (true)
{//菜單調(diào)用
showMenu();
cin >>select;
switch (select)
{case 1:
addPerson(&abs);
break;
case 2:
showPerson(&abs);
break;
case 3:
{ //case中加括號的原因是string name在switch語句中重復定義了,必須括起來變成局部變量
deletePerson(&abs);
break;
}
case 4:
findPerson(&abs);
break;
case 5:
modifyPerson(&abs);
break;
case 6:
cleanPerson(&abs);
break;
case 0:
cout<< "歡迎下次使用"<< endl;
system("pause");
return 0;
break;
default:
break;
}
}
system("pause");
return 0;
}
三、參考黑馬程序員視頻P72~P83你是否還在尋找穩(wěn)定的海外服務器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機房具備T級流量清洗系統(tǒng)配攻擊溯源,準確流量調(diào)度確保服務器高可用性,企業(yè)級服務器適合批量采購,新人活動首月15元起,快前往官網(wǎng)查看詳情吧
網(wǎng)站題目:C++通訊錄管理系統(tǒng)-創(chuàng)新互聯(lián)
文章鏈接:http://chinadenli.net/article24/ddioce.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供軟件開發(fā)、動態(tài)網(wǎng)站、網(wǎng)站改版、靜態(tài)網(wǎng)站、網(wǎng)站內(nèi)鏈、網(wǎng)頁設計公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容