概述
此项目适合c++初学者,针对前面c++基础知识,涉及到变量、结构体定义使用、数组定义使用、指针定义使用等。
c++实现通讯录管理系统(控制台版)
运行之后的结果如下:
代码:
#include <iostream> #include <string> using namespace std; #define MAX 1000//定义通讯录最大容量为1000 //联系人结构体 struct person { string name; int sex; int age; string address; }; //通讯录结构体 struct contact { struct person p[MAX]; int size; }; //操作菜单 void showMenu() { cout << "*********通讯录管理系统**********" << endl; cout << "********* 1.增加联系人 **********" << endl; cout << "********* 2.删除联系人 **********" << endl; cout << "********* 3.修改联系人 **********" << endl; cout << "********* 4.查询联系人 **********" << endl; cout << "********* 5.显示联系人 **********" << endl; cout << "********* 6.清空通讯录 **********" << endl; cout << "********* 0.退出通讯录 **********" << endl; } //封装查询联系人是否存在 int isExist(contact* con, string name) { for (int i = 0; i < con->size; i++) { if (con->p[i].name == name) { return i; } } return -1; } //添加联系人函数 void addPerson(contact *con){//传入数组的指针 if (con->size == MAX) { cout << "通讯录已满"<<endl; } else { cout << "输入姓名"; string name; cin >> name; cout << "输入年龄"; int age; cin >> age; cout << "输入性别1:女 2:男"; int sex; cin >> sex; cout << "输入地址"; string address; cin >> address; con->p[con->size].name = name; con->p[con->size].age = age; con->p[con->size].sex = sex; con->p[con->size].address = address; con->size++; } cout << "添加成功"<<endl; system("pause"); } //删除联系人函数 void deletecon(contact* con) { string name; cout << "输入要删除的姓名"; cin >> name; int res = isExist(con,name); if (res != -1) { for (int i = res; i < con->size; i++) { con->p[res] = con->p[res + 1]; } cout << "删除成功" << endl; con->size--; } else { cout << "查无此人"<<endl; } system("pause"); } //修改联系人函数 void updateContact(contact* con) { string name; cout << "输入要修改的联系人姓名查询" << endl; cin >> name; int res = isExist(con, name); if (res != -1) { cout << "姓名:" << con->p[res].name << "年龄:" << con->p[res].age << "性别:" << (con->p[res].sex == 1 ? "女" : "男") << "地址:" << con->p[res].address << endl; cout << "输入修改姓名"; string nametemp; cin >> nametemp; con->p[res].name = nametemp; cout << "输入修改年龄" << endl; int age; cin >> age; con->p[res].age = age; cout << "输入修改性别1:女2:男" << endl; int sex; cin >> sex; con->p[res].sex = sex; cout << "输入修改地址" << endl; string address; cin >> address; con->p[res].address = address; cout << "修改成功" << endl; } else { cout << "查无此人" << endl; } system("pause"); } //查询联系人函数 void search(contact *con) { string name; cout << "输入姓名查询"<<endl; cin >> name; int res = isExist(con, name); if (res != -1) { cout << "姓名:" << con->p[res].name << "年龄:" << con->p[res].age << "性别:" <<(con->p[res].sex==1?"女":"男")<<endl; } else { cout << "查无此人" << endl; } system("pause"); } //清空联系人函数 void clearContact(contact* con) { con->size = 0; cout << "已清空通讯录"<<endl; system("pause"); } //显示联系人函数 void showContact(contact* con) { if (con->size == 0) { cout << "暂无联系人"<<endl; } else { for (int i = 0; i < con->size; i++) { cout <<"姓名:"<< con->p[i].name <<"年龄:"<< con->p[i].age <<"地址:"<< con->p[i].address <<"性别:"<<(con->p[i].sex==1?"女":"男") << endl; } } system("pause"); } //退出系统函数 int exitapp() { exit(0); } /** 通讯录管理系统主程序 */ int main() { contact con; con.size = 0; while (true) { system("cls");//清屏 showMenu(); cout << "输入你的选择"; int select; cin >> select; switch (select) { case 1: addPerson(&con); break; case 2: deletecon(&con); break; case 3: updateContact(&con); break; case 4: search(&con); break; case 5: showContact(&con); break; case 6: clearContact(&con); break; case 0://退出 exitapp(); break; } } }