黑馬程序員匠心之作|C++教程從0到1入門編程,學(xué)習(xí)編程不再難

//通訊錄管理系統(tǒng),已改進(jìn),可直接復(fù)制使用
#include <iostream>
#include <string>
#include <windows.h>
#define MAX 1000
using namespace std;
string player_select = "";?
bool run = true;
struct person
{
string name;
string gender;
string age;
string telephone_number;
string address;
};
struct address_book
{
person person_list[MAX];
int len;
};
void loading()
{
cout << "Loading";
for(int i=0;i<5;i++){
cout << ".";
Sleep(150);
}
cout << "Done" << endl;
Sleep(500);
}
void showMenu()
{
system("cls");
cout << " ██████████████" << endl;
??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;
cout << " ██████████████" << endl;
}?
int is_Exist(address_book *p, string name)
{
for(int i=0;i<p->len;i++){
if(p->person_list[i].name == name){
return i;
}
}
return -1;
}
void add_Person(address_book * p)
{
if(p->len == MAX)
{
//存了這么多還想存,我在這祝你電腦內(nèi)存條馬上爆炸?
cout << "對不起人數(shù)已滿,無法繼續(xù)添加" << endl;
system("pause");
system("cls");?
}else{
int num = p->len;
//名字
string name;
do{
cout << "姓??名 >>>";
cin >> name;
if(is_Exist(p, name) != -1)
{
cout << "姓名已被占用" << endl;
}
}while(is_Exist(p, name) != -1);
p->person_list[num].name = name;
//性別
string gender;
cout << "性??別 >>>";
cin >> gender;
p->person_list[num].gender = gender;
//年齡
string age;
cout << "年??齡 >>>";
cin >> age;
p->person_list[num].age = age;
//電話號碼
string telephone_number;
cout << "電話號碼 >>>";
cin >> telephone_number;
p->person_list[num].telephone_number = telephone_number;
//地址
string address;
cout << "地??址 >>>";
cin >> address;
p->person_list[num].address = address;
//收尾
++p->len;
cout << "\n已成功存入" << endl;
system("pause");
system("cls");?
}
}
void show_Person(address_book *p)
{
if(p->len == 0)
{
cout << "當(dāng)前通訊錄人數(shù)為零,無法打印" << endl;
system("pause");
system("cls");
}else
{
for(int i=0;i< p->len;i++){
cout << "姓名:" << p->person_list[i].name << "\n電話號碼:" << p->person_list[i].telephone_number << "\n性別:" << p->person_list[i].gender << "\n年齡:" << p->person_list[i].age << "\n地址:" << p->person_list[i].address << endl;
cout << endl;
}
cout << "結(jié)束" << endl;
system("pause");
system("cls");
}?
}
void delete_Person(address_book *p)
{
string name;
cout << "請輸入要刪除聯(lián)系人的姓名 >>>";
cin >> name;
int ret = is_Exist(p, name);
if (ret == -1)
{
cout << "未找到聯(lián)系人" << endl;
}
else
{
for(int i=ret;i<p->len;++i)
{
p->person_list[i] = p->person_list[i+1];
}
--p->len;
cout << "刪除成功" << endl;
}
system("pause");
system("cls");
}
void find_Person(address_book *p)
{
string name;
cout << "請輸入要查找聯(lián)系人的姓名 >>>";
cin >> name;
int ret = is_Exist(p, name);
if(ret != -1)
{
cout << "姓名:" << p->person_list[ret].name << "\n電話號碼:" << p->person_list[ret].telephone_number << "\n性別:" << p->person_list[ret].gender << "\n年齡:" << p->person_list[ret].age << "\n地址:" << p->person_list[ret].address << endl;
}
else
{
cout << "未找到聯(lián)系人" << endl;
}
system("pause");
system("cls");
}
void modify_Person(address_book *p)
{
string name;
cout << "請輸入要修改聯(lián)系人的姓名 >>>";
cin >> name;
int ret = is_Exist(p, name);
if(ret != -1)
{
int num = ret;
//名字
string name;
cout << "姓??名 >>>";
cin >> name;
p->person_list[num].name = name;
//性別
string gender;
cout << "性??別 >>>";
cin >> gender;
p->person_list[num].gender = gender;
//年齡
string age;
cout << "年??齡 >>>";
cin >> age;
p->person_list[num].age = age;
//電話號碼
string telephone_number;
cout << "電話號碼 >>>";
cin >> telephone_number;
p->person_list[num].telephone_number = telephone_number;
//地址
string address;
cout << "地??址 >>>";
cin >> address;
p->person_list[num].address = address;
cout << "修改成功" << endl;
}
else
{
cout << "未找到聯(lián)系人" << endl;
}
system("pause");
system("cls");
}
void clean_Person(address_book *p){
address_book temp;
*p = temp;
p->len = 0;
cout << "已成功清空" << endl;
system("pause");
system("cls");
}
int main()
{
address_book abs;
abs.len = 0;
loading();
while(run)
{
showMenu();
cout << ">>>";
cin >> player_select;
if (player_select == "1")
{
add_Person(&abs);
continue;
}else if (player_select == "2")
{
show_Person(&abs);
continue;
}else if (player_select == "3")
{
delete_Person(&abs);
continue;
}else if (player_select == "4")
{
find_Person(&abs);
continue;
}else if (player_select == "5")
{
modify_Person(&abs);
continue;
}else if (player_select == "6")
{
clean_Person(&abs);
continue;
}else if (player_select == "0")
{
cout << "歡迎下次使用!" << endl;
run = false;
continue;
}else
{
cout << "請輸入正確的指令!" << endl;
system("pause");
system("cls");
continue;
}
}
system("pause");
return 0;
}