Home Article Practice C++01-联系人通讯录

C++01-联系人通讯录

2021-04-17 18:29  views:912  source:蔡徐坤    

#include<iostream>
#include<string>
#define MAX 1000 //电话薄最大容量
using namespace std;
//显示主菜单
void showMenus() {
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;
}
//联系人结构体
struct Contact
{
string name;
string phone;
int sex;//性别:1男 2女
int age;
string address;
};
//联系薄结构体
struct ContactBooks {
struct Contact contact[MAX];
int size;
};
//打印信息
void printContactInfo(ContactBooks *cb,int index) {
cout << "姓名:" << cb->contact[index].name << "\t";
cout << "性别:" << (cb->contact[index].sex == 1 ? "男" : "女") << "\t";
cout << "年龄:" << cb->contact[index].age << "\t";
cout << "电话:" << cb->contact[index].phone << "\t\t";
cout << "住址:" << cb->contact[index].address << endl;
}
void assembleInfo(ContactBooks *cb,int index) {
//输入用户名
cout << "请输入用户名:" << endl;
string name;
cin >> name;
cb->contact[index].name = name;
//输入电话
cout << "请输入电话:" << endl;
string phone;
cin >> phone;
cb->contact[index].phone = phone;
//输入年龄
cout << "请输入年龄:" << endl;
int age;
while (true)
{
cin >> age;
if (age > 0 && age < 150) {
cb->contact[index].age = age;
break;
}
cout << "输入有误,请重新输入" << endl;
}
//输入性别
cout << "请输入性别:" << endl;
cout << "1---男 2---女" << endl;
int sex = 0;
while (true)
{
cin >> sex;
if (sex == 1 || sex == 2) {
cb->contact[index].sex = sex;
break;
}
cout << "输入有误,请重新输入" << endl;
}
//输入住址
cout << "请输入住址:" << endl;
string address;
cin >> address;
cb->contact[index].address = address;
}
//添加联系人
void addContact(ContactBooks *cb) {
//判断联系薄是否已满
if(cb->size==MAX){
cout << "联系薄已满" << endl;
return;
}
//提示用户输入信息
assembleInfo(cb,cb->size);
cout << "添加成功" << endl;
cb->size++;
system("pause");
system("cls");
}
//显示所有练习人信息
void showConstact(ContactBooks *cb) {
if (cb->size == 0) {
cout << "通讯录里还没有联系人哦,马上去添加吧!" << endl;
return;
}
for (int i = 0; i < cb->size; i++)
{
printContactInfo(cb, i);
}
system("pause");
system("cls");
}
//根据姓名查找指定的联系人
int isExist(ContactBooks *cb,string name) {
for (int i = 0; i < cb->size; i++)
{
if (cb->contact[i].name==name)
{
return 0;
}
}
return -1;
}
//删除联系人
void deleteContact(ContactBooks *cb) {
//根据输入的姓名查找该联系人
cout << "请输入要删除的姓名" << endl;
string name;
cin >> name;
int index =isExist(cb, name);
if (index== -1) {
cout << "查无此人" << endl;
return;
}
//删除指定的联系人
for (int i = index; i < cb->size; i++)
{
cb->contact[i] = cb->contact[i + 1];
}
cb->size--;
cout << "删除成功!" << endl;
system("pause");
system("cls");
}
//查找联系人
void queryContact(ContactBooks *cb){
if (cb->size == 0) {
cout << "通讯录里还没有联系人哦,马上去添加吧!" << endl;
return;
}
cout << "请输入需要查找的联系人姓名!" << endl;
string name;
cin >> name;
int index = isExist(cb, name);
if (index==-1)
{
cout << "查无此人" << endl;
return;
}
printContactInfo(cb, index);
system("pause");
system("cls");
}
//修改联系人
void modifyContact(ContactBooks *cb) {
if (cb->size == 0) {
cout << "通讯录里还没有联系人哦,马上去添加吧!" << endl;
return;
}
cout << "请输入需要修改的联系人姓名!" << endl;
string name;
cin >> name;
int index = isExist(cb, name);
if (index == -1)
{
cout << "查无此人" << endl;
return;
}
//显示需要修改的信息
printContactInfo(cb, index);
//提示用户修改信息
assembleInfo(cb, index);
cout << "修改成功" << endl;
system("pause");
system("cls");
}
//清空联系人,只做逻辑删除
void clearAll(ContactBooks *cb){
cout << "你确定要清空所有联系人吗?请输入Y/N" << endl;
string flag;
while (true)
{
cin >> flag;
if (flag == "Y") {
cb->size=0 ;
cout << "联系人清空成功!" << endl;
break;
}
if (flag == "N") {
return;
}
cout << "输入有误,请重新输入" << endl;
}
system("pause");
}
int main() {
ContactBooks contactBooks;
contactBooks.size = 0;
//显示菜单
while (true)
{
showMenus();
int select = 0;
cin >> select;
switch (select)
{
case 1: //1、添加联系人
addContact(&contactBooks);
break;
case 2: //2、显示联系人
showConstact(&contactBooks);
break;
case 3: // 3、删除联系人
deleteContact(&contactBooks);
break;
case 4: //4、查找联系人
queryContact(&contactBooks);
break;
case 5: //5、修改联系人
modifyContact(&contactBooks);
break;
case 6: //6、清空联系人
clearAll(&contactBooks);
break;
case 0: //0、退出通讯录
cout << "退出成功,欢迎再次使用" << endl;
system("pause");
return 0;
break;
}
}
system("pause");
return 0;
}



Disclaimer: The above articles are added by users themselves and are only for typing and communication purposes. They do not represent the views of this website, and this website does not assume any legal responsibility. This statement is hereby made! If there is any infringement of your rights, please contact us promptly to delete it.

字符:    改为:
去打字就可以设置个性皮肤啦!(O ^ ~ ^ O)