1、系统需求
通讯录是一个可以记录亲人、好友信息的工具。本教程主要利用C++来实现一个通讯录管理系统系统中需要实现的功能如下:
●添加联系人:向通讯录中添加新人,信息包括(姓名、性别、年龄、联系电话、家庭住址)最多记录1000人
●显示联系人:显示通讯录中所有联系人信息
●删除联系人:按照姓名进行删除指定联系人
●查找联系人:按照姓名查看指定联系人信息
●修改联系人:按照姓名重新修改指定联系人
●清空联系人:清空通讯录中所有信息
.●退出通讯录:退出当前使用的通讯录
特别简单,只是步骤有点多,直接上代码
#include <iostream> #include <string> #define MAX 1000 using namespace std; struct person { string name; string sex; int age; string number; string Addr; }; struct tongxvnlu { struct person personArray[MAX]; int Size; }; void show() { cout << "*************************" << endl; cout << "***** 1、添加联系人 *****" << endl; cout << "***** 2、显示联系人 *****" << endl; cout << "***** 3、删除联系人 *****" << endl; cout << "***** 4、查找联系人 *****" << endl; cout << "***** 5、修改联系人 *****" << endl; cout << "***** 6、清空联系人 *****" << endl; cout << "***** 0、退出通讯录 *****" << endl; cout << "*************************" << endl; } void addperson(struct tongxvnlu *txl)//添加联系人 { if (txl->Size == MAX) { cout << "通讯录已满,无法添加" << endl; return; } else { string name, sex, Addr, number; int age; cout << "请输入姓名:" << endl; cin >> name; txl->personArray[txl->Size].name = name; cout << "请输入性别:" << endl; cin >> sex; txl->personArray[txl->Size].sex = sex; cout << "请输入年龄:" << endl; cin >> age; txl->personArray[txl->Size].age = age; cout << "请输入电话号码:" << endl; cin >> number; txl->personArray[txl->Size].number = number; cout << "请输入家庭地址:" << endl; cin >> Addr; txl->personArray[txl->Size].Addr = Addr; txl->Size++; } system("pause"); system("cls"); } void showperson(struct tongxvnlu *txl)//显示联系人 { if (txl->Size == 0) cout << "当前记录为空" << endl; else { for (int i = 0; i < txl->Size; i++) { cout << "姓名:" << txl->personArray[i].name << "\t"; cout << "性别:" << txl->personArray[i].sex << "\t"; cout << "年龄:" << txl->personArray[i].age << "\t"; cout << "电话:" << txl->personArray[i].number << "\t"; cout << "住址:" << txl->personArray[i].Addr << endl; } } system("pause"); system("cls"); } int isExist(struct tongxvnlu* txl, string name) { for (int i = 0; i < txl->Size; i++) { if (txl->personArray[i].name == name) { return i; } } return -1; } void deleteperson(struct tongxvnlu* txl) { string name; cout << "请输入要删除的联系人:" << endl; cin >> name; int ret = isExist(txl, name); if(ret==-1) { cout << "该联系人不存在!" << endl; } else { while (ret<txl->Size) { txl->personArray[ret] = txl->personArray[ret + 1]; ret++; } } txl->Size--; system("pause"); system("cls"); } void selectperson(struct tongxvnlu* txl) { string name; cout << "请输入要查找的联系人:" << endl; cin >> name; int ret = isExist(txl, name); if (ret == -1) { cout << "查无此人!" << endl; } else { cout << "姓名:" << txl->personArray[ret].name << "\t"; cout << "性别:" << txl->personArray[ret].sex << "\t"; cout << "年龄:" << txl->personArray[ret].age << "\t"; cout << "电话:" << txl->personArray[ret].number << "\t"; cout << "住址:" << txl->personArray[ret].Addr << endl; } system("pause"); system("cls"); } void modifyperson(tongxvnlu* txl) { string name, sex, Addr, number; int age; cout << "请输入要修改的联系人姓名;" << endl; cin >> name; int ret = isExist(txl, name); if(ret==-1) { cout << "查无此人!" << endl; } else { cout << "请输入姓名:" << endl; cin >> name; txl->personArray[ret].name = name; cout << "请输入性别:" << endl; cin >> sex; txl->personArray[ret].sex = sex; cout << "请输入年龄:" << endl; cin >> age; txl->personArray[ret].age = age; cout << "请输入电话号码:" << endl; cin >> number; txl->personArray[ret].number = number; cout << "请输入家庭地址:" << endl; cin >> Addr; txl->personArray[ret].Addr = Addr; } system("pause"); system("cls"); } void cleanperson(tongxvnlu* txl) { txl->Size = 0; cout << "通讯录已空" << endl; system("pause"); system("cls"); } int main() { tongxvnlu txl; txl.Size = 0; while (true) { show(); int select; cin >> select; switch (select) { case 1: addperson(&txl); break; case 2: showperson(&txl); break; case 3: deleteperson(&txl); break; case 4: selectperson(&txl); break; case 5: modifyperson(&txl); break; case 6: { cout << "是否要清空所有联系人:" << endl << "是请输入1 " << "否请输入2" << endl; int i; cin >> i; if (i == 2)exit(1); else cleanperson(&txl); } break; case 0: cout << "欢迎下次使用!" << endl; system("pause"); break; return 0; default: break; } } }代码参考自 黑马程序员
如有侵权,请与本人联系