C/C++教程

B站黑马c++学习笔记 —— 通讯录案例

本文主要是介绍B站黑马c++学习笔记 —— 通讯录案例,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

通讯录案例

1.1 add.h

1.2 add.cpp

1.3 通讯录.cpp

在这里插入图片描述

1.1 add.h

#include<iostream>
#include<string>
using namespace std;

struct person {
	string name;
	string sex;
	int age;
	string tele;
	string addr;
};

void title();
void print2(int a);
void search(bool * a);
void con(bool * a, bool * b);
void clean(struct person per_arr[], int * num_add);
void print(struct person per_arr[], int * num_add);
void add_per(struct person per_arr[],int * num_add);
void seek(struct person per_arr[], int * num_add, bool * a);
void delet(struct person per_arr[], int * num_add, bool * a);
void revise(struct person per_arr[], int * num_add, bool * a);

1.2 add.cpp

#include"add.h"

//添加联系人
void add_per(struct person per_arr[],int * num_add) {
	cout << "输入联系人的名字:" << endl;
	cin >> per_arr[(*num_add)].name;
	cout << "输入联系人的电话:" << endl;
	cin >> per_arr[(*num_add)].tele;
	(*num_add)++;
}

//功能表
void title() {
	system("cls");
	cout << endl;
	cout << "\t*****************" << endl;
	cout << "\t添加联系人 —— 1" << endl;
	cout << "\t显示联系人 —— 2" << endl;
	cout << "\t删除联系人 —— 3" << endl;
	cout << "\t查找联系人 —— 4" << endl;
	cout << "\t修改联系人 —— 5" << endl;
	cout << "\t清空联系人 —— 6" << endl;
	cout << "\t退出通讯录 —— 7" << endl;
	cout << "\t*****************" << endl;
	cout << endl;
}

//打印通讯录
void print(struct person per_arr[], int * num_add) {
	for (int i = 0; i < (*num_add); i++) {
		cout << "\n\t序号:" << i + 1 << "\t名字:" << per_arr[i].name << "\t电话:" << per_arr[i].tele << endl;
	}
}


//查找联系人
void seek(struct person per_arr[], int * num_add, bool * a) {
	struct person temp[1];
	cin >> temp[0].name;
	for (int i = 0; i < (*num_add); i++) {
		if (temp[0].name == per_arr[i].name) {
			cout << "查询到此人:" << "\n\n\t名字:" << per_arr[i].name << "\t电话:" << per_arr[i].tele << endl;
			(*a) = false;
		}
	}
}

//修改联系人
void revise(struct person per_arr[], int * num_add, bool * a) {
	struct person temp[1];
	cin >> temp[0].name;
	for (int i = 0; i < (*num_add); i++) {
		if (temp[0].name == per_arr[i].name) {
			cout << "查询到此人,联系人信息如下:\n\n\t" << "名字:" << per_arr[i].name << "\t电话:" << per_arr[i].tele << endl;
			cout << "\n输入联系人新的电话号码:" << endl;
			cin >> per_arr[i].tele;
			cout << "\n修改成功" << endl;
			(*a) = false;
		}
	}
}

//删除联系人
void delet(struct person per_arr[], int * num_add, bool * a) {
	struct person temp[1];
	cin >> temp[0].name;
	for (int i = 0; i < (*num_add); i++) {
		if (temp[0].name == per_arr[i].name) {
			for (int j = i; j < (*num_add); j++) {
				per_arr[j] = per_arr[j + 1];
			}
			(*num_add)--;
			(*a) = false;
			cout << "删除成功!!!" << endl;
		}
	}
}

//未查询反馈
void search(bool * a) {
	while ((*a)) {
		cout << "未查询到此人" << endl;
		system("pause");
		break;
	}
}

//继续操作
void con(bool * a, bool * b) {
	string select = "n";//清空使用
	cin >> select;
	if (select == "y") {
		cout << "\n请继续" << endl;
		(*a) = true;
	}
	else {
		(*b) = false;
	}
}

//清空通讯录
void clean(struct person per_arr[], int * num_add) {
	string select = "n";//清空使用
	struct person temp[1];
	cin >> select;
	if (select == "y") {
		for (int i = 0; i < (*num_add); i++) {
			per_arr[i] = temp[0];
		}
		(*num_add) = 0;
		cout << endl << "成功清空通讯录!!!" << endl;
	}
	else {
		cout << endl << "已取消" << endl;
	}
}

//打印输出
void print2(int a) {
	switch (a)
	{
		case 1:
		cout << "\n是否继续添加联系人,继续添加请输入 y ,返回菜单请按任意键继续 ..." << endl;
		break;
		
		case 3:
		cout << "\n是否继续删除联系人,继续删除请输入 y ,返回菜单请按任意键继续 ..." << endl;
		break;

		case 4:
		cout << "\n是否继续查找联系人,继续查找请输入 y ,返回菜单请按任意键继续 ..." << endl;
		break;
		
		case 5:
		cout << "\n是否继续修改联系人,继续修改请输入 y ,返回菜单请按任意键继续 ..." << endl;
		break;

		case 6:
		cout << "是否清空通讯录,确认请输入 y ,退出请按任意键继续 ..." << endl;
		break;
	}
}

1.3 通讯录.cpp

#include"add.h"

int num_add = 0;

int main() {
	bool b = true;//判断是否循环
	struct person per_arr[1000];
	struct person temp[1];//删除使用

	while (b) {
		title();
		bool a = true;//判断是否查询到此人
		string select = "n";//清空使用
		int fun = 0;//功能选择

		cout << "请选择功能:" << endl;
		cin >> fun;
		cout << endl;

		switch (fun) {

			case 1://添加联系人
				while (b) {
					if (num_add <= 1000) {
						add_per(per_arr, &num_add);
					}
					print2(1);
					con(&a, &b);
				}
				b = true;
				break;

			case 2://显示联系人
				cout << "联系人列表:" << endl;
				print(per_arr, &num_add);
				cout << endl;
				system("pause");
				break;
				
			case 3://删除联系人
				while (b) {
					cout << "输入要删除联系人的姓名:" << endl;
					delet(per_arr, &num_add, &a);
					search(&a);
					print2(3);
					con(&a, &b);
				}
				b = true;
				break;

			case 4://查找联系人
				while (b) {
					cout << "输入要查找联系人的姓名:" << endl;
					seek(per_arr, &num_add, &a);
					search(&a);
					print2(4);
					con(&a,&b);
				}
				b = true;
				break;

			case 5://修改联系人
				while (b) {
					cout << "输入要修改联系人的姓名:" << endl;
					revise(per_arr, &num_add, &a);
					search(&a);
					print2(5);
					con(&a, &b);
				}
				b = true;
				break;

			case 6://清空联系人
				print2(6);
				clean(per_arr, &num_add);
				system("pause");
				break;

			case 7://退出通讯录
				b = false;
				break;

			default:
				cout << "没有此项功能\n" << endl;
				system("pause");
				break;
			}
	}
	system("pause");
	return 0;
}
这篇关于B站黑马c++学习笔记 —— 通讯录案例的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!