欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C++實現(xiàn)通訊錄小功能

 更新時間:2022年06月20日 14:08:02   作者:糯米團子鴨  
這篇文章主要為大家詳細介紹了C++實現(xiàn)通訊錄小功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了C++實現(xiàn)通訊錄功能的具體代碼,供大家參考,具體內容如下

思路:

1.顯示菜單欄

void menu() {
?
?? ?cout << "——————————————————" << endl;
?? ?cout << "*********** 1.添加聯(lián)系人 ***********" << endl;
?? ?cout << "*********** 2.刪除聯(lián)系人 ***********" << endl;
?? ?cout << "*********** 3.修改聯(lián)系人 ***********" << endl;
?? ?cout << "*********** 4.查找聯(lián)系人 ***********" << endl;
?? ?cout << "*********** 5.顯示通訊錄 ***********" << endl;
?? ?cout << "*********** 6.清空聯(lián)系人 ***********" << endl;
?? ?cout << "*********** 0.退出通訊錄 ***********" << endl;
?? ?cout << "——————————————————" << endl;
}

2.退出

int main() {
?
?? ?int select = 0;
?? ?cin >> select;
?? ?switch (select) {
?? ??? ?
?? ??? ?case 0://退出通訊錄
?? ??? ? ? ?cout << "歡迎下次使用" << endl;
?? ??? ??? ?system("pause");
?? ??? ??? ?return 0;
?? ??? ??? ?break;
?? ??? ?
?? ?}
?? ?
?? ?system("pause");
?? ?return 0;
}

3.創(chuàng)建結構體

3.1創(chuàng)建聯(lián)系人結構體
3.2創(chuàng)建通訊錄結構體

//定義聯(lián)系人結構體
//姓名、電話號碼、郵箱、地址
struct person {
?? ?string name;
?? ?string number;
?? ?string Email;
?? ?string address;
};
// 定義通訊錄結構體
struct contacts {
?? ?int people_num;
?? ?struct person personarr[MAX];//子結構體:聯(lián)系人信息
};

4.添加聯(lián)系人

void addperson(struct contacts* p) {
?? ?if (p->people_num == MAX ) {
?? ??? ?cout << "通訊錄已滿" << endl;
?? ?}
?? ?else {//添加聯(lián)系人信息
?? ??? ?string name, number, Email, address;
?? ??? ?cout << "請輸入姓名:" << endl;
?? ??? ?cin >> name;
?? ??? ?cout << "請輸入電話:" << endl;
?? ??? ?cin >> number;
?? ??? ?cout << "請輸入郵箱:" << endl;
?? ??? ?cin >> Email;
?? ??? ?cout << "請輸入地址:" << endl;
?? ??? ?cin >> address;
?? ??? ?p->personarr[p->people_num].name = name;
?? ??? ?p->personarr[p->people_num].number = number;
?? ??? ?p->personarr[p->people_num].Email = Email;
?? ??? ?p->personarr[p->people_num].address = address;
?? ??? ?
?? ??? ?p->people_num++;
?? ??? ?cout << "添加成功!" << endl;
?? ?}
}

5.刪除聯(lián)系人

判斷聯(lián)系人是否存在

int existperson(struct contacts* p,string name) {
?? ?
?? ?for (int i = 0; i < p->people_num; i++) {
?? ??? ?if ( p->personarr[i].name == name ) {
?? ??? ??? ?return i;
?? ??? ?}
?? ?}
?? ?return -1;
}

若存在,獲取聯(lián)系人在通訊錄位置,將position后面的都往前移動一個位置,覆蓋之前的值

//刪除聯(lián)系人
void delperson(struct contacts* p,int position) {
?? ?while (position < (p->people_num)) {
?? ??? ?p->personarr[position] = p->personarr[position + 1];
?? ??? ?position++;
?? ?}
?? ?p->people_num--;
?? ?cout << "刪除成功!" << endl;
}

6.修改

檢查要修改聯(lián)系人是否存在,并獲取當前位置

void modifyperson(struct contacts* p, int position) {
?? ?string ?number, Email, address;
?? ?
?? ?cout << "請輸入修改電話:" << endl;
?? ?cin >> number;
?? ?cout << "請輸入修改郵箱:" << endl;
?? ?cin >> Email;
?? ?cout << "請輸入修改地址:" << endl;
?? ?cin >> address;
?
?? ?p->personarr[position].number = number;
?? ?p->personarr[position].Email = Email;
?? ?p->personarr[position].address = address;
?? ?cout << "修改成功!" << endl;
}

7.查找

void searchperson(struct contacts* p, int position) {
?? ?cout << "姓名:" << p->personarr[position].name << " ? ?"
?? ??? ?<< "電話:" << p->personarr[position].number << " ? ?"
?? ??? ?<< "郵箱:" << p->personarr[position].Email << " ? ?"
?? ??? ?<< "地址:" << p->personarr[position].address << " ? ?";
}

8.顯示通訊錄

void showcontact(struct contacts* p) {
?? ?for (int i = 0; i < (p->people_num); i++) {
?? ??? ?cout <<"姓名:" << p->personarr[i].name << " ? ?"
?? ??? ??? ?<<"電話:" << p->personarr[i].number << " ? ?"
?? ??? ??? ?<<"郵箱:" << p->personarr[i].Email << " ? ?"
?? ??? ??? ?<<"地址:" << p->personarr[i].address << " ? ?"<< endl;
?
?? ?}?? ??? ?
}

9.清空通訊錄

void cleancontact(struct contacts* p) {
?? ?p->people_num = 0;
?? ?cout << "已清空!" << endl;
}

全部代碼

#include<iostream>
using namespace std;
#define MAX 200?
?
//1.菜單欄顯示
void menu() {
?
?? ?cout << "——————————————————" << endl;
?? ?cout << "*********** 1.添加聯(lián)系人 ***********" << endl;
?? ?cout << "*********** 2.刪除聯(lián)系人 ***********" << endl;
?? ?cout << "*********** 3.修改聯(lián)系人 ***********" << endl;
?? ?cout << "*********** 4.查找聯(lián)系人 ***********" << endl;
?? ?cout << "*********** 5.顯示通訊錄 ***********" << endl;
?? ?cout << "*********** 6.清空聯(lián)系人 ***********" << endl;
?? ?cout << "*********** 0.退出通訊錄 ***********" << endl;
?? ?cout << "——————————————————" << endl;
}
?
//2.定義結構體
//2.1定義聯(lián)系人結構體
//姓名、電話號碼、郵箱、地址
struct person {
?? ?string name;
?? ?string number;
?? ?string Email;
?? ?string address;
};
//2.2 定義通訊錄結構體
struct contacts {
?? ?int people_num;
?? ?struct person personarr[MAX];//子結構體:聯(lián)系人信息
};
?
//3.添加聯(lián)系人
void addperson(struct contacts* p) {
?? ?if (p->people_num == MAX ) {
?? ??? ?cout << "通訊錄已滿" << endl;
?? ?}
?? ?else {//添加聯(lián)系人信息
?? ??? ?string name, number, Email, address;
?? ??? ?cout << "請輸入姓名:" << endl;
?? ??? ?cin >> name;
?? ??? ?cout << "請輸入電話:" << endl;
?? ??? ?cin >> number;
?? ??? ?cout << "請輸入郵箱:" << endl;
?? ??? ?cin >> Email;
?? ??? ?cout << "請輸入地址:" << endl;
?? ??? ?cin >> address;
?? ??? ?p->personarr[p->people_num].name = name;
?? ??? ?p->personarr[p->people_num].number = number;
?? ??? ?p->personarr[p->people_num].Email = Email;
?? ??? ?p->personarr[p->people_num].address = address;
?? ??? ?
?? ??? ?p->people_num++;
?? ??? ?cout << "添加成功!" << endl;
?? ?}
}
//4.刪除聯(lián)系人
//4.1檢測聯(lián)系人是否存在
int existperson(struct contacts* p,string name) {
?? ?
?? ?for (int i = 0; i < p->people_num; i++) {
?? ??? ?if ( p->personarr[i].name == name ) {
?? ??? ??? ?return i;
?? ??? ?}
?? ?}
?? ?return -1;
}
//刪除聯(lián)系人
void delperson(struct contacts* p,int position) {
?? ?while (position < (p->people_num)) {
?? ??? ?p->personarr[position] = p->personarr[position + 1];
?? ??? ?position++;
?? ?}
?? ?p->people_num--;
?? ?cout << "刪除成功!" << endl;
}
//5.修改聯(lián)系人
void modifyperson(struct contacts* p, int position) {
?? ?string ?number, Email, address;
?? ?
?? ?cout << "請輸入修改電話:" << endl;
?? ?cin >> number;
?? ?cout << "請輸入修改郵箱:" << endl;
?? ?cin >> Email;
?? ?cout << "請輸入修改地址:" << endl;
?? ?cin >> address;
?
?? ?p->personarr[position].number = number;
?? ?p->personarr[position].Email = Email;
?? ?p->personarr[position].address = address;
?? ?cout << "修改成功!" << endl;
}
?
//6.查找聯(lián)系人
void searchperson(struct contacts* p, int position) {
?? ?cout << "姓名:" << p->personarr[position].name << " ? ?"
?? ??? ?<< "電話:" << p->personarr[position].number << " ? ?"
?? ??? ?<< "郵箱:" << p->personarr[position].Email << " ? ?"
?? ??? ?<< "地址:" << p->personarr[position].address << " ? ?";
}
?
//7.顯示通訊錄
void showcontact(struct contacts* p) {
?? ?for (int i = 0; i < (p->people_num); i++) {
?? ??? ?cout <<"姓名:" << p->personarr[i].name << " ? ?"
?? ??? ??? ?<<"電話:" << p->personarr[i].number << " ? ?"
?? ??? ??? ?<<"郵箱:" << p->personarr[i].Email << " ? ?"
?? ??? ??? ?<<"地址:" << p->personarr[i].address << " ? ?"<< endl;
?
?? ?}?? ??? ?
}
//8.清空聯(lián)系人
void cleancontact(struct contacts* p) {
?? ?p->people_num = 0;
?? ?cout << "已清空!" << endl;
}
?
int main() {
?? ?//創(chuàng)建通訊錄結構體變量
?? ?struct contacts c;
?? ?//初始化通訊錄當前聯(lián)系人個數(shù)
?? ?c.people_num = 0;
?? ?int select = 0;
?? ?string name;
?? ?while (1) {
?? ??? ?menu();
?? ??? ?cin >> select;
?? ??? ?switch (select) {
?? ??? ??? ?case 1:// 添加聯(lián)系人
?? ??? ??? ??? ?addperson(&c);
?? ??? ??? ??? ?system("pause");
?? ??? ??? ??? ?break;
?
?? ??? ??? ?case 2:// 刪除聯(lián)系人
?? ?
?? ??? ??? ??? ?cout << "請輸入刪除聯(lián)系人的姓名:";
?? ??? ??? ??? ?cin >> name;
?? ??? ??? ??? ?if (existperson(&c,name)==-1) {
?? ??? ??? ??? ??? ?cout << "該聯(lián)系人不存在!";
?? ??? ??? ??? ?}
?? ??? ??? ??? ?else{
?? ??? ??? ??? ??? ?delperson(&c, existperson(&c, name));
?? ??? ??? ??? ?}
?? ??? ??? ??? ?
?? ??? ??? ??? ?system("pause");
?? ??? ??? ??? ?break;
?
?? ??? ??? ?case 3:// 修改聯(lián)系人
?? ??? ??? ?
?? ??? ??? ??? ?cout << "請輸入要修改聯(lián)系人的姓名:";
?? ??? ??? ??? ?cin >> name;
?? ??? ??? ??? ?if (existperson(&c,name) == -1) {
?? ??? ??? ??? ??? ?cout << "該聯(lián)系人不存在!";
?? ??? ??? ??? ?}
?? ??? ??? ??? ?else {
?? ??? ??? ??? ??? ?modifyperson(&c, existperson(&c, name));
?? ??? ??? ??? ?}
?? ??? ??? ??? ?
?? ??? ??? ??? ?system("pause");
?? ??? ??? ??? ?break;
?
?? ??? ??? ?case 4:// 查找聯(lián)系人
?? ??? ??? ??? ?
?? ??? ??? ??? ?cout << "請輸入查找聯(lián)系人的姓名:";
?? ??? ??? ??? ?cin >> name;
?? ??? ??? ??? ?if (existperson(&c,name) == -1) {
?? ??? ??? ??? ??? ?cout << "該聯(lián)系人不存在!";
?? ??? ??? ??? ?}
?? ??? ??? ??? ?else {
?? ??? ??? ??? ??? ?searchperson(&c, existperson(&c, name));
?? ??? ??? ??? ?}
?? ??? ??? ??? ?
?? ??? ??? ??? ?system("pause");
?? ??? ??? ??? ?break;
?
?? ??? ??? ?case 5:// 顯示通訊錄?
?? ??? ??? ??? ?showcontact(&c);
?? ??? ??? ??? ?system("pause");?
?? ??? ??? ??? ?break;
?? ??? ??? ?case 6:// 清空聯(lián)系人
?? ??? ??? ??? ?cleancontact(&c);
?? ??? ??? ??? ?system("pause");
?? ??? ??? ??? ?break;
?? ??? ??? ?case 0://退出通訊錄
?? ??? ??? ??? ?cout << "歡迎下次使用" << endl;
?? ??? ??? ??? ?system("pause");//暫停批文件的處理
?? ??? ??? ??? ?return 0;
?? ??? ??? ??? ?break;
?? ??? ?}
?? ??? ?system("cls");//清屏
?? ?}
?? ?
?? ?system("pause");
?? ?return 0;
}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • C++示例講解觀察者設計模式

    C++示例講解觀察者設計模式

    觀察者模式是極其重要的一個設計模式,也是我?guī)啄觊_發(fā)過程中使用最多的設計模式,本文首先概述觀察者模式的基本概念和Demo實現(xiàn),接著是觀察者模式在C++中的應用,最后是對觀察者模式的應用場景和優(yōu)缺點進行總結
    2022-12-12
  • 詳解C語言#define預處理宏定義

    詳解C語言#define預處理宏定義

    本文主要介紹了C語言#define預處理宏定義,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • OpenCV cv.Mat與.txt文件數(shù)據(jù)的讀寫操作

    OpenCV cv.Mat與.txt文件數(shù)據(jù)的讀寫操作

    這篇文章主要介紹了OpenCV cv.Mat 與 .txt 文件數(shù)據(jù)的讀寫操作,現(xiàn)在分享給大家,也給大家做個參考
    2018-05-05
  • C++獲取當前進程IAT的方法

    C++獲取當前進程IAT的方法

    這篇文章主要介紹了C++獲取當前進程IAT的方法,實例講述了IAT(導入地址表)的獲取方法,在Windows應用程序開發(fā)中有著非常實用的應用價值,需要的朋友可以參考下
    2014-10-10
  • C++數(shù)據(jù)結構與算法之雙緩存隊列實現(xiàn)方法詳解

    C++數(shù)據(jù)結構與算法之雙緩存隊列實現(xiàn)方法詳解

    這篇文章主要介紹了C++數(shù)據(jù)結構與算法之雙緩存隊列實現(xiàn)方法,結合實例形式分析了雙緩存隊列的原理、實現(xiàn)方法與相關注意事項,需要的朋友可以參考下
    2017-08-08
  • 深入理解c++中virtual關鍵字

    深入理解c++中virtual關鍵字

    本篇文章主要是對c++中virtual關鍵字進行了詳細的介紹,需要的朋友可以過來參考下,希望對大家有所幫助
    2014-02-02
  • C語言基礎函數(shù)用法示例詳細解析

    C語言基礎函數(shù)用法示例詳細解析

    最接地氣的C函數(shù)基礎介紹,此處對于函數(shù)的相關知識點做一些簡要的介紹,作者實屬初學,寫博客也是作者學習的一個過程,難免文章中有內容理解不到位或者有不當之處,還請朋友們不吝指正
    2021-11-11
  • 在1個Matlab m文件中定義多個函數(shù)直接運行的操作方法

    在1個Matlab m文件中定義多個函數(shù)直接運行的操作方法

    這篇文章主要介紹了如何在1個Matlab m文件中定義多個函數(shù)直接運行,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-12-12
  • C++實現(xiàn)靜態(tài)鏈表

    C++實現(xiàn)靜態(tài)鏈表

    這篇文章主要為大家詳細介紹了C++實現(xiàn)靜態(tài)鏈表,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-04-04
  • 詳解C++ 參數(shù)的三種傳遞方式和應用場景

    詳解C++ 參數(shù)的三種傳遞方式和應用場景

    這篇文章主要介紹C++ 參數(shù)的三種傳遞方式和應用場景,C++ 參數(shù)的三種傳遞方式分別是值傳遞、指針傳遞和引用傳遞,感興趣的同學可以參考閱讀下
    2023-06-06

最新評論