時間:2020-10-07來源:www.outletmksalestore.com作者:電腦系統城
概述
系統中需要實現的功能如下:
步驟
新建結構體
contact.h
?1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
#include<iostream> #include<string> using namespace std; struct Contact { string name; //姓名 string sex; //性別 int age; //年齡 int phoneNumber; //聯系電話 string address; //家庭地址 }; void printContactInfo( const Contact *p); |
定義
contact.cpp
?1 2 3 4 5 6 7 8 9 10 |
#include "Contact.h" void printContactInfo( const Contact * p) { cout << "姓名:" << p->name << "---性別:" << p->sex << "---年齡:" << p->age << "---聯系電話:" << p->phoneNumber << "---家庭地址:" << p->address << endl; } |
ContactManager.h
?1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
#include<iostream> #include "Contact.h" using namespace std; #define MAX 1000 struct ContactManager { //聯系人數組 Contact contactArr[MAX]; //當前聯系人數量 int size; }; void showMenu(); void exitSys(); void addContact(ContactManager *manager); void showContactList(ContactManager *manager); void delContactByName(ContactManager *manager); void findContactByName(ContactManager *manager); void updateContactByName(ContactManager *manager); void clearManager(ContactManager *manager); |
實現管理者
實現菜單功能
?1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#include "ContactManager.h" 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 << "-----> 請選擇操作項并輸入操作項編號:" << endl; } |
實現退出功能
?1 2 3 4 5 |
void exitSys() { cout << "歡迎下次使用,再見" << endl; system ( "pause" ); } |
新增聯系人
?1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
void addContact(ContactManager *manager) { cout << "請輸入聯系人姓名:" ; cin >> manager->contactArr[manager->size].name; cout << "請輸入聯系人性別:" ; cin >> manager->contactArr[manager->size].sex; cout << "請輸入聯系人年齡:" ; cin >> manager->contactArr[manager->size].age; cout << "請輸入聯系人號碼:" ; cin >> manager->contactArr[manager->size].phoneNumber; cout << "請輸入聯系人地址:" ; cin >> manager->contactArr[manager->size].address; cout << "添加聯系人成功?。?!" << endl; manager->size++; system ( "pause" ); system ( "cls" ); } |
展示聯系人列表
?1 2 3 4 5 6 7 8 9 |
void showContactList(ContactManager * manager) { for ( int i = 0; i < manager->size; i++) { printContactInfo(&manager->contactArr[i]); } system ( "pause" ); system ( "cls" ); } |
刪除聯系人
?1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
void delContactByName(ContactManager * manager) { cout << "請輸入要刪除聯系人的姓名:" ; string name; cin >> name; int pos = isExist(manager, name); if (pos == -1) { cout << "聯系人不存在??!" << endl; } else { cout << "聯系人的位置在" << pos << endl; //數據前移 for ( int i = pos; i < manager->size; i++) { manager->contactArr[pos] = manager->contactArr[pos + 1]; } cout << "刪除聯系人成功??!" << endl; manager->size--; } system ( "pause" ); system ( "cls" ); } |
查找聯系人
?1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
void findContactByName(ContactManager * manager) { cout << "請輸入要查找聯系人的姓名:" ; string name; cin >> name; int pos = isExist(manager, name); if (pos == -1) { cout << "聯系人不存在??!" << endl; } else { printContactInfo(&manager->contactArr[pos]); } system ( "pause" ); system ( "cls" ); } |
更新聯系人
?1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
void updateContactByName(ContactManager * manager) { cout << "請輸入要修改聯系人的姓名:" ; string name; cin >> name; int pos = isExist(manager, name); if (pos == -1) { cout << "聯系人不存在??!" << endl; } else { cout << "請輸入聯系人性別:" ; cin >> manager->contactArr[pos].sex; cout << "請輸入聯系人年齡:" ; cin >> manager->contactArr[pos].age; cout << "請輸入聯系人號碼:" ; cin >> manager->contactArr[pos].phoneNumber; cout << "請輸入聯系人地址:" ; cin >> manager->contactArr[pos].address; cout << "修改聯系人成功?。?!" << endl; } system ( "pause" ); system ( "cls" ); } |
清空通訊錄
?1 2 3 4 5 6 7 |
void clearManager(ContactManager * manager) { manager->size = 0; cout << "清空聯系人成功?。?!" << endl; system ( "pause" ); system ( "cls" ); } |
運行截圖
那么整體的項目到這里就算完成了。
到此這篇關于C++實現管理系統的示例代碼的文章就介紹到這了
2022-03-01
Java PTA 計算3到7位 水仙花數實例2022-03-01
AJAX SpringBoot 前后端數據交互的項目實現2020-10-22
關于idea無法修改模板中jdk版本問題