通訊錄管理
創(chuàng)新互聯(lián)建站專業(yè)提供成都溫江機(jī)房服務(wù),為用戶提供五星數(shù)據(jù)中心、電信、雙線接入解決方案,用戶可自行在線購買成都溫江機(jī)房服務(wù),并享受7*24小時(shí)金牌售后服務(wù)。1.菜單面板的初始化
2.功能實(shí)現(xiàn)
?從簡單功能開始:①退出
?②添加聯(lián)系人:設(shè)計(jì)聯(lián)系人結(jié)構(gòu)體
??? ? ? ? ? ? 通訊錄存儲結(jié)構(gòu)(順序)
??? ? ? ? ? ? 封裝函數(shù)
?③顯示聯(lián)系人
?④查找聯(lián)系人:直接查找
?⑤刪除聯(lián)系人
?⑥修改聯(lián)系人
?⑦清空聯(lián)系人
3.運(yùn)行結(jié)果
#define MAX 10
#define INCREMENT 10
#include "iostream"
#include "string"
#include "stdlib.h"
using namespace std;
struct person
{
char name[20];//姓名
int sex;//性別:1男 2女
int age;//年齡
char phone[20]; //電話 //電話為11位超出了int范圍
char address[20];//住址
};
struct Addressbook
{
struct person* Ary;//聯(lián)系人數(shù)組指針
int nember;//聯(lián)系人人數(shù)
int booksize;//當(dāng)前容量
};
void createmenu();//菜單界面的實(shí)現(xiàn)
bool createbook(struct Addressbook& book);//初始化通訊錄
void add(struct Addressbook& book);//添加聯(lián)系人
void show(struct Addressbook sbook);//顯示聯(lián)系人
int search_person(struct Addressbook sbook,string sname);//在通訊錄中按照姓名關(guān)鍵字查找聯(lián)系人,查找到則返回相應(yīng)位置否則返回-1
void delete_person(struct Addressbook& book,string sname);//刪除通訊錄中的聯(lián)系人
void modify_person(struct Addressbook& book, string sname);//按名字查找聯(lián)系人,并選擇修改聯(lián)系人的一項(xiàng)信息
void clean(struct Addressbook& book);//清空聯(lián)系人
int main() {
createmenu();
Addressbook book;
createbook(book);
string sname;//被刪除聯(lián)系人姓名
do {
int opt = 0;
cout<< "請輸入功能號:"<< endl;
cin >>opt;
switch (opt)
{
case 1:
add(book);
break;
case 2:
show(book);
break;
case 3:
cout<< "請輸入所要刪除的聯(lián)系人姓名:";
cin >>sname;
delete_person(book, sname);
break;
case 4: {
cout<< "請輸入所要查找的聯(lián)系人姓名:";
cin >>sname;
if (search_person(book, sname) == -1)
{
cout<< "通訊錄內(nèi)無此聯(lián)系人!"<< endl;
}
else
{
cout<< "姓名:"<< book.Ary[search_person(book, sname)].name<< "\t";
cout<< "性別:"<< (book.Ary[search_person(book, sname)].sex == 1 ? "男" : "女")<< "\t";
cout<< "年齡:"<< book.Ary[search_person(book, sname)].age<< "\t";
cout<< "電話:"<< book.Ary[search_person(book, sname)].phone<< "\t";
cout<< "住址:"<< book.Ary[search_person(book, sname)].address<< endl;
}
break;
}
case 5: {
cout<< "請輸入所要修改聯(lián)系人的姓名:";
cin >>sname;
if (search_person(book, sname) == -1)
{
cout<< "通訊錄內(nèi)無此聯(lián)系人!"<< endl;
}
else
{
modify_person(book, sname);
}
createmenu();
break;
}
case 6:
clean(book);
createmenu();
break;
case 0: cout<< "感謝使用,歡迎下次使用!"<< endl;
system("pause");
return 0;
default:
cout<< "請輸入正確的功能號!"<< endl;
}
} while (true);
system("pause");
return 0;
}
void createmenu() {
cout<< "\n"<< endl;
cout<< "\n"<< endl;
cout<< "\t\t\t\t\t\t1、添加聯(lián)系人"<< endl;
cout<< "\t\t\t\t\t\t2、顯示聯(lián)系人"<< endl;
cout<< "\t\t\t\t\t\t3、刪除聯(lián)系人"<< endl;
cout<< "\t\t\t\t\t\t4、查找聯(lián)系人"<< endl;
cout<< "\t\t\t\t\t\t5、修改聯(lián)系人"<< endl;
cout<< "\t\t\t\t\t\t6、清空聯(lián)系人"<< endl;
cout<< "\t\t\t\t\t\t0、退出通訊錄"<< endl;
cout<< "\n"<< endl;
}
bool createbook(struct Addressbook& book)
{
book.Ary = (struct person*)malloc(MAX * sizeof(struct person));
if (! book.Ary)
{
cout<< "內(nèi)存分配失敗,請重啟程序!"<< endl;
exit(-1);
}
book.nember = 0;
book.booksize = MAX;
return true;
}
void add(struct Addressbook& book) {
if (book.nember == MAX)
{
book.Ary = (struct person*)realloc(book.Ary, (book.booksize + INCREMENT) * sizeof(struct person));
book.booksize += INCREMENT;
if (! book.Ary)
{
cout<< "內(nèi)存分配失敗,請重啟程序!"<< endl;
exit(-1);
}
}
struct person* p = &(book.Ary[book.nember]);
struct person st;
cout<< "請輸入聯(lián)系人姓名:"<< endl;
cin >>st.name;
strcpy_s(p->name,st.name);
cout<< "請輸入聯(lián)系人性別(輸入數(shù)字:1男 2女)"<< endl;
cin >>st.sex;
p->sex = st.sex;
if (!(st.sex == 1 || st.sex ==2))
{
cout<< "請輸入正確的數(shù)字:";
cin >>st.sex;
p->sex = st.sex;
}
cout<< "請輸入聯(lián)系人年齡:"<< endl;
cin >>st.age;
p->age = st.age;
cout<< "請輸入聯(lián)系人電話"<< endl;
cin >>st.phone;
strcpy_s(p->phone, st.phone);
cout<< "請輸入聯(lián)系人住址"<< endl;
cin >>st.address;
strcpy_s(p->address, st.address);
book.nember++;
cout<< "添加成功!"<< endl;
cout<< "\n"<< endl;
}
void show(struct Addressbook sbook) {
int i ;
if (sbook.nember == 0)
{
cout<< "通訊錄內(nèi)無聯(lián)系人!"<< endl;
return;
}
for ( i = 0; i< sbook.nember; i++)
{
cout<< "姓名:"<< sbook.Ary[i].name<< "\t";
cout<< "性別:"<< (sbook.Ary[i].sex == 1 ? "男":"女")<< "\t";
cout<< "年齡:"<< sbook.Ary[i].age<< "\t";
cout<< "電話:"<< sbook.Ary[i].phone<< "\t";
cout<< "住址:"<< sbook.Ary[i].address<< endl;
}
cout<< "已顯示所有聯(lián)系人信息!"<< endl;
system("pause");
system("cls");
createmenu();
}
int search_person(struct Addressbook sbook,string sname) {
int i;
for ( i = 0; i< sbook.nember; i++)
{
if (sbook.Ary[i].name == sname) {
return i;
}
}
return -1;
}
void delete_person(struct Addressbook& book, string sname) {
if (search_person(book,sname) == -1)
{
cout<< "通訊錄內(nèi)無此聯(lián)系人"<< endl;
}
else
{
int val = search_person(book, sname);//被刪除聯(lián)系人位置
struct person* p = &(book.Ary[val]);
for (int i = 1; i< (book.nember - val); i++)
{
++p;
*(p - 1) = *p;
}
--book.nember;
cout<< "刪除成功!"<< endl;
}
}
void modify_person(struct Addressbook& book, string sname) {
int sex = 0;
int age = 0;
char phone[20] ;
char address[20];
int i = 0;
int val = search_person(book, sname);
struct person* p = &(book.Ary[val]);
cout<< "\n"<< endl;
cout<< "\n"<< endl;
cout<< "\t\t\t\t\t\t1、修改聯(lián)系人性別"<< endl;
cout<< "\t\t\t\t\t\t2、修改聯(lián)系人年齡"<< endl;
cout<< "\t\t\t\t\t\t3、修改聯(lián)系人電話"<< endl;
cout<< "\t\t\t\t\t\t4、修改聯(lián)系人住址"<< endl;
cout<< "\n"<< endl;
cout<< "請輸入所要修改聯(lián)系人信息項(xiàng)前的功能號:"<< endl;
cin >>i;
switch (i)
{
case 1:
cout<< "請輸入聯(lián)系人修改后性別:" ;
cin >>sex;
p->sex = sex;
cout<< "修改成功!"<< endl;
break;
case 2:
cout<< "請輸入聯(lián)系人修改后年齡:";
cin >>age;
p->age = age;
cout<< "修改成功!"<< endl;
break;
case 3:
cout<< "請輸入聯(lián)系人修改后電話:";
cin >>phone;
strcpy_s(p->phone, phone);
cout<< "修改成功!"<< endl;
break;
case 4:
cout<< "請輸入聯(lián)系人修改后住址:";
cin >>address;
strcpy_s(p->address, address);
cout<< "修改成功!"<< endl;
default:
cout<< "請輸入正確的功能號:"<< endl;
modify_person(book,sname);
break;
}
system("pause");
system("cls");
}
void clean(struct Addressbook& book) {
int i = 0;
cout<< "是否確定清空通訊錄(1——是 2——否)"<< endl;
cin >>i;
if (i == 1)
{
book.nember = 0;//邏輯清空
}
cout<< "通訊錄已清空成功!"<< endl;
system("pause");
system("cls");
}
你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機(jī)房具備T級流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級服務(wù)器適合批量采購,新人活動首月15元起,快前往官網(wǎng)查看詳情吧