C++基础Day09.通讯录管理系统

2021/11/13 11:14:30

本文主要是介绍C++基础Day09.通讯录管理系统,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

Day09.2021.11.13

案例:通讯录管理系统

image-20211110161540570

样图

image-20211110161707634

 

对于上次的增加和显示做了一定程度上的优化,如输入用1/2代替男/女,输入的不是则重新输入;增加了操作成功后按任意键清屏;在函数中定义形参变量loc,而不是重复执行isExist方法分配通讯录内存;

 

#include <iostream>
 using namespace std;
 #define Max 1000//定义Max,以便后续修改数组大小
 ​
 struct Person
 {
     string name;
     int sex;//1男 2女
     int age;
     long long number;
     string address;
 };
 ​
 struct addressBook
 {
     Person arrays[Max];
     int size;
 };
 //增加
 void insert(addressBook* ads) {
     
     //因为需要确实的加一个人进去,所以需要用地址传递(值传递只改形参)
     if (ads->size == Max) {
         cout << "通讯录满" << endl;
         return;
     }
     else {
         cout << "请输入新联系人的姓名:";
         cin >> ads->arrays[ads->size].name;
         cout << endl;
         int sex;
         while (true) {
             cout << "请输入新联系人的性别(1男 2女):";
             cin >> sex;
             if (sex == 1 || sex == 2) {
                 ads->arrays[ads->size].sex = sex;
                 break;
             }
             else {
                 cout<< "请输入数字1或2其中之一" << endl;
             }
             cout << endl;
         }
         
         cout << "请输入新联系人的年龄:";
         cin >> ads->arrays[ads->size].age;
         cout << endl;
         cout << "请输入新联系人的电话:";
         cin >> ads->arrays[ads->size].number;
         cout << endl;
         cout << "请输入新联系人的地址:";
         cin >> ads->arrays[ads->size].address;
         cout << endl;
         cout << "添加成功" << endl;
         ads->size++;
         system("pause");
         system("cls");//清屏
         
     }
 ​
 }
 //显示
 void select(addressBook* ads) {
     if (ads->size == 0) {
         cout << "通讯录为空" << endl;
         system("pause");
         system("cls");
     }
     else {
         cout << "姓名  性别  年龄  电话号码  家庭住址";
         cout << endl;
         for (int i = 0; i < ads->size; i++) {
             cout << ads->arrays[i].name << "    " << ((ads->arrays[i].sex==1)?"男" :"女") << "    " << ads->arrays[i].age << "    " << ads->arrays[i].number << "      " << ads->arrays[i].address << endl;
         }
         system("pause");
         system("cls");
     }
     
 }
 //检测查找的人是否存在。是就返回位置。不是则返回-1
 int isExist(addressBook* ads,string name) {
     for (int i = 0; i < ads->size; i++) {
         if (ads->arrays[i].name == name)
             return i;
     }
     return -1;
 }
 //删除操作用后面的数据覆盖待删除的数据,然后size--
 void del(addressBook* ads) {
     string delName;
 int loc;//查找联系人返回的位置 
​ 
if (ads->size == 0) { 
cout << "通讯录为空" << endl; 
system("pause"); 
system("cls");//清屏 
    } 
else { 
cout << "请输入待删除联系人的姓名:"; 
cin >> delName; 
loc = isExist(ads, delName); 
if (loc == -1) { 
cout << endl << "查无此人" << endl; 
system("pause"); 
system("cls");//清屏 
    } 
else { 
for (int i = loc; i <= ads->size; i++) { 
ads->arrays[i] = ads->arrays[i + 1]; 
    } 
ads->size--; 
cout << "删除成功" << endl; 
system("pause"); 
system("cls");//清屏 
    } 
    } 
} 
//修改 
void update(addressBook* ads) { 
string oldName; 
string newName; 
int newSex; 
int newAge; 
long long newNumber; 
string newAddress; 
int loc;//查找联系人返回的位置 
​ 
if (ads->size == 0) { 
cout << "通讯录为空" << endl; 
system("pause"); 
system("cls");//清屏 
    } 
else { 
cout << "请输入待修改联系人的姓名:"; 
cin >> oldName; 
loc = isExist(ads, oldName); 
if (loc == -1) { 
cout << endl << "查无此人" << endl; 
system("pause"); 
system("cls");//清屏 
    } 
else { 
/*最开始我没定义变量loc,直接把中括号所有内容都写isExist(ads, oldName), 
每一条语句都会执行一次isExist函数,不断分配内存可能会造成内存溢出(个人理解有待查证), 
报错:写入位置造成访问冲突*/ 
cout << "请输入修改后的姓名:"; 
cin >> newName; 
ads->arrays[loc].name = newName; 
cout << endl; 
​ 
cout << "请输入修改后的性别(1男 2女):"; 
while (true) { 
cin >> newSex; 
if (newSex == 1 || newSex == 2) { 
ads->arrays[loc].sex = newSex; 
break; 
    } 
cout << "请输入数字1或2其中之一" << endl; 
    } 
     
     
cout << endl; 
​ 
cout << "请输入修改后的年龄:"; 
cin >> newAge; 
ads->arrays[loc].age = newAge; 
cout << endl; 
​ 
cout << "请输入修改后的电话:"; 
cin >> newNumber; 
ads->arrays[loc].number = newNumber; 
cout << endl; 
​ 
cout << "请输入修改后的地址:"; 
cin >> newAddress; 
ads->arrays[loc].address = newAddress; 
cout << endl; 
​ 
cout << "修改成功,修改后的数据为:" << endl; 
cout <<"姓名:"<< ads->arrays[loc].name << "    " <<"性别:" << ((ads->arrays[loc].sex == 1) ? "男" : "女") << "    " <<"年龄:" << ads->arrays[loc].age << "    " <<"电话号码:" << ads->arrays[loc].number << "      " <<"家庭住址:" << ads->arrays[loc].address << endl; 
system("pause"); 
system("cls");//清屏 
    } 
    } 
} 
//搜索 
void search(addressBook* ads) { 
string delName; 
int loc;//查找联系人返回的位置 
​ 
if (ads->size == 0) { 
cout << "通讯录为空" << endl; 
system("pause"); 
system("cls");//清屏 
    } 
else { 
cout << "请输入待查找联系人的姓名:"; 
cin >> delName; 
loc = isExist(ads, delName); 
if (loc == -1) { 
cout << endl << "查无此人" << endl; 
system("pause"); 
system("cls");//清屏 
    } 
else { 
cout << "姓名:" << ads->arrays[loc].name << "    " << "性别:" << ((ads->arrays[loc].sex == 1) ? "男" : "女") << "    " << "年龄:" << ads->arrays[loc].age << "    " << "电话号码:" << ads->arrays[loc].number << "      " << "家庭住址:" << ads->arrays[loc].address << endl; 
system("pause"); 
system("cls");//清屏 
    } 
    } 
} 
//清空:把size置0,因为显示的是从0-size 
void destory(addressBook* ads) { 
ads->size = 0; 
cout << "通讯录已清空"; 
system("pause"); 
system("cls");//清屏 
} 
​ 
int main() { 
​ 
addressBook ads; 
ads.size = 0; 
string name1; 
while(true){ 
cout << "Welcome to Addressbook Management System" << endl; 
cout << "1. 新建联系人" << endl; 
cout << "2. 显示联系人" << endl; 
cout << "3. 删除联系人" << endl; 
cout << "4. 修改联系人" << endl; 
cout << "5. 搜索联系人" << endl; 
cout << "6. 清空联系人" << endl; 
cout << "0. 退出系统" << endl; 
cout << "请输入您的选择" << endl; 
int a; 
cin >> a; 
switch (a) { 
case 1:insert(&ads); break; 
case 2:select(&ads); break; 
case 3:del(&ads);break; 
case 4:update(&ads); break; 
case 5:search(&ads); break; 
case 6:destory(&ads); break; 
case 0: { 
cout << "欢迎下次使用" << endl; 
return 0;//表示正常退出程序 
break; } 
default:break; 
    } 
    } 
system("pause"); 
return 0; 
}

成品

image-20211113093956608

 来源:b站黑马



这篇关于C++基础Day09.通讯录管理系统的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程