简单的实现,没有设计数据库和文件的读写,代码可以参考。
#include<iostream>
using namespace std;
#include<string>
#include <windows.h>
void showMenu();
void quitSystem();
//在屏幕中间显示
void CenterText(string &text) {
// 获取标准输出句柄
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
// 获取控制台屏幕缓冲区信息
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo(hConsole, &csbi);
// 计算中心位置
int consoleWidth = csbi.srWindow.Right - csbi.srWindow.Left + 1;
int consoleHeight = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
int textLength = text.length();
int xPosition = (consoleWidth - textLength) / 2;
int yPosition = consoleHeight / 100;
// 设置光标位置
COORD coord;
coord.X = xPosition;
coord.Y = yPosition;
SetConsoleCursorPosition(hConsole, coord);
cout << text << endl;
}
//创建联系人结构体
struct contactPerson {
string phonenem;
string name;
string sex;
string homeadd;
int age;
};
//进入界面
void interfaceSp() {
string text;
text = "* * * * * * * * * * * * * * * * 欢迎进入通讯录管理系统 * * * * * * * * * * * * * * * *\n";
CenterText(text);
//cout << "欢迎进入通讯录管理系统" << endl;
}
//输出系统功能选择
void output() {
cout << " * * * * * * * * * * * * 1、添加联系人 * * * * * * * * * * * * " << endl;
cout << " * * * * * * * * * * * * 2、显示联系人 * * * * * * * * * * * * " << endl;
cout << " * * * * * * * * * * * * 3、删除联系人 * * * * * * * * * * * * " << endl;
cout << " * * * * * * * * * * * * 4、查找联系人 * * * * * * * * * * * * " << endl;
cout << " * * * * * * * * * * * * 5、修改联系人 * * * * * * * * * * * * " << endl;
cout << " * * * * * * * * * * * 6、清空所有联系人 * * * * * * * * * * * " << endl;
cout << " * * * * * * * * * * * * 0、退出系统 * * * * * * * * * * * * * \n" << endl;
}
//添加联系人功能
void append(contactPerson *people,int len,int* p) {
int i = *p;
if (i + 1 > len) {
cout << "通讯录联系人已满,无法添加" << endl;
return;
}
else {
string text;
text = " * * * * * * * * * * 添加联系人 * * * * * * * * * * \n";
cout << text << endl;
cout << "请输入联系人姓名:,年龄:,性别:,电话号码:,家庭住址:,\n";
cin >> people[i].name >> people[i].age >> people[i].sex >> people[i].phonenem >> people[i].homeadd;
*p = *p + 1;
cout << "添加成功" << endl;
system("pause");
}
system("cls");//清屏操作
}
//显示联系人功能
void showContactP(contactPerson *people,int len, int *p) {
string text;
text = " * * * * * * * * * * 显示联系人 * * * * * * * * * * \n";
cout << text << endl;
cout << "全部的联系人" << endl;
if (*p == 0) {
cout << "通讯录为空!!!" << endl;
}
else {
for (int i = 0; i < *p; i++) {
cout << " " << people[i].name << " " << people[i].age << " " << people[i].sex << " " << people[i].phonenem << " " << people[i].homeadd << endl;
}
}
system("pause");
system("cls");//清屏操作
}
//菜单界面
void showMenu(){
output();
//system("pause");
//system("cls");//清屏操作
}
//删除联系人
void delectContactP(contactPerson* people, int len, int* p) {
string text;
text = " * * * * * * * * * * 删除联系人 * * * * * * * * * * \n";
cout << text << endl;
cout << "请选择要删除的联系人" << endl;
string delectname;
cin >> delectname;
bool found = false;
for (int i = 0; i < *p; i++) {
if (delectname == people[i].name) {
for (int j = i; j < *p; j++) {
people[j] = people[j + 1];
}
*p = *p - 1;
cout << "删除成功!!!" << endl;
found = true;
break;
}
}
if (found==false) {
cout << "查无此人!!!" << endl;
}
system("pause");
system("cls");//清屏操作
}
//查找联系人
void indexContactP(contactPerson* people, int len,int *p) {
string text;
text = " * * * * * * * * * * 查找联系人 * * * * * * * * * * \n";
cout << text << endl;
bool found = false;
cout << "输入要查找的联系人姓名:" << endl;
string indexname;
cin >> indexname;
for (int i = 0; i < *p; i++) {
if (people[i].name == indexname) {
cout << people[i].name << " " << people[i].age << " " << people[i].sex << " " << people[i].phonenem << " " << people[i].homeadd << endl;
}
found = true;
break;
}
if (found == false) {
cout << "没有该联系人" << endl;
}
system("pause");
system("cls");
}
//修改联系人
void editContactP(contactPerson* people, int len,int *p) {
string text;
text = " * * * * * * * * * * 修改联系人 * * * * * * * * * * \n";
cout << text << endl;
bool found = false;
cout << "输入要修改的联系人姓名:" << endl;
string indexname;
cin >> indexname;
for (int i = 0; i < *p; i++) {
if (people[i].name == indexname) {
cout << people[i].name << " " << people[i].age << " " << people[i].sex << " " << people[i].phonenem << " " << people[i].homeadd << endl;
cout << "输入要修改的信息:" << endl;
cout << "姓名:";
cin >> people[i].name;
cout << "\n";
cout << "年龄:";
cin >> people[i].age;
cout << "\n";
cout << "性别:";
cin >> people[i].sex;
cout << "\n";
cout << "电话:";
cin >> people[i].phonenem;
cout << "\n";
cout << "家庭住址:";
cin >> people[i].homeadd;
cout << "\n";
//cout << "修改成功" << endl;
cout << people[i].name << " " << people[i].age << " " << people[i].sex << " " << people[i].phonenem << " " << people[i].homeadd << endl;
cout << "修改成功" << endl;
}
found = true;
break;
}
if (found == false) {
cout << "没有该联系人" << endl;
}
system("pause");
system("cls");
}
//清空所有联系人
void clearContactP(contactPerson *people,int len, int* p) {
string text;
text = " * * * * * * * * * * 清空所有联系人 * * * * * * * * * * \n";
cout << text << endl;
* p = 0;
cout << "清空完成" << endl;
system("pause");
system("cls");
}
//退出系统功能
void quitSystem() {
string text;
text = " * * * * * * * * * * 欢迎下次使用 * * * * * * * * * * \n";
cout << text << endl;
exit(0);
}
//主函数
int main() {
//contactPerson people[1000];
//int len = sizeof(people) / sizeof(people[0]);
//append(people, len);
interfaceSp();
flag:
char s;
cout << "是否继续:(Y/N)" << endl;
cin >> s;
if (s == 'Y' || s == 'y') {
cout << "进入系统进行操作,按照提示选择操作\n" << endl;
}
else if (s == 'N' || s == 'n') {
return 0;
//exit(0);
}
else {
cout << "请输入正确的内容!!!" << endl;
goto flag;
}
contactPerson people[1000];
int len = sizeof(people) / sizeof(people[0]);
int sum = 0;
int *p = ∑
//cin >> select;
while (true) {
cout << " * * * * * * * * * * 请选择你所需要进行的功能 * * * * * * * * * * " << endl;
showMenu();
int select;
cout << "请输入你的选择: ";
cin >> select;
if (select == 1) {
append(people, len, p);
}
else if (select == 2) {
showContactP(people, len,p);
}
else if (select == 3) {
delectContactP(people,len, p);
}
else if (select == 4) {
indexContactP(people,len,p);
}
else if (select == 5) {
editContactP(people,len,p);
}
else if (select == 6) {
clearContactP(people,len, p);
}
else if (select == 0) {
quitSystem();
}
else {
cout << "违法输入,请输入正确的内容!!!" << endl;
}
}
system("pause");
return 0;
}