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

//這個(gè)有改一些,沒太多
#include<bits/stdc++.h>
#include<windows.h>
#define MAX 1000//最大人數(shù)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 << "請(qǐng)輸入姓名: ";
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輸入不正確,請(qǐng)重新輸入:";
}
}
cout << "\n年齡: ";
cin >> Age;
abs->persoArray[abs->m_Size].m_Age = Age;
cout << "\n電話號(hào)碼: ";
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 << "聯(lián)系人為空,請(qǐng)先添加聯(lián)系人";
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聯(lián)系電話:" << 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 << "輸入您要?jiǎng)h除的聯(lián)系人姓名:";
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 << "輸入聯(lián)系人姓名:";
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 << " 聯(lián)系電話:" << 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 << "請(qǐng)輸入要修改的聯(lián)系人姓名:";
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聯(liá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 << "確定要清空聯(lián)系人嗎";
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,添加聯(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 << "?-------------- \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;
}
}
}