1. 程式人生 > 其它 >C++之通訊錄管理系統

C++之通訊錄管理系統

技術標籤:# C++C++通訊錄管理系統FanSmale因吉

文章目錄

引入

  工程分為三個檔案,如下圖:
在這裡插入圖片描述
  完整程式碼已上傳至Gitee:
  https://gitee.com/inkiinki/data20201205/blob/master/Code20210102/AddressBookManagementSystem.zip

main.cpp

/*
 作者:因吉
 聯絡方式:[email protected]
 建立日期:2021年1月21日
 近一次修改日期:2021年2月5日
*/

#include "menu.h"
int main (){ struct AddressBooks books; initializeAddressBooks(&books); string inputStr = ""; showMenu(); while (true) { switch (inputSelect()) { case 0: // 退出 cout << "下次再見。"; system("pause"); return 0; case 1: // 新增 addPerson(&
books); break; case 2: // 展示 showBooks(&books); break; case 3: // 刪除 if (isEmpty(&books)) { cout << "通訊錄為空,無法刪除。\n"; } else { cout << "輸入需要刪除聯絡人的姓名或者電話:"; cin >> inputStr; dllPerson(&books, inputStr); }
// Of if break; case 4: // 查詢 if (isEmpty(&books)) { cout << "通訊錄為空,無法查詢。\n"; } else { cout << "輸入需要查詢聯絡人的姓名或者電話:"; cin >> inputStr; findPerson(&books, inputStr); } // Of if break; case 5: // 修改 if (isEmpty(&books)) { cout << "通訊錄為空,無法修改。\n"; } else { cout << "輸入需要查詢聯絡人的姓名或者電話:"; cin >> inputStr; modifyPerson(&books, inputStr); } // Of if break; case 6: // 重置 cout << "通訊錄已重置。\n"; initializeAddressBooks(&books); break; case 7: // 清屏 system("cls"); showMenu(); break; default: break; } // Of switch } // Of while return 0; } // Of main

menu.h

/*
 作者:因吉
 聯絡方式:[email protected]
 建立日期:2021年1月21日
 近一次修改日期:2021年2月5日
*/

#include <iostream>
#include <string>
#include <sstream>
#define MAX_SIZE 1000

using namespace std;

struct Person {
	string name;
	int sex;
	int age;
	string num;
	string addr;
};

struct AddressBooks {
	int size;
	struct Person personArr[MAX_SIZE];
};

const string SEX[2] = {"女", "男"};

void showMenu();
int inputSelect();
void initializeAddressBooks(struct AddressBooks*);
bool isEmpty(struct AddressBooks*);
bool isFull(struct AddressBooks*);
void addPerson(struct AddressBooks*);
void inputInfo(struct AddressBooks*);
void inputInfo(struct AddressBooks*, int);
void showBooks(struct AddressBooks*);
void showPerson(struct AddressBooks*, int);
int isExist(struct AddressBooks*, string);
bool isNum(string);
void dllPerson(struct AddressBooks*, string);
void findPerson(struct AddressBooks*, string);
void modifyPerson(struct AddressBooks*, string);

menu.cpp

/*
 作者:因吉
 聯絡方式:[email protected]
 建立日期:2021年1月21日
 近一次修改日期:2021年2月5日
*/

#include "menu.h"

void showMenu() {
	cout << "****************************\n"
			"**********功能介紹**********\n"
			"**********1、新增 **********\n"
			"**********2、展示 **********\n"
			"**********3、刪除 **********\n"
			"**********4、查詢 **********\n"
			"**********5、修改 **********\n"
			"**********6、重置 **********\n"
			"**********7、清屏 **********\n"
			"**********0、退出 **********\n"
			"****************************\n";
} // Of showMenu

int inputSelect() {
	int select = 0;
	cout << "請輸入選擇:\n";
	cin >> select;

	return select;
} // Of inputSelect

void initializeAddressBooks(struct AddressBooks* books) {
	books->size = 0;
} // Of initializeAddressBooks

bool isEmpty(struct AddressBooks* books) {
	return books->size == 0;
} // Of isEmpty

bool isFull(struct AddressBooks* books) {
	return books->size == MAX_SIZE;
} // Of isFull

void addPerson(struct AddressBooks* books) {
	if (isFull(books)) {
		cout << "通訊錄已滿,無法新增!\n";
		return;
	} // Of if

	inputInfo(books);
	books->size++;
} // Of addPerson

void inputInfo(struct AddressBooks* books) {
	inputInfo(books, books->size);
} // Of inputInfo

void inputInfo(struct AddressBooks* books, int idx) {
	cout << "請輸入姓名:";
	cin >> books->personArr[idx].name;
	cout << "請輸入性別 (1男0女):";
	while (true) {
		cin >> books->personArr[idx].sex;
		if (books->personArr[idx].sex == 1 || books->personArr[idx].sex == 0) {
			break;
		} // Of if
		cout << "請重新輸入性別:";
	} // Of while
	cout << "請輸入年齡:";
	while (true) {
		cin >> books->personArr[idx].age;
		if (books->personArr[idx].age >= 0) {
			break;
		} // Of if
		cout << "請重新輸入年齡:";
	} // Of while
	cout << "請輸入電話:";
	cin >> books->personArr[idx].num;
	cout << "請輸入地址:";
	cin >> books->personArr[idx].addr;
	cout << "輸入完畢。\n";
} // Of inputInfo

void showBooks(struct AddressBooks* books) {
	if (isEmpty(books)) {
		cout << "通訊錄為空。\n";
		return;
	} // Of if

	for (int i = 0; i < books->size; i++) {
		cout << "姓名:" <<books->personArr[i].name << "; "
				"性別:" <<SEX[books->personArr[i].sex] << "; "
				"年齡:" <<books->personArr[i].age << "; "
				"電話:" <<books->personArr[i].num << "; "
				"地址:" <<books->personArr[i].addr << "。\n";
	} // Of for i
} // Of showBooks

void showPerson(struct AddressBooks* books, int idx) {
	cout << "姓名:" << books->personArr[idx].name << "; "
			"性別:" << SEX[books->personArr[idx].sex] << "; "
			"年齡:" << books->personArr[idx].age << "; "
			"電話:" << books->personArr[idx].num << "; "
			"地址:" << books->personArr[idx].addr << "。\n";
} // Of showPerson

int isExist(struct AddressBooks* books, string str) {

	bool flag = isNum(str);
	for (int i = 0; i < books->size; i++) {
		if (books->personArr[i].name == str || (flag && books->personArr[i].num == str)) {
			return i;
		} // Of if
	} // Of for i

	return -1;
} // Of isExist

bool isNum(string str)
{
	stringstream sin(str);
	double d;
	char c;
	if (!(sin >> d))
		return false;
	if (sin >> c)
		return false;
	return true;
} // Of ifNum

void dllPerson(struct AddressBooks* books, string str) {
	
	int findIdx = isExist(books, str);
	if (findIdx == -1) {
		cout << "無聯絡人資訊。\n";
		return;
	} // Of if

	cout << "已刪除的資訊:\n";
	showPerson(books, findIdx);
	if (findIdx == books->size - 1) {
		return;
	} // Of if

	books->size--;
	for (int i = findIdx; i < books->size; i++) {
		books->personArr[i].name = books->personArr[i + 1].name;
		books->personArr[i].sex = books->personArr[i + 1].sex;
		books->personArr[i].age = books->personArr[i + 1].age;
		books->personArr[i].num = books->personArr[i + 1].num;
		books->personArr[i].addr = books->personArr[i + 1].addr;
	} // Of for i
	cout << "刪除完畢。\n";

} // Of dllPerson

void findPerson(struct AddressBooks* books, string str) {
	int findIdx = isExist(books, str);
	if (findIdx == -1) {
		cout << "無聯絡人資訊。\n";
		return;
	} // Of if

	cout << "查詢資訊如下:\n";
	showPerson(books, findIdx);
} // Of findPerson

void modifyPerson(struct AddressBooks* books, string str) {
	int findIdx = isExist(books, str);
	if (findIdx == -1) {
		cout << "無聯絡人資訊。\n";
		return;
	} // Of if

	cout << "請輸入修改後的資訊:\n";
	inputInfo(books, findIdx);
	cout << "修改後如下:\n";
	showPerson(books, findIdx);
} // Of modifyPerson