main.cpp
#include<iostream>
using namespace std;
#include"contact.h"
int main()
{
int select = 0;
addressBooks abs;
while (1)
{
showMenu();
cin >> select;
switch (select)
{
case 1:
addPerson(&abs);
break;
case 2:
showPerson(&abs);
break;
case 3:
deletePerson(&abs);
break;
case 4:
searchPerson(&abs);
break;
case 5:
modifyPerson(&abs);
break;
case 6:
clearPerson(&abs);
break;
case 0:
cout << "welcome back" << endl;
system("pause");
return 0;
break;
default:
break;
}
}
system("pause");
return 0;
}
contact.h
#pragma once
#define MAX 1000
struct person {
string m_name;
int m_gender;
int m_age;
string m_address;
string m_phone;
};
struct addressBooks {
struct person personArray[MAX];
int m_size = 0;
};
void showMenu();
void addPerson(addressBooks* addressBooks);
void showPerson(addressBooks* abs);
int isExist(addressBooks* abs, string name);
void deletePerson(addressBooks* abs);
void searchPerson(addressBooks* abs);
void modifyPerson(addressBooks* abs);
void clearPerson(addressBooks* abs);
contact.cpp
#include<iostream>
using namespace std;
#define MAX 1000
#include"contact.h"
void showMenu()
{
cout << "******************************************" << endl;
cout << "************ 1.add contact ************" << endl;
cout << "************ 2.show contact ************" << endl;
cout << "************ 3.delete contact ************" << endl;
cout << "************ 4.search contact ************" << endl;
cout << "************ 5.modify contact ************" << endl;
cout << "************ 6.clear contact ************" << endl;
cout << "************ 0.exit contact ************" << endl;
cout << "******************************************" << endl;
}
void addPerson(addressBooks* addressBooks)
{
int m_size = addressBooks->m_size;
if (addressBooks->m_size > MAX) {
cout << "address books is full" << endl;
}
else {
string name;
cout << "please enter your name:" << endl;
cin >> name;
addressBooks->personArray[m_size].m_name = name;
int gender;
cout << "please select your gender:" << endl;
cout << "1----male" << endl;
cout << "2----female" << endl;
while (1)
{
cin >> gender;
if (gender == 1 || gender == 2)
{
break;
}
else {
cout << "error, please select your gender again!" << endl;
}
}
addressBooks->personArray[m_size].m_gender = gender;
int age;
cout << "please enter your age:" << endl;
cin >> age;
addressBooks->personArray[m_size].m_age = age;
string address;
cout << "please enter your address:" << endl;
cin >> address;
addressBooks->personArray[m_size].m_address = address;
string phone;
cout << "please enter your phone:" << endl;
cin >> phone;
addressBooks->personArray[m_size].m_phone = phone;
cout << "add contact successfully" << endl;
addressBooks->m_size++;
system("pause");
system("cls");
}
}
void showPerson(addressBooks* abs)
{
if (abs->m_size == 0)
{
cout << "address books is empty" << endl;
}
else {
for (int i = 0; i < abs->m_size; i++)
{
cout << "c_id: " << i << "\t"
<< "name: " << abs->personArray[i].m_name << "\t"
<< "gender: " << (abs->personArray[i].m_gender == 1 ? "male" : "female") << "\t"
<< "age: " << abs->personArray[i].m_age << "\t"
<< "address:" << abs->personArray[i].m_address << "\t"
<< "phone:" << abs->personArray[i].m_phone << endl;
}
system("pause");
system("cls");
}
}
int isExist(addressBooks* abs, string name)
{
for (int i = 0; i < abs->m_size; i++)
{
if (abs->personArray[i].m_name == name)
{
return i;
}
}
return -1;
}
void deletePerson(addressBooks* abs)
{
string name;
cout << "please enter the name will you delete:" << endl;
cin >> name;
int ret = isExist(abs, name);
if (ret != -1)
{
for (int i = ret; i < abs->m_size; i++)
{
abs->personArray[i] = abs->personArray[i + 1];
}
cout << "delete contact " << name << " succussfully" << endl;
abs->m_size--;
}
else {
cout << "no such person" << endl;
}
system("pause");
system("cls");
}
void searchPerson(addressBooks* abs)
{
string name;
cout << "please enter the name will you search:" << endl;
cin >> name;
int ret = isExist(abs, name);
if (ret != -1)
{
int i = ret;
cout << "c_id: " << i << "\t"
<< "name: " << abs->personArray[i].m_name << "\t"
<< "gender: " << (abs->personArray[i].m_gender == 1 ? "male" : "female") << "\t"
<< "age: " << abs->personArray[i].m_age << "\t"
<< "address:" << abs->personArray[i].m_address << "\t"
<< "phone:" << abs->personArray[i].m_phone << endl;
}
else {
cout << "no such person" << endl;
}
system("pause");
system("cls");
}
void modifyPerson(addressBooks* abs)
{
string name;
cout << "please enter the name will you search:" << endl;
cin >> name;
int ret = isExist(abs, name);
if (ret != -1)
{
string name;
cout << "please enter your name:" << endl;
cin >> name;
abs->personArray[ret].m_name = name;
int gender;
cout << "please select your gender:" << endl;
cout << "1----male" << endl;
cout << "2----female" << endl;
while (1)
{
cin >> gender;
if (gender == 1 || gender == 2)
{
break;
}
else {
cout << "error, please select your gender again!" << endl;
}
}
abs->personArray[ret].m_gender = gender;
int age;
cout << "please enter your age:" << endl;
cin >> age;
abs->personArray[ret].m_age = age;
string address;
cout << "please enter your address:" << endl;
cin >> address;
if (address.length() > 1)
{
abs->personArray[ret].m_address = address;
}
string phone;
cout << "please enter your phone:" << endl;
cin >> phone;
if (phone.length() > 1)
{
abs->personArray[ret].m_phone = phone;
}
cout << "modify contact successfully" << endl;
system("pause");
system("cls");
}
else {
cout << "no such person" << endl;
}
}
void clearPerson(addressBooks* abs)
{
abs->m_size = 0;
cout << "clear contact successfully" << endl;
system("pause");
system("cls");
}