该代码是我学习1周C++写出来的,诸多不完美之处还需后续改善
//使用数组实现
#define MAX 1000 //最大人数,使用宏常量
#include <iostream>
#include <string>
#include <limits>
using namespace std;
struct contactPerson{
//设计联系人结构体
string name;
short sex;
short age;
string telephone;
string address;
};
struct AddressBooks{
struct contactPerson personArray[MAX] = {};
int CurrentSize;
};
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;
cout << "请输入选项>> ";
}
/
void ensureInput(AddressBooks * abs, int fields, short index);
void addContactPerson(AddressBooks * abs){
//先判断最特殊的情况!!这个思想很重要!
if(abs->CurrentSize > 1000){
cout << "当前通讯录已满人,请清理后再尝试添加!" << endl;
return; //无返回值函数也可以直接返回
}
//添加具体联系人
system("cls");
cout << "姓名:"; cin >> abs->personArray[abs->CurrentSize].name;
cout << "年龄: ";
ensureInput(abs, 1, abs->CurrentSize); //输入年龄
cout << "0-男/1-女: ";
ensureInput(abs, 2, abs->CurrentSize); //输入性别
cout << "电话: "; cin >> abs->personArray[abs->CurrentSize].telephone;
cout << "住址: "; cin >> abs->personArray[abs->CurrentSize].address;
abs->CurrentSize ++;
cout << "联系人信息保存成功!" << endl;
}
void showAddressBooks(AddressBooks * abs, short start = 0){
if(abs->CurrentSize == 0){
cout << "当前未添加联系人!" << endl;
return;
}
system("cls");
for(short i = start; i < abs->CurrentSize; i++){
cout << "姓名: " << abs->personArray[i].name << endl;
cout << "电话: " << abs->personArray[i].telephone << endl;
cout << "年龄: " << abs->personArray[i].age << endl;
//表达式记得用()括起来,不然会报错
cout << "性别: " << (abs->personArray[i].sex == 0 ? "男" : "女") << endl;
cout << "住址:" << abs->personArray[i].address << endl;
cout << "---------------------------------" << endl;
}
}
short checkContactPerson(AddressBooks * abs){
string name;
cin >> name;
short location = -1;
for(short i = 0; i < abs->CurrentSize; i++){
if(abs->personArray[i].name == name){
location = i;
break;
}
}
if(location == -1){
cout << "未查询到" << name << "的相关信息!" << endl;
}
return location; //最后把检索到的位置返回出去
}
short SelectAddressBooks(AddressBooks * abs){
short index = checkContactPerson(abs);
if(index == -1){
return index;
}
short temp = abs->CurrentSize;
abs->CurrentSize = index + 1;
//调用之前写的展示函数进行展示
showAddressBooks(abs, index);
abs->CurrentSize = temp;
return index;
}
void DeleteContactPerson(AddressBooks * abs){
cout << "请输入你要删除的联系人名: ";
short index = SelectAddressBooks(abs);
if(index == -1){
return;
}
string alarm;
cout << "你确认删除该联系人吗?(确认-y/其他键-n):";
cin >> alarm;
if(alarm != "y" and alarm != "Y"){
cout << "删除操作已撤回!" << endl;
return;
}
for(short i = index; i < abs->CurrentSize; i++){
//数据前移,最后面的数据怎么办呢?其实会被全0给覆盖掉
//由于在查询时已经总数已经减少一个人,最后面那个全0数据就不会显示出来
abs->personArray[i] = abs->personArray[i+1];
}
abs->CurrentSize --;
cout << "删除成功!" << endl;
}
void ModifyAddressBooks(AddressBooks * abs){
cout << "请输入你要修改的联系人名: ";
short index = SelectAddressBooks(abs);
if(index == -1){
return;
}
short fields;
string modityVal;
while(true){
cout << "请输入你要修改的字段:" << endl;
cout << "1-姓名 / 2-电话 / 3-性别 / 4-年龄 / 5-住址" << endl;
cout << ">>";
cin >> fields;
if(fields != 1 and fields != 2 and fields != 3 and fields != 4 and fields != 5){
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n'); //清除输入缓冲区
cout << "请输入数字1-5!" << endl;
continue;
}
cout << "请输入你要修改成的值:";
switch(fields){
case(1): cin >> modityVal; abs->personArray[index].name = modityVal; break;
case(2): cin >> modityVal; abs->personArray[index].telephone = modityVal; break;
case(3): ensureInput(abs, 2, index); break;
case(4): ensureInput(abs, 1, index); break;
case(5): cin >> modityVal; abs->personArray[index].address = modityVal; break;
default: break;
}
cout << "修改成功!" << endl;
return;
}
}
void clearAddressBooks(AddressBooks * abs){
string alarm;
cout << "你确定要删除整个通讯录的记录吗?" << endl;
cout << "y-确定 / 其他键取消:"; cin >> alarm;
if(alarm == "y"){
abs->CurrentSize = 0;
*abs->personArray = {};
cout << "通讯录已清空!" << endl;
}
else{
cout << "当前清空操作已撤回。" << endl;
}
}
void ensureInput(AddressBooks * abs, int fields, short index){
//fields = 1 代表要输入的是age字段,其他数字代表的是sex字段
//index特别指代是哪个联系人
if(fields == 1){
while(true){
short age;
cin >> age;
if(cin.fail()){
cout << "输入错误,请检查输入!" << endl;
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n'); //清除输入缓冲区
cout << "年龄:";
continue;
}
abs->personArray[index].age = age;
return;
}
}
else{
while(true){
short sex;
cin >> sex;
if(sex == 0 || sex == 1){
abs->personArray[index].sex = sex;
return;
}
else{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n'); //清除输入缓冲区
cout << "请输入数字0或1!" << endl;
cout << "0-男/1-女: ";
}
}
}
}
int main(){
//初始化通讯录
AddressBooks abs;
abs.CurrentSize = 0;
short choice; //当cin输入失败时,会继承变量的默认值0
while(true){
system("cls");
showMenu();
cin >> choice;
switch(choice){
case(1):
addContactPerson(&abs);
break;
case(2):
showAddressBooks(&abs);
break;
case(3):
DeleteContactPerson(&abs);
break;
case(4):
cout << "请输入你要查询的联系人名: ";
SelectAddressBooks(&abs);
break;
case(5):
ModifyAddressBooks(&abs);
break;
case(6):
clearAddressBooks(&abs);
break;
case(0): cout << "成功退出系统!"; return 0; break;
default: cout << "无效输入!" << endl;
if(cin.fail()){
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n'); //清除输入缓冲区
}
break;
}
system("pause");
}
}
中间加入大量函数,且在诸如查询函数,修改函数,删除函数等函数间建立了相对复杂的关系,譬如删除函数DeleteContactPerson依赖了SelectComtactPerson查询函数,而查询函数又依赖于检查函数CheckContactPerson和展示所有联系人的函数ShowAddressBooks。大量依赖关系主要是为了减少最终的代码量,但似乎代码量还是挺大的.jpg
第二段代码是在学习完链表后写的,由于之前学过Python的?面向对象,所以粗仿python的写法用面向对象写了一下管理系统,为了避免增加太多工作量删除了一些输入字段,函数的复用性不高,而且似乎也没有很好的写出面向对象的感觉
唯一比较有亮点的是特地仿照MySQL的表格格式来展示通讯录信息,让整个信息显得更加整齐...
#include <iostream>
#include <string>
#include <iomanip>
#include <limits>
//采用面向对象的思想与链表的方式重写通讯录管理系统
//我思考了一下,为了能够实现最快的添加联系人,还是用将新添加的联系人放置在链表头部
class AddressBook{
public:
struct ContactPerson{
std::string name;
std::string telephone;
std::string address;
ContactPerson* next;
};
static void middleShow(std::string showStr, int showWidth){
int text_length = showStr.length();
int left_padding = (showWidth - text_length) / 2;
int right_padding = showWidth - text_length - left_padding;
std::cout << std::setw(left_padding)
<< ' ' << showStr
<< std::setw(right_padding);
}
private:
ContactPerson* headNode = nullptr;
int contactPersonCount = 0;
public:
void showMenu(){
//菜单界面
std::cout << "******************************" << std::endl;
std::cout << "***** 1. Add contact *****" << std::endl;
std::cout << "***** 2. show contact *****" << std::endl;
std::cout << "***** 3. delete contact *****" << std::endl;
std::cout << "***** 4. select contact *****" << std::endl;
std::cout << "***** 5. modify contact *****" << std::endl;
std::cout << "***** 6. clear contact *****" << std::endl;
std::cout << "***** 0. exit system *****" << std::endl;
std::cout << "******************************" << std::endl;
std::cout << "Enter your choice>> ";
}
void addContactPerson(){
//添加具体联系人
system("cls");
ContactPerson* newNode = new ContactPerson();
std::cout << "Name:"; std::cin >> newNode->name;
std::cout << "telephone:"; std::cin >> newNode->telephone;
std::cout << "address:"; std::cin >> newNode->address;
newNode->next = headNode;
headNode = newNode;
contactPersonCount ++;
std::cout << "\nsuccessfully save!" << std::endl;
}
void showAddressBooks(){
if(contactPersonCount == 0){
std::cout << "No contact person in the addressbook!\n";
return;
}
system("cls");
ContactPerson* temp = headNode;
std::cout << "+----+----------+-----------------+-------------------+\n";
std::cout << "| id | Name | Telephone | Address |\n";
std::cout << "+----+----------+-----------------+-------------------+\n";
for(int i = 0; i < contactPersonCount; ++i){
std::cout << "| "<< i+1 << std::setw(3) << '|';
middleShow(temp->name, 11); std::cout << '|';
middleShow(temp->telephone, 18); std::cout << '|';
middleShow(temp->address, 20); std::cout << '|';
std::cout << std::endl;
std::cout << "+----+----------+-----------------+-------------------+\n";
temp = temp->next;
}
}
void SimplyShow(ContactPerson* currentNode){
system("cls");
std::cout << "+----+----------+-----------------+-------------------+\n";
std::cout << "| id | Name | Telephone | Address |\n";
std::cout << "+----+----------+-----------------+-------------------+\n";
std::cout << "| "<< 1 << std::setw(3) << '|';
middleShow(currentNode->name, 11); std::cout << '|';
middleShow(currentNode->telephone, 18); std::cout << '|';
middleShow(currentNode->address, 20); std::cout << '|';
std::cout << "\n+----+----------+-----------------+-------------------+\n";
}
bool ConfirmOperation(std::string alarm){
std::string confirm;
std::cout << alarm;
std::cin >> confirm;
if(confirm != "y" and confirm != "Y"){
std::cout << "Undo the operation!\n";
return false;
}
return true;
}
ContactPerson* SelectAddressBooks(std::string info=
"Please enter the contact name you want to search for:"){
system("cls");
std::string name;
std::cout << info;
std::cin >> name;
ContactPerson* temp = headNode;
while(temp != nullptr){
if(temp->name == name){
break;
}
temp = temp->next;
}
if(temp == nullptr){
std::cout << "Cannot find such the person: " << name << std::endl;
return nullptr;
}
SimplyShow(temp);
return temp;
}
void DeleteContactPerson(){
system("cls");
short id = 0;
std::string alarm;
ContactPerson* temp = headNode;
while(true){
std::cout << "Please enter the contact id you want to delete:";
std::cin >> id;
if(id == 1){
SimplyShow(temp);
if(ConfirmOperation("Are you sure you want to delete this person(y/n)?")){
headNode = temp->next;
delete temp;
contactPersonCount --;
std::cout << "Sucessfully delete!\n";
}
return;
}
else if(std::cin.fail() || id==0 || id > contactPersonCount){
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
system("cls");
std::cout << "Invaild enter, please check your input!\n";
}
else{
break;
}
}
system("pause");
for(int i = 0; i < id - 1; ++i){
temp = temp->next;
}
SimplyShow(temp->next);
if(ConfirmOperation("Are you sure you want to delete this person(y/n)?")){
ContactPerson* deleteNode = temp->next;
temp->next = deleteNode->next;
delete deleteNode;
std::cout << "Sucessfully delete!\n";
contactPersonCount --;
return;
}
}
void ModifyAddressBooks(){
ContactPerson* address = SelectAddressBooks("Please enter the contact name:");
if(address == nullptr){
return;
}
short fields;
std::string modityVal;
while(true){
std::cout << "Please enter the fields you want to modify:\n";
std::cout << "1-name / 2-telephone / 3-address\n";
std::cout << ">>";
std::cin >> fields;
if(fields != 1 and fields != 2 and fields != 3){
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cout << "Please enter the number 1-3!\n";
continue;
}
std::cout << "Please enter the value after modify:";
std::cin >> modityVal;
switch(fields){
case(1): address->name = modityVal; break;
case(2): address->telephone = modityVal; break;
case(3): address->address = modityVal; break;
default: break;
}
std::cout << "Modify sucessfully!\n";
return;
}
}
void clearAddressBooks(){
showAddressBooks();
if(ConfirmOperation("Are you sure you want to clear the total addressbooks(y/n)? ")){
ContactPerson* temp;
for(int i = 0; i < contactPersonCount; ++i){
temp = headNode->next;
delete headNode;
headNode = temp;
}
contactPersonCount = 0;
std::cout << "Sucessfully clear the total addressbook!" << std::endl;
}
}
};
int main(){
AddressBook abs;
short choice;
while(true){
system("cls");
abs.showMenu();
std::cin >> choice;
switch(choice){
case(1):
abs.addContactPerson();
break;
case(2):
abs.showAddressBooks();
break;
case(3):
abs.DeleteContactPerson();
break;
case(4):
abs.SelectAddressBooks();
break;
case(5):
abs.ModifyAddressBooks();
break;
case(6):
abs.clearAddressBooks();
break;
case(0): std::cout << "Sucessfully exit!"; return 0; break;
default: std::cout << "Invaild enter, please check your input!\n";
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); //清除输入缓冲区
break;
}
system("pause");
}
}