C++基础知识阶段小项目
使用C++基础知识,实现一个通讯录管理系统,分别实现添加联系人、显示联系人、删除联系人、查找联系人、修改联系人、清空联系人、退出系统等功能,比较简单,随意看看!
#include <iostream>
#define MAX 1000
using namespace std;
struct Person {
string p_Name;
string p_Sex;
int p_Age;
int p_Tel;
string p_Addr;
};
struct Addressbooks {
struct Person personArray[MAX];
int m_Size;
};
void showMenu(string arr[]) {
for (int i = 0; i < 9; ++i) {
if (i != 0 && i != 8) {
cout << "***** " << arr[i - 1] << " *****" << endl;
} else {
for (int j = 0; j < 26; ++j) {
cout << '*';
}
cout << endl;
}
}
}
void addPerson(Addressbooks *abs) {
if (abs->m_Size == MAX) {
cout << "通讯录已满!";
return;
} else {
string name;
cout << "请输入联系人姓名:" << endl;
cin >> name;
abs->personArray[abs->m_Size].p_Name = name;
string sex;
cout << "请输入联系人性别:" << endl;
cin >> sex;
abs->personArray[abs->m_Size].p_Sex = sex;
int age;
cout << "请输入联系人年龄:" << endl;
cin >> age;
abs->personArray[abs->m_Size].p_Age = age;
int tel;
cout << "请输入联系人电话:" << endl;
cin >> tel;
abs->personArray[abs->m_Size].p_Tel = tel;
string addr;
cout << "请输入联系人地址:" << endl;
cin >> addr;
abs->personArray[abs->m_Size].p_Addr = addr;
abs->m_Size++;
cout << "已经添加成功!" << endl;
// system("pause");
// system("cls");
}
}
void showPerson(Addressbooks *abs) {
if (abs->m_Size == 0) {
cout << "当前联系人为空,你可以选择添加联系人!" << endl;
} else {
for (int i = 0; i < abs->m_Size; ++i) {
cout << "姓名:" << abs->personArray[i].p_Name << '\t';
cout << "性别:" << abs->personArray[i].p_Sex << '\t';
cout << "年龄:" << abs->personArray[i].p_Age << '\t';
cout << "电话:" << abs->personArray[i].p_Tel << '\t';
cout << "地址:" << abs->personArray[i].p_Addr << endl;
}
}
// system("pause");
// system("cls");
}
void showSinglePerson(Addressbooks *abs, int cnt) {
cout << "姓名:" << abs->personArray[cnt].p_Name << '\t';
cout << "性别:" << abs->personArray[cnt].p_Sex << '\t';
cout << "年龄:" << abs->personArray[cnt].p_Age << '\t';
cout << "电话:" << abs->personArray[cnt].p_Tel << '\t';
cout << "地址:" << abs->personArray[cnt].p_Addr << endl;
}
int isExist(Addressbooks *abs, string name) {
for (int i = 0; i < abs->m_Size; i++) {
if (abs->personArray[i].p_Name == name) {
return i;
}
}
return -1;
}
void deletePerson(Addressbooks *abs, int cnt) {
if (cnt == -1) {
cout << "查无此人!" << endl;
return;
} else {
for (int i = cnt; i < abs->m_Size; i++) {
abs->personArray[i] = abs->personArray[i + 1];
}
}
// system("pause");
// system("cls");
}
void alterPerson(Addressbooks *abs, int cnt) {
cout << "输入你需要修改的信息:" << endl;
cout << "***** 1.修改姓名 *****" << endl;
cout << "***** 2.修改性别 *****" << endl;
cout << "***** 3.修改年龄 *****" << endl;
cout << "***** 4.修改电话 *****" << endl;
cout << "***** 5.修改地址 *****" << endl;
int order = 0;
cin >> order;
switch (order) {
case 1: {
string name;
cin >> name;
abs->personArray[cnt].p_Name = name;
break;
}
case 2: {
string sex;
cin >> sex;
abs->personArray[cnt].p_Sex = sex;
break;
}
case 3: {
int age;
cin >> age;
abs->personArray[cnt].p_Age = age;
break;
}
case 4: {
int tel;
cin >> tel;
abs->personArray[cnt].p_Tel = tel;
break;
}
case 5: {
string addr;
cin >> addr;
abs->personArray[cnt].p_Addr = addr;
break;
}
}
}
int main() {
string arrMenu[] = {"1.添加联系人", "2.显示联系人", "3.删除联系人", "4.查找联系人", "5.修改联系人", "6.清空联系人",
"0.退出通讯录"};
int select = 0;
bool judge = true;
Addressbooks abs;
abs.m_Size = 0;
while (judge) {
showMenu(arrMenu);
cin >> select;
switch (select) {
case 1:
addPerson(&abs);
break;
case 2:
showPerson(&abs);
break;
case 3: {
cout << "请输入删除联系人姓名:" << endl;
string name;
cin >> name;
int cnt = isExist(&abs, name);
deletePerson(&abs, cnt);
break;
}
case 4: {
cout << "请输入查找联系人姓名:" << endl;
string name;
cin >> name;
if (isExist(&abs, name) == -1) {
cout << "查无此人!" << endl;
} else {
showSinglePerson(&abs, isExist(&abs, name));
}
break;
}
case 5: {
cout << "请输入需要修改联系人姓名:" << endl;
string name;
cin >> name;
if (isExist(&abs, name) == -1) {
cout << "查无此人!" << endl;
} else {
alterPerson(&abs, isExist(&abs, name));
}
}
break;
case 6:
abs.m_Size = 0;
break;
case 0:
cout << "欢迎再次使用!" << endl;
judge = false;
break;
default:
cout << "请输入正确的数字:" << endl;
break;
}
}
}
标签:cnt,cout,管理系统,int,C++,abs,通讯录,personArray,Size
From: https://www.cnblogs.com/HanXiaoCao/p/16716918.html