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

//这个有改一些,没太多
#include<bits/stdc++.h>
#include<windows.h>
#define MAX 1000//最大人数1000
using namespace std;
struct person {
string m_Name;
int m_Sex;
int m_Age;
string m_Phone;//年龄
string m_Addr;//住址
};
struct Addressbooks {
struct person persoArray[MAX];
int m_Size = 0;
};
void addPerson(Addressbooks * abs) {
if (abs->m_Size == MAX) {
cout << "人已满\n";
return;
} else {
string Name;
int Sex;
int Age;
string Phone;//年龄
string Addr;//住址
cout << "请输入姓名: ";
cin >> Name;
abs->persoArray[abs->m_Size].m_Name = Name;
cout << "\n性别 1.♂xy /2.♀xx: ";
while (true) {
cin >> Sex;
if (Sex == 1 || Sex == 2) {
abs->persoArray[abs->m_Size].m_Sex = Sex;
break;
} else {
cout << "\n输入不正确,请重新输入:";
}
}
cout << "\n年龄: ";
cin >> Age;
abs->persoArray[abs->m_Size].m_Age = Age;
cout << "\n电话号码: ";
cin >> Phone;
abs->persoArray[abs->m_Size].m_Phone = Phone;
cout << "\n住址: ";
cin >> Addr;
abs->persoArray[abs->m_Size].m_Addr = Addr;
cout << "\nOK\n";
cout << "\n 回到界面中......";
Sleep(2000);
system("cls");
}
}
void showPerson(Addressbooks * abs) {
if (abs->m_Size == 0) {
cout << "联系人为空,请先添加联系人";
cout << "\n 回到界面中......";
Sleep(2000);
system("cls");
} else {
for (int i = 0; i < abs->m_Size; i++) {
cout << "姓名:" << abs->persoArray[i].m_Name;
cout << "\t性别:" << (abs->persoArray[i].m_Sex == 1 ? "男" : "女");
cout << "\t年龄:" << abs->persoArray[i].m_Age;
cout << "\t联系电话:" << abs->persoArray[i].m_Phone;
cout << "\t住址:" << abs->persoArray[i].m_Addr << endl;
}
cout << endl << " ";
system("pause");
system("cls");
}
}
int isExist(Addressbooks * abs, string Name) {
for (int i = 0; i < abs->m_Size; i++) {
if (Name == abs->persoArray[i].m_Name) {
return i;
}
}
return -1;
}
void deletePerson(Addressbooks * abs) {
cout << "输入您要删除的联系人姓名:";
string name;
cin >> name;
int ret = isExist(abs, name);
if (ret != -1) {
for (int i = ret; i < abs->m_Size; i++) {
abs->persoArray[i] = abs->persoArray[i + 1];
}
abs->m_Size--;
cout << "已删除";
cout << "\n 2s后回到界面中......";
Sleep(2000);
system("cls");
} else {
cout << "查无此人";
cout << "\n 2s后回到界面中......";
Sleep(2000);
system("cls");
}
}
void findperson(Addressbooks * abs) {
cout << "输入联系人姓名:";
string name;
cin >> name;
int ret = isExist(abs, name);
if (ret != -1) {
cout << "姓名:" << abs->persoArray[ret].m_Name << "\t";
cout << " 性别:" << (abs->persoArray[ret].m_Sex == 1 ? "男" : "女") << "\t";
cout << " 年龄:" << abs->persoArray[ret].m_Age << "\t";
cout << " 联系电话:" << abs->persoArray[ret].m_Phone << "\t";
cout << " 住址:" << abs->persoArray[ret].m_Addr << endl;
cout << "\n 2s后回到界面中......";
Sleep(2000);
system("cls");
} else {
cout << "(无)";
cout << "\n 2s后回到界面中......";
Sleep(2000);
system("cls");
}
}
void modifyPerson(Addressbooks * abs) {
cout << "请输入要修改的联系人姓名:";
string name;
cin >> name;
int ret = isExist(abs, name);
if (ret != -1) {
string name;
int sex;
int age;
string phone;
string addr;
cout << "修改的姓名:";
cin >> name;
abs->persoArray[ret].m_Name = name;
cout << "\n性别 1.♂xy /2.♀xx:";
cin >> sex;
abs->persoArray[ret].m_Sex = sex;
cout << "\n年龄:";
cin >> age;
abs->persoArray[ret].m_Age = age;
cout << "\n联系电话:";
cin >> phone;
abs->persoArray[ret].m_Phone = phone;
cout << "\n住址:";
cin >> addr;
abs->persoArray[ret].m_Addr = addr;
cout << "\n 2s后回到界面中......";
Sleep(2000);
system("cls");
} else {
cout << "查无此人\n";
cout << "\n 2s后回到界面中......";
Sleep(2000);
system("cls");
}
}
void cleanPerson(Addressbooks * abs) {
string aabbccdd;
cout << "确定要清空联系人吗";
cout << "\nyes.确定 no.不";
cin >> aabbccdd;
if (aabbccdd == "yes") {
abs->m_Size = 0;
cout << "已清空";
cout << "\n 2s后回到界面中......";
Sleep(2000);
system("cls");
} else {
cout << "\n好的\n 正在回到界面中......";
Sleep(1500);
system("cls");
}
}
void showMenu() {
cout << " --------------" << endl;
cout << " | 1,添加联系人 |" << endl;
cout << " | 2,显示联系人 |" << endl;
cout << " | 3,删除联系人 |" << endl;
cout << " | 4,查找联系人 |" << endl;
cout << " | 5,修改联系人 |" << endl;
cout << " | 6,清空联系人 |" << endl;
cout << " | 0,退出通讯录 |" << endl;
cout << " -------------- \n" << endl;
}
int main() {
Addressbooks abs;
abs.m_Size;
int select = 0;
while (true) {
showMenu();
cin >> select;
switch (select) {
case 1:
addPerson(&abs);
abs.m_Size++;
break;
case 2:
showPerson(&abs);
break;
case 3:
deletePerson(&abs);
break;
case 4:
findperson(&abs);
break;
case 5:
modifyPerson(&abs);
break;
case 6:
cleanPerson(&abs);
break;
case 0:
cout << "欢迎下次使用" << endl;
return 0;
break;
default:
break;
}
}
}