#include#includeusing namespace std;
#define MAX 1000
//設(shè)計(jì)聯(lián)系人結(jié)構(gòu)體
struct Person
{//姓名
string m_Name;
//性別
int m_Sex;
//年齡
int m_Age;
//電話
string m_Phone;
//住址
string m_Addr;
};
//設(shè)計(jì)通訊錄結(jié)構(gòu)體
struct Addressbooks
{//通訊錄中保存的聯(lián)系人數(shù)組
Person personArray[MAX];
//通訊錄中當(dāng)前記錄聯(lián)系人個(gè)數(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<< "通訊錄已滿,無(wú)法添加!"<< endl;
return;
}
else
{//添加具體聯(lián)系人
string name;
cout<< "請(qǐng)輸入姓名:";
cin >>name;
abs->personArray[abs->m_Size].m_Name = name;
int sex;
cout<< "請(qǐng)輸入性別:"< cin >>sex;
if (sex == 1 || sex == 2)
{ abs->personArray[abs->m_Size].m_Sex = sex;
break;
}
else
{ cout<< "請(qǐng)重新輸入"<>age;
abs->personArray[abs->m_Size].m_Age = age;
string phone;
cout<< "請(qǐng)輸入聯(lián)系電話:";
cin >>phone;
abs->personArray[abs->m_Size].m_Phone = phone;
string address;
cout<< "請(qǐng)輸入家庭住址:";
cin >>address;
abs->personArray[abs->m_Size].m_Addr = address;
abs->m_Size++;
cout<< "添加成功"<//判斷通訊錄中人數(shù)是否為0
if (abs->m_Size == 0)
{cout<< "當(dāng)前記錄為空"<< 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檢測(cè)聯(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ù)組下標(biāo)
}
}
return -1;
}
//3.2刪除指定聯(lián)系人
void deletePerson(Addressbooks *abs)
{string name;
cout<< "請(qǐng)輸入您要?jiǎng)h除的聯(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<< "請(qǐng)輸入您要查找的聯(lián)系人"<< endl;
cin >>name;
int index = isExist(abs, name);
if ( index== -1)
{cout<< "找不到這個(gè)聯(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<< "請(qǐng)輸入要修改的聯(lián)系人姓名"<< endl;
cin >>name;
int index = isExist(abs, name);
if (index == -1)
{cout<< "查無(wú)此人"<< endl;
}
else
{//修改
string name;
cout<< "請(qǐng)輸入姓名:";
cin >>name;
abs->personArray[index].m_Name = name;
int sex;
cout<< "請(qǐng)輸入性別:"<< 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<< "請(qǐng)重新輸入"<< endl;
}
}
int age;
cout<< "請(qǐng)輸入年齡:";
cin >>age;
abs->personArray[index].m_Age = age;
string phone;
cout<< "請(qǐng)輸入聯(lián)系電話:";
cin >>phone;
abs->personArray[index].m_Phone = phone;
string address;
cout<< "請(qǐng)輸入家庭住址:";
cin >>address;
abs->personArray[index].m_Addr = address;
abs->m_Size= abs->m_Size;//只是修改,保存原來(lái)人數(shù)
cout<< "修改成功"<< endl;
}
system("pause");
system("cls");
}
//6.清空聯(lián)系人
void cleanPerson(Addressbooks* abs)
{int judge = 0;
cout<< "確認(rèn)是否清空 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)建通訊里結(jié)構(gòu)體變量
Addressbooks abs;
//初始化通訊錄中當(dāng)前人員個(gè)數(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中加括號(hào)的原因是string name在switch語(yǔ)句中重復(fù)定義了,必須括起來(lái)變成局部變量
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)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機(jī)房具備T級(jí)流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級(jí)服務(wù)器適合批量采購(gòu),新人活動(dòng)首月15元起,快前往官網(wǎng)查看詳情吧
讓客戶滿意是我們工作的目標(biāo),不斷超越客戶的期望值來(lái)自于我們對(duì)這個(gè)行業(yè)的熱愛(ài)。我們立志把好的技術(shù)通過(guò)有效、簡(jiǎn)單的方式提供給客戶,將通過(guò)不懈努力成為客戶在信息化領(lǐng)域值得信任、有價(jià)值的長(zhǎng)期合作伙伴,公司提供的服務(wù)項(xiàng)目有:空間域名、虛擬主機(jī)、營(yíng)銷軟件、網(wǎng)站建設(shè)、周寧網(wǎng)站維護(hù)、網(wǎng)站推廣。