欢迎光临散文网 会员登陆 & 注册

黑马程序员匠心之作|C++教程从0到1入门编程,学习编程不再难

2021-12-18 19:51 作者:不会后空翻的初一生  | 我要投稿

//通讯录管理系统,已改进,可直接复制使用

#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.添加联系人  ██" << endl;

cout << " ██  ◎2.显示联系人  ██" << endl;

cout << " ██  ◎3.删除联系人  ██" << endl;

cout << " ██  ◎4.查找联系人  ██" << endl;

cout << " ██  ◎5.修改联系人  ██" << endl;

cout << " ██  ◎6.清空联系人  ██" << 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)

{

//存了这么多还想存,我在这祝你电脑内存条马上爆炸 

cout << "对不起人数已满,无法继续添加" << 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 << "当前通讯录人数为零,无法打印" << 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 << "结束" << endl;

system("pause");

system("cls");

}

void delete_Person(address_book *p)

{

string name;

cout << "请输入要删除联系人的姓名 >>>";

cin >> name;

int ret = is_Exist(p, name);

if (ret == -1)

{

cout << "未找到联系人" << 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 << "请输入要查找联系人的姓名 >>>";

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 << "未找到联系人" << endl;

}

system("pause");

system("cls");

}

void modify_Person(address_book *p)

{

string name;

cout << "请输入要修改联系人的姓名 >>>";

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 << "未找到联系人" << 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;

}

黑马程序员匠心之作|C++教程从0到1入门编程,学习编程不再难的评论 (共 条)

分享到微博请遵守国家法律