项目目标:
1、添加联系人 2、显示联系人 3、删除联系人
4、查找联系人 5、修改联系人 6、清空通讯录
自编头文件:
#pragma once
#include<iostream>
#include<string>
using namespace std;
#define MAX 1000
#define fm(a) (a)==1?"男":"女"
struct Person
{
string Name;
int sex;
int Age;
string Phone;
string Addr;
};
struct AddressBooks
{
Person Array[MAX];
int m_Size;
};
void showManu();
void Addperson(AddressBooks* abs);
void showPerson(AddressBooks* abs);
int isExist(AddressBooks* abs, string name);
void DeletePerson(AddressBooks* abs);
void findPerson(AddressBooks* abs);
void modifyPerson(AddressBooks* abs);
void clearBook(AddressBooks* abs);
头文件函数实现:
#include"AddressBooks.h"
void showManu()
{
cout << "****************" << endl;
cout << "1 Add a contact" << endl;
cout << "2 Show contacts" << endl;
cout << "3 Delete contact" << endl;
cout << "4 Search contact" << endl;
cout << "5 Modify contact" << endl;
cout << "6 Clear contacts" << endl;
cout << "****************" << endl;
return;
}
void Addperson(AddressBooks* abs)
{
if (abs->m_Size == MAX)
{
cout << "The AddressBook is full" << endl;
return;
}
else
{
string name;
cout << "Please enter the name" << endl;
cin >> name;
abs->Array[abs->m_Size].Name = name;
cout << "Please enter the sex" << endl;
cout << "1--male" << endl;
cout << "2--female" << endl;
int s = 0;
while (true)
{
cin >> s;
if (s == 1 || s == 2)
{
abs->Array[abs->m_Size].sex = s;
break;
}
cout << "Please re-enter the sex" << endl;
}
cout << "Please enter the age" << endl;
int a = 0;
cin >> a;
abs->Array[abs->m_Size].Age = a;
cout << "Please enter the Phone number" << endl;
string phone;
cin >> phone;
abs->Array[abs->m_Size].Phone = phone;
cout << "Please enter the Address" << endl;
string add;
cin >> add;
abs->Array[abs->m_Size].Addr = add;
abs->m_Size++;
cout << "Added successfully" << endl;
system("pause");//请按任意键继续
system("cls");//清屏
}
}
void showPerson(AddressBooks* abs)
{
if (abs->m_Size == 0)
{
cout << "The current record is empty" << endl;
}
else
{
for (int i = 0; i < abs->m_Size; i++)
{
cout << "姓名: " << abs->Array[i].Name << "\t";
cout << "性别: " << (fm(abs->Array[i].sex)) << "\t";
cout << "年龄: " << abs->Array[i].Age << "\t";
cout << "电话: " << abs->Array[i].Phone << "\t";
cout << "住址: " << abs->Array[i].Addr << endl;
}
}
system("pause");
system("cls");
}
int isExist(AddressBooks* abs, string name)
{
for (int i = 0; i < abs->m_Size; i++)
{
if (abs->Array[i].Name == name)return i;
}
return -1;
}
void DeletePerson(AddressBooks* abs)
{
cout << "Please enter the person you want to delete" << endl;
string name;
cin >> name;
int pos = isExist(abs, name);
if (pos != -1)
{
for (int i = pos; i < abs->m_Size - 1; i++)
{
abs->Array[i] = abs->Array[i + 1];
}
abs->m_Size -= 1;
cout << "Delete successfully" << endl;
}
else
{
cout << "Check to have no this person" << endl;
}
system("pause");
system("cls");
return;
}
void findPerson(AddressBooks* abs)
{
cout << "Please enter the contact you want to search" << endl;
string name;
cin >> name;
int pos = isExist(abs, name);
if (pos != -1)
{
cout << "姓名: " << abs->Array[pos].Name << "\t";
cout << "性别: " << (fm(abs->Array[pos].sex)) << "\t";
cout << "年龄: " << abs->Array[pos].Age << "\t";
cout << "电话: " << abs->Array[pos].Phone << "\t";
cout << "住址: " << abs->Array[pos].Addr << endl;
}
else
{
cout << "The contact is not exsit" << endl;
}
system("pause");
system("cls");
return;
}
void modifyPerson(AddressBooks* abs)
{
cout << "Please enter the contact you want to modify" << endl;
string name;
cin >> name;
int pos = isExist(abs, name);
if (pos != -1)
{
cout << "Please modify the name" << endl;
string na;
cin >> na;
abs->Array[pos].Name = na;
cout << "Please modify the sex" << endl;
cout << "1--男" << endl;
cout << "2--女" << endl;
int s;
cin >> s;
abs->Array[pos].sex = s;
cout << "Please modify the age" << endl;
int a;
cin >> a;
abs->Array[pos].Age = a;
cout << "Please modify the phone" << endl;
string phone;
cin >> phone;
abs->Array[pos].Phone = phone;
cout << "Please modify the Address" << endl;
string add;
cin >> add;
abs->Array[pos].Addr = add;
cout << "Modify successfully" << endl;
}
else
{
cout << "The contact is not exsit" << endl;
}
system("pause");
system("cls");
return;
}
void clearBook(AddressBooks* abs)
{
abs->m_Size = 0;
cout << "Address book cleared" << endl;
system("pause");
system("cls");
return;
}
主函数:
#include<iostream>
#include<cstdlib>
#include<string.h>
#include"AddressBooks.h"
using namespace std;
int main()
{
AddressBooks abs;
abs.m_Size = 0;
int select = 0;
while (true)
{
showManu();
cin >> select;
switch (select)
{
case 1:
Addperson(&abs);
break;
case 2:
showPerson(&abs);
break;
case 3:
DeletePerson(&abs);
break;
case 4:
findPerson(&abs);
break;
case 5:
modifyPerson(&abs);
break;
case 6:
clearBook(&abs);
break;
case 0:
cout << "Welcome to use it next time" << endl;
system("pause");
return 0;
break;
default:break;
}
}
return 0;
}