C++实现通讯录管理系统
#include<iostream>
#include<string>
using namespace std;
#define MAX 1000
struct Contacts {
string name;
int Sex;
int Age;
string Phone;
string Address;
};
struct Addressbook {
Contacts personarray[MAX];
int Size;
};
void Addcontact(Addressbook* ab) {
if (ab->Size == MAX) {
cout << "Address book full." << endl;
return;
}
else {
cout << "Enter name" << endl;
cin >> ab->personarray[ab->Size].name;
cout << "Enter sex.1--man,2--woman" << endl;
int sex = 0;
while (1) {
cin >> sex;
if (sex == 1 || sex == 2) {
ab->personarray[ab->Size].Sex = sex;
break;
}
cout << "Reenter" << endl;
}
cout << "Enter age" << endl;
cin >> ab->personarray[ab->Size].Age;
cout << "Enter phone number" << endl;
cin >> ab->personarray[ab->Size].Phone;
cout << "Enter address" << endl;
cin >> ab->personarray[ab->Size].Address;
ab->Size++;
cout << "Added successfully" << endl;
system("pause");
system("cls");
}
}
void showPerson(Addressbook* ab) {
if (ab->Size == 0) {
cout << "It is currently empty" << endl;
}
else {
for (int i = 0; i < ab->Size; i++) {
cout << "Name: " << ab->personarray[i].name <<
" Sex: "<< (ab->personarray[i].Sex==1?"man": "woman")<<
" Age: " << ab->personarray[i].Age<<
" Phone number: " << ab->personarray[i].Phone <<
" Address: " << ab->personarray[i].Address << endl;
}
}
system("pause");
system("cls");
}
int isExist(Addressbook* ab,string Name) {
for (int i = 0; i < ab->Size; i++) {
if (ab->personarray[i].name == Name) {
return i;
}
}
return -1;
}
void deletePerson(Addressbook* ab) {
cout << "Please enter the contact you want to delete" << endl;
string name;
cin >> name;
int ret = isExist(ab, name);
if (ret == -1) {
cout << "There is no such person" << endl;
}
else {
for (int i = ret; i < ab->Size; i++) {
ab->personarray[ret] = ab->personarray[ret + 1];
ab->Size--;
cout << "Successfully deleted" << endl;
}
}
system("pause");
system("cls");
}
void findPerson(Addressbook* ab) {
string name;
cout << "Enter name" << endl;
cin >> name;
int ret = isExist(ab, name);
if (ret == -1) {
cout << "There is no such person" << endl;
}
else {
cout << "Name: " << ab->personarray[ret].name <<
" Sex: " << (ab->personarray[ret].Sex == 1 ? "man" : "woman") <<
" Age: " << ab->personarray[ret].Age <<
" Phone number: " << ab->personarray[ret].Phone <<
" Address: " << ab->personarray[ret].Address << endl;
}
system("pause");
system("cls");
}
void modifyPerson(Addressbook* ab) {
cout << "Enter name" << endl;
string name;
cin >> name;
int ret = isExist(ab, name);
if (ret == -1) {
cout << "There is no such man" << endl;
}
else {
cout << "Please change the name." << endl;
cin >> ab->personarray[ret].name;
cout << "Please change the sex,1--man,2--woman" << endl;
while (1) {
int sex;
cin >> sex;
if (sex == 1 || sex == 2) {
ab->personarray[ret].Sex = sex;
break;
}
else {
cout << "Please reenter" << endl;
}
}
cout << "Please reenter the age" << endl;
cin >> ab->personarray[ret].Age;
cout << "Please change the phonenumber" << endl;
cin >> ab->personarray[ret].Phone;
cout << "Please change the address" << endl;
cin >> ab->personarray[ret].Address;
cout << "Modified successfully" << endl;
}
system("pause");
system("cls");
}
void clearContacts(Addressbook* ab) {
ab->Size = 0;
cout << "Empty successfully" << endl;
system("pause");
system("cls");
}
void showMenu() {
cout << "1.Add a Contact." << endl;
cout << "2.Show Contacts." << endl;
cout << "3.Delete Contacts." << endl;
cout << "4.Find Contacts." << endl;
cout << "5.Modify Contacts." << endl;
cout << "6.Clear Contacts." << endl;
cout << "7.Exit address book." << endl;
}
int main() {
Addressbook ab1;
ab1.Size = 0;
int select = 0;
while (1) {
showMenu();
cin >> select;
switch (select) {
case 1://添加
Addcontact(&ab1);
break;
case 2://显示
showPerson(&ab1);
break;
case 3://删除
deletePerson(&ab1);
break;
case 4://查找
findPerson(&ab1);
break;
case 5://修改
modifyPerson(&ab1);
break;
case 6://清空
clearContacts(&ab1);
break;
case 7://退出
cout << "Welcom to use next time.";
return 0;
break;
}
}
system("pause");
return 0;
}