共计 4241 个字符,预计需要花费 11 分钟才能阅读完成。
9 通讯录管理系统
咱们要用之前学的所有常识做一个通讯录管理系统。须要实现上面几个性能:
- 增加通讯录
- 显示通讯录
- 删除通讯录
- 查找通讯录
- 批改通讯录
- 清空通讯录
- 退出通讯录
我决定分两层来写,一层 Manager 用来治理和主界面交互的性能,另一层 AddressBook 用来管制底层的存储逻辑。
底层的头文件和源文件如下:
//AddressBook.h
#pragma once
#include<iostream>
#include<string>
#define MAX 1000
using namespace std;
struct Node {
string name;
bool gender;
int year;
string phone;
string address;
};
struct Book {Node nodes[MAX]{};
int size = 0;
};
void addBook(Book&, string, bool, int, string, string);
void delByNum(Book&, int);
void getBook(const Book&);
void findBookByName(const Book&, string);
void setBookByName(Book&, string, bool, int, string, string);
void delAll(Book&);
这个头文件定义了一个 node 构造,负责存储每一个具体的人的信息。又定义了一个 Book 构造,存储了一个 node 数组用来存储整个通讯录,并且存储了一个 size 变量用于存储通讯录中有多少集体的信息。并且咱们定义了减少、删除、显示、查找、设置、清空这六项性能。
咱们在源文件中实现了这些性能:
//AddressBook.cpp
#include"AddressBook.h"
void addBook(Book& book, string name, bool gender, int year, string phone, string address) {book.nodes[book.size] = {name,gender,year,phone,address};
book.size++;
}
void delByNum(Book& book, int num) {for (int i = num - 1; i < book.size - 1; i++) {book.nodes[i] = book.nodes[i + 1];
}
book.size--;
}
void getBook(const Book& book) {for (int i = 0; i < book.size; i++) {
cout << '|' << i + 1 << '\t';
cout << '|' << book.nodes[i].name << '\t'
<< '|';
if (book.nodes[i].gender) {cout << "Male";}
else {cout << "Female";}
cout << '\t'
<< '|' << book.nodes[i].year << '\t'
<< '|' << book.nodes[i].phone << '\t'
<< '|' << book.nodes[i].address << '\t' << '|' << endl;
}
}
void findBookByName(const Book& book, string name) {for (int i = 0; i < book.size; i++) {if (book.nodes[i].name == name) {
cout << '|' << i + 1 << '\t';
cout << '|' << book.nodes[i].name << '\t'
<< '|';
if (book.nodes[i].gender) {cout << "Male";}
else {cout << "Female";}
cout << '\t'
<< '|' << book.nodes[i].year << '\t'
<< '|' << book.nodes[i].phone << '\t'
<< '|' << book.nodes[i].address << '\t' << '|' << endl;
goto FLAG;
}
}
cout << "no founding." << endl;
FLAG:
cout << endl;
}
void setBookByName(Book& book, string name, bool gender, int year, string phone, string address) {for (int i = 0; i < book.size; i++) {if (book.nodes[i].name == name) {book.nodes[i].gender = gender;
book.nodes[i].year = year;
book.nodes[i].phone = phone;
book.nodes[i].address = address;
goto FLAG;
}
}
cout << "no founding." << endl;
FLAG:
cout << endl;
}
void delAll(Book& book) {book.size = 0;}
然而这些性能只是负责底层交互的,然而这些函数都须要很多的参数,调用的时候如果还要写很多的语句来补充参数代码看起来就很简单。所以咱们须要再封装一层,把取得参数的这些语句也封装起来。
咱们抉择把这些性能再放在 Manager 层。
//Manager.h
#pragma once
#include"AddressBook.h"
void mAddBook(Book& book);
void mDelByNum(Book& book);
void mGetBook(const Book& book);
void mFindBookByName(const Book& book);
void mSetBookByName(Book& book);
void mDelAll(Book& book);
咱们同样在源文件中把这些性能实现:
//Manager.cpp
#include"Manager.h"
void mAddBook(Book& book) {
cout << "请输出姓名:";
string name;
cin >> name;
cout << "请输出性别(男 1,女 0):";
bool gender;
cin >> gender;
cout << "请输出年龄:";
int year;
cin >> year;
cout << "请输出电话号码:";
string phone;
cin >> phone;
cout << "请输出地址:";
string address;
cin >> address;
addBook(book, name, gender, year, phone, address);
cout << "增加胜利!" << endl;
}
void mDelByNum(Book& book) {
cout << "请输出须要删除的编号:";
int num;
cin >> num;
delByNum(book, num);
cout << "删除胜利!" << endl;
}
void mGetBook(const Book& book) {getBook(book);
}
void mFindBookByName(const Book& book) {
cout << "请输出要搜寻的人名:";
string name;
cin >> name;
findBookByName(book, name);
}
void mSetBookByName(Book& book) {
cout << "请输出姓名:";
string name;
cin >> name;
cout << "请输出性别(男 1,女 0):";
bool gender;
cin >> gender;
cout << "请输出年龄:";
int year;
cin >> year;
cout << "请输出电话号码:";
string phone;
cin >> phone;
cout << "请输出地址:";
string address;
cin >> address;
setBookByName(book, name, gender, year, phone, address);
cout << "设置胜利!" << endl;
}
void mDelAll(Book& book) {delAll(book);
cout << "已清空!" << endl;
}
最初咱们在主程序中写出整个的面板:
//main.cpp
#include"Manager.h"
int main() {
Book booky;
cout << "****************" << endl;
cout << "**1. 增加通讯录 **" << endl;
cout << "**2. 显示通讯录 **" << endl;
cout << "**3. 删除通讯录 **" << endl;
cout << "**4. 查找通讯录 **" << endl;
cout << "**5. 批改通讯录 **" << endl;
cout << "**6. 清空通讯录 **" << endl;
cout << "**7. 退出通讯录 **" << endl;
cout << "****************" << endl;
int op;
cin >> op;
while (op!=7) {switch (op)
{
case 1:
mAddBook(booky);
break;
case 2:
mGetBook(booky);
break;
case 3:
mDelByNum(booky);
break;
case 4:
mFindBookByName(booky);
break;
case 5:
mSetBookByName(booky);
break;
case 6:
mDelAll(booky);
break;
default:
cout << "谬误操作符!从新输出。";
}
cin.get();
cin.get();
cout << "****************" << endl;
cout << "**1. 增加通讯录 **" << endl;
cout << "**2. 显示通讯录 **" << endl;
cout << "**3. 删除通讯录 **" << endl;
cout << "**4. 查找通讯录 **" << endl;
cout << "**5. 批改通讯录 **" << endl;
cout << "**6. 清空通讯录 **" << endl;
cout << "**7. 退出通讯录 **" << endl;
cout << "****************" << endl;
cin >> op;
}
cout << "已退出通讯录。" << endl;
return 0;
}
这样就最终失去了咱们的零碎。
到目前为止,咱们根本实现了 C++ 中 C 语言包含的局部的根底内容。在下一个板块中,咱们会持续学习对于 C++ 多出 C 语言的局部,包含面向对象编程、泛型编程等等。
正文完