#include <iostream> #include <string> #include <cstdio> using namespace std; //显示菜单 void showmenu(); //-------------------------------- int cnt = -1;//通讯录中的人数,一个人时为0 typedef struct People { int index; string name; string sex; int age; string number; string location; } people; people peo[1001]; //—————各功能模块—————————— void quit_system();//退出 void add_people(); void show_info(); void delete_people(); void find_people(); void edit_people(); void clear_sys(); int isExit(string name); //--------------------------------- int main() { int n; do{ showmenu(); cout << "输入选择的序号" << endl; cin >> n; switch (n) { case 1:add_people(); break;//添加 case 2:show_info(); break;//显示 case 3:delete_people(); break;//删除 case 4:find_people(); break;//查找 case 5:edit_people(); break;//修改 case 6:clear_sys(); break;//清空 case 0:quit_system(); return 0;//退出 default:cout << "不在范围内,请重新输入" << endl; break; } } while (n); system("pause"); return 0; } //显示菜单 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; cout << "**********************" << endl; } void quit_system() { } void add_people() { cnt++; peo[cnt].index = cnt + 1; cout << "请输入姓名:" << endl; cin >> peo[cnt].name; cout << "请输入性别:" << endl; cin >> peo[cnt].sex; cout << "请输入年龄:" << endl; cin >> peo[cnt].age; cout << "请输入电话号:" << endl; cin >> peo[cnt].number; cout << "请输入住址:" << endl; cin >> peo[cnt].location; } void show_info() { int len = cnt; if (len == -1) cout << "无信息" << endl; else { while (len != -1) { cout << "序号:" << peo[len].index; cout << " 姓名:" << peo[len].name; cout << " 性别:" << peo[len].sex; cout << " 年龄:" << peo[len].age; cout << " 电话:" << peo[len].number; cout << " 住址:" << peo[len].location << endl; len--; } } system("pause"); } void delete_people() { cout << "请输入要删除的联系人的姓名" << endl; string name; cin >> name; int ret = isExit(name); if (ret == -1) { cout << "查无此人" << endl; }else { for (int i = ret; i < cnt; i++) { peo[i] = peo[i + 1]; } cnt--; } } void find_people() { cout << "请输入要查找的联系人的姓名" << endl; string name; cin >> name; int ret = isExit(name); if (ret == -1) { cout << "查无此人" << endl; }else { cout << "序号:" << peo[ret].index; cout << " 姓名:" << peo[ret].name; cout << " 性别:" << peo[ret].sex; cout << " 年龄:" << peo[ret].age; cout << " 电话:" << peo[ret].number; cout << " 住址:" << peo[ret].location << endl; } system("pause"); } void edit_people() { cout << "输入要修改的联系人的姓名" << endl; string name; cin >> name; int ret = isExit(name); if (ret == -1) { cout << "查无此人" << endl; } else { cout << "旧的信息;序号:" << peo[ret].index; cout << " 姓名:" << peo[ret].name; cout << " 性别:" << peo[ret].sex; cout << " 年龄:" << peo[ret].age; cout << " 电话:" << peo[ret].number; cout << " 住址:" << peo[ret].location << endl; cout << "请输入性别:" << endl; cin >> peo[ret].sex; cout << "请输入年龄:" << endl; cin >> peo[ret].age; cout << "请输入电话号:" << endl; cin >> peo[ret].number; cout << "请输入住址:" << endl; cin >> peo[ret].location; cout << "新的信息为;序号:" << peo[ret].index; cout << " 姓名:" << peo[ret].name; cout << " 性别:" << peo[ret].sex; cout << " 年龄:" << peo[ret].age; cout << " 电话:" << peo[ret].number; cout << " 住址:" << peo[ret].location << endl; } system("pause"); } void clear_sys() { cnt = -1; cout << "已清空" << endl; system("pause"); } int isExit(string name) { int x = -1; for (int i = 0; i <= cnt; i++) { if (name == peo[i].name) return i;//找到返回下标 } return -1;//未找到 }
还不完美存在以下缺点
1、未对联系人信息作出限制,比如 年龄可以输入1111111111111而不报错
2、下一步可以用链表实现,这样在删除和清空时效率更高
3、清空只是在逻辑上清空,改成链表后可以真正清空。