前言
雖然釋出這麼簡單的邏輯代碼有點羞恥,但是千裡之行,始于足下。記錄吾c++學習過程。
通信錄管理系統
就是簡單的對數組的添加,修改,删除等操作來模拟通訊錄管理系統。根據使用者輸入1、2、3、4、5、6、0進行互動。
主程式
執行檔案
#include <iostream>
#include <string>
#include "mail_list.h"
using namespace std;
int main() {
/*
1 添加聯系人
2 顯示聯系人
3 删除聯系人
4 查找聯系人
5 修改聯系人
6 清空聯系人
0 退出通訊錄
*/
show_menu();
bool flag = true;
struct Mail_list mailList;
while (flag){
int select = 0;
cout << "請輸入你的選擇!" << endl;
cin >> select;
switch (select) {
case 1:{
add_person(&mailList);
break;
}
case 2:{
show_mail_list(mailList);
break;
}
case 3:{
cout << "請輸入你要删除的聯系人姓名:" << endl;
string name;
cin >> name;
del_person(mailList, name);
break;
}
case 4:{
cout << "請輸入你要查找的人姓名:" << endl;
string name;
cin >> name;
find_person(mailList, name, true);
break;
}
case 5:{
cout << "請輸入要修改的聯系人姓名:" << endl;
string name;
cin >> name;
modify_person(mailList, name);
break;
}
case 6:{
clear_person(mailList);
break;
}
case 0:{
flag=false;
cout << "退出" << endl;
break;
}
default:{
flag= false;
cout << "please input right select value!, value range is 0~6" << endl;
break;
}
}
}
return 0;
}
頭檔案
定義函數聲明、類和結構體。
#include <iostream>
#include <string>
using namespace std;
#define MAX 100
struct Person{
string name;
string sex;
int age;
int telephone_number;
string address;
};
struct Mail_list{
struct Person person_array[MAX];
int m_size=0;
};
void show_menu();
void add_person(Mail_list * mailList);
void show_mail_list(Mail_list mailList);
int find_person(Mail_list mailList, string name, bool show= false);
void del_person(Mail_list & mailList, string name);
void modify_person(Mail_list & mailList, string name);
void clear_person(Mail_list & mailList);
源檔案
具體的函數實作
//
// Created by 安超 on 2021/8/12.
//
#include "mail_list.h"
#include <iostream>
#include <string>
using namespace std;
#define MAX 100
void show_menu(){
cout << "1 添加聯系人" << endl;
cout << "2 顯示聯系人" << endl;
cout << "3 删除聯系人" << endl;
cout << "4 查找聯系人" << endl;
cout << "5 修改聯系人" << endl;
cout << "6 清空聯系人" << endl;
cout << "0 退出通訊錄" << endl;
}
void add_person(Mail_list * mailList){
if (mailList->m_size == MAX){
cout << "通訊錄已滿,無法添加!" << endl;
return;
}
else{
string name;
string sex;
int age;
int telephone_number;
string address;
cout << "請輸入姓名:" << endl;
cin >> name;
cout << "請輸入性别:" << endl;
cin >> sex;
cout << "請輸入年齡:" << endl;
cin >> age;
cout << "請輸入電話号碼:" << endl;
cin >> telephone_number;
cout << "請輸入位址:" << endl;
cin >> address;
mailList->person_array[mailList->m_size].name=name;
mailList->person_array[mailList->m_size].sex=sex;
mailList->person_array[mailList->m_size].age=age;
mailList->person_array[mailList->m_size].telephone_number=telephone_number;
mailList->person_array[mailList->m_size].address=address;
mailList->m_size++;
cout << name <<"添加成功!" << endl;
}
}
void show_mail_list(Mail_list mailList){
if(mailList.m_size == 0){
cout << "通信錄為空!" << endl;
}
else{
for(int i=0; i<mailList.m_size; i++){
cout << "**************************************" << endl;
cout << "姓名:" << mailList.person_array[i].name << endl;
cout << "性别:" << mailList.person_array[i].sex << endl;
cout << "年齡:" << mailList.person_array[i].age << endl;
cout << "電話号碼:" << mailList.person_array[i].telephone_number << endl;
cout << "位址:" << mailList.person_array[i].address << endl;
cout << "**************************************" << endl;
}
}
}
int find_person(Mail_list mailList, string name, bool show){
if(mailList.m_size==0){
cout << "通信錄為空,您要找的人不存在!"<< endl;
return -1;
}
else{
for(int i=0; i<mailList.m_size; i++){
if(name==mailList.person_array[i].name){
cout << "查有此人!" << endl;
if(show){
cout << "查詢到的資訊:" << endl;
cout << "**************************************" << endl;
cout << "姓名:"<< mailList.person_array[i].name << endl;
cout << "性别:"<< mailList.person_array[i].sex << endl;
cout << "年齡:"<< mailList.person_array[i].age << endl;
cout << "電話号碼:"<< mailList.person_array[i].telephone_number << endl;
cout << "位址:"<< mailList.person_array[i].address << endl;
cout << "**************************************" << endl;
}
return i;
}
}
}
cout << "查無此人!" << endl;
return -1;
}
void del_person(Mail_list & mailList, string name){
int index = find_person(mailList, name);
if(index != -1){
for(int i=index; i<mailList.m_size; i++){
mailList.person_array[index] = mailList.person_array[index+1];
}
cout << "删除成功!" << endl;
mailList.m_size--;
}
}
void modify_person(Mail_list & mailList, string name){
int index = find_person(mailList, name);
if(index != -1){
cout << "請輸入修改後的姓名:" << endl;
string new_name;
cin >> new_name;
mailList.person_array[index].name = new_name;
cout << "請輸入修改後的性别:" << endl;
string new_sex;
cin >> new_sex;
mailList.person_array[index].sex = new_sex;
cout << "請輸入修改後的年齡:" << endl;
int new_age;
cin >> new_age;
mailList.person_array[index].age = new_age;
cout << "請輸入修改後的電話号:" << endl;
int new_tel;
cin >> new_tel;
mailList.person_array[index].telephone_number= new_tel;
cout << "請輸入修改後的住址:" << endl;
string new_address;
cin >> new_address;
mailList.person_array[index].address= new_address;
}
}
void clear_person(Mail_list & mailList){
mailList.m_size=0;
cout << "通訊錄已清空!"<< endl;
}
總結
寫這些邏輯雖然不費腦子,但是費體力,是以必須記錄,為了能學好c++,keep going!