C++:通讯录

一、背景

经过一段时间的学习,c语言已经完成进阶,遥记我的第一篇博客,写的便是c语言下的通讯录,现在看来是那么稚嫩,增删查改只做到了增和查,但我仍是喜悦的,因为对比本篇和第一篇,我看到了我的巨大的进步

那么今天,就让我们看看c++下的较为完备的通讯录吧!

二、代码

我们用了双向链表作为容器,具体代码如下:

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

struct Node {//链表节点
	Node* pre;
	string name;
	string number;
	Node* next;
	Node(string name, string number) :pre(nullptr),name(name), number(number), next(nullptr) {};
};

//通讯录链表类
class AddressBook {
private:
	Node* phead;

public:
	AddressBook() :phead(nullptr) {};

	//尾插
	void append(string name,string number) {
		Node* newnode = new Node(name, number);
		if (phead == nullptr)
			phead = newnode;
		else {
			Node* current = phead;
			while (current->next != nullptr)
				current = current->next;
			current->next = newnode;
			newnode->pre = current;
		}
	}
	//查找联系人
	Node* find(string name) {
		Node* current = phead;
		while (current != nullptr && name.compare(current->name) != 0) {
			current = current->next;
		}
		if (current == nullptr) {
			cout << name << " not found" << endl;
			return nullptr;
		}
		else return current;
	}
	//修改联系人
	void change(string name) {
		Node* pf = find(name);
		cout << "请输入新的姓名:";
		cin >> pf->name;
		cout << endl;
		cout << "请输入新的号码:";
		cin >> pf->number;
	}
	//删除联系人
	void pop(string name) {
		Node* pf = find(name);
		if (pf == nullptr) return;
		else {
			Node* temp;
			temp = pf;
			pf->pre->next = pf->next;
			pf->next->pre = pf->pre;
			delete pf;
		}
	}

	//打印
	void print() {
		Node* current = phead;
		while (current != nullptr) {
			cout << "Name:" << current->name << " Number:" << current->number << endl;
			current = current->next;
		}
	}
};
//打印菜单
void PrintMenu() {
	cout << "******************************************" << endl;
	cout << "*******1.添加联系人**2.删除联系人*******" << endl;
	cout << "*******3.查找联系人**4.修改联系人*******" << endl;
	cout << "*******5.打印通讯录**6.退出通讯录*******" << endl;
	cout << "******************************************" << endl;
}
int main() {
	AddressBook a1;
    //简单初始化
	a1.append("lisi", "13900000000");
	a1.append("wangwu", "15900000000");
	a1.append("zhangsan", "12300000000");
	while (true) {
		system("cls");
		PrintMenu();
		int choice;
		cin >> choice;
		switch (choice) {
		case 1://添加
		{
			string newname;
			string newnumber;
			cout << "请输入添加联系人的姓名:";
			cin >> newname;
			cout << endl;
			cout << "请输入添加联系人的号码:";
			cin >> newnumber;
			a1.append(newname, newnumber);
			break;
		}
		case 2://删除
		{
			string name;
			cout << "请输入要删除联系人的姓名:" ;
			cin >> name;
			a1.pop(name);
			break;
		}
		case 3://查找
		{
			string name;
			cout << "请输入要查找联系人的姓名:" ;
			cin >> name;
			Node* pf = a1.find(name);
			if (pf != nullptr)
				cout << "Number:" << pf->number << endl;
			system("pause");
			break;
		}
		case 4://修改
		{
			string name;
			cout << "请输入要修改联系人的姓名:" ;
			cin >> name;
			a1.change(name);
			break;
		}
		case 5://打印
			a1.print();
			system("pause");
			break;
		case 6://退出
			cout << "欢迎下次使用!"<<endl;
			system("pause");
			return 0;
			break;
		default:
			cout << "选择出错,请重新选择" << endl;
			system("pause");
			break;
		}
	}
	system("pause");
	return 0;
}

 代码我同样放在了我的gitee上,感兴趣可以自行下载:

李俊伟 (lijunwei5980) - Gitee.com

感谢观看~

  • 15
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

冬有雪的学习之路

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值