C++通讯录管理系统(适合作为期末作业)

无脑复制,直接提交作业,有问题留言!!!

通讯录的增删改查等操作

#include <iostream>
#include <string>
using namespace std;
#define MAX 1
//通讯录人员信息包括姓名、性别1--男 2--女、年龄、电话、住址 
struct Person {
	string name;
	int sex;
	int age;
	string phone;
	string addr;
};

//通讯录表包括人员信息和大小 
struct List {
	struct Person per[MAX];
	int size;
};

//struct person* p;
//struct List* L;
//菜单列表
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;
}

//添加联系人
void Add_person(struct List* L ) {
	if (L -> size < MAX) {
		cout << "请输入姓名:" << endl;
		string name;
		cin >> name;
		L->per[L->size].name = name;

		cout << "请输入性别1--男 2--女:" << endl;
		int sex;
		cin >> sex;
		while (1) {
			if (sex == 1 || sex == 2) {
				L->per[L->size].sex = sex;
				break;
			}
			else {
				cout << "输入错误!请重新输入:" << endl;
				cin >> sex;
			}
		}

		cout << "请输入年龄:" << endl;
		int age;
		cin >> age;
		L->per[L->size].age = age;

		cout << "请输入电话:" << endl;
		string phone;
		cin >> phone;
		L->per[L->size].phone = phone;

		cout << "请输入住址:" << endl;
		string addr;
		cin >> addr;
		L->per[L->size].addr = addr;

		L->size++;
		cout << "添加成功!" << endl;
	}
	else {
		cout << "通讯录已满,无法添加!" << endl;
	}
	system("pause");
	system("cls");
}

//显示联系人
void show_person(struct List L) {
	if(L.size)
		for (int i = 0; i < L.size; i++) {
			cout << "姓名:" << L.per[i].name << '\t';
			cout << "性别:" << (L.per[i].sex == 1 ? "男" : "女") << '\t';
			cout << "年龄:" << L.per[i].age << '\t';
			cout << "电话:" << L.per[i].phone << '\t';
			cout << "住址:" << L.per[i].addr << '\t';
			cout << endl;
		}
	else {
		cout << "通讯录为空!" << endl;
		}
	system("pause");
	system("cls");
}

//查找联系人
int seek_person(struct List L) {
	string name;
	cout << "请输入要查找的联系人姓名:" ;
	cin >> name;
	for (int i = 0; i < L.size; i++) {
		if (L.per[i].name == name) {
			return i;
		}
	}
	return -1;
}

//修改联系人
void amend_person(struct List* L) {
	string name;
	int n = -1;
	cout << "请输入要修改的联系人姓名:";
	cin >> name;
	for (int i = 0; i < L->size; i++) {
		if (L->per[i].name == name) {
			n = i;
			break;
		}
	}
	if (n == -1) {
		cout << "查无此人,无法修改!" << endl;
	}
	else {
		cout << "请输入修改的姓名:" << endl;
		string name;
		cin >> name;
		L->per[n].name = name;

		cout << "请输入修改的性别1--男 2--女:" << endl;
		int sex;
		cin >> sex;
		while (1) {
			if (sex == 1 || sex == 2) {
				L->per[n].sex = sex;
				break;
			}
			else {
				cout << "输入错误!请重新输入:" << endl;
				cin >> sex;
			}
		}

		cout << "请输入修改的年龄:" << endl;
		int age;
		cin >> age;
		L->per[n].age = age;

		cout << "请输入修改的电话:" << endl;
		string phone;
		cin >> phone;
		L->per[n].phone = phone;

		cout << "请输入修改的住址:" << endl;
		string addr;
		cin >> addr;
		L->per[n].addr = addr;
		cout << "修改成功!" << endl;
	}
	system("pause");
	system("cls");
}

//删除联系人
void delete_person(struct List* L) {
	string name;
	int n = -1;
	cout << "请输入要删除的联系人姓名:";
	cin >> name;
	for (int i = 0; i < L->size; i++) {
		if (L->per[i].name == name) {
			n = i;
			break;
		}
	}
	if(n == -1)
		cout << "查无此人,无法删除!" << endl;
	else {
		for (int i = n; i < L->size; i++) {
			L->per[i] = L->per[i + 1];
		}
		cout << "删除成功!" << endl;
		L->size--;
	}
	system("pause");
	system("cls");
}

//清空通讯录
void clear_List(struct List* L) {
	L->size = 0;
	cout << "通讯录已清空!" << endl;
	system("pause");
	system("cls");
}
int main(int argc, char** argv) {
	char select;
	struct Person p;
	struct List L;
	L.size = 0;
	while (true) {
		showMenu();
		cin >> select;
		switch (select) {
			case '0':
				cout << "欢迎下次使用!" << endl;
				return 0;
			case '1':
				Add_person(&L);
				break;
			case '2':
				show_person(L);
				break;
			case '3': {
				int flag = 0;
				flag = seek_person(L);
				if (flag != -1) {
					cout << "找到了:" << "姓名:" << L.per[flag].name << '\t';
					cout << "性别:" << (L.per[flag].sex == 1 ? "男" : "女") << '\t';
					cout << "年龄:" << L.per[flag].age << '\t';
					cout << "电话:" << L.per[flag].phone << '\t';
					cout << "住址:" << L.per[flag].addr << '\t';
					cout << endl;
					system("pause");
					system("cls");
				}
				else {
					cout << "查无此人!" << endl;
					system("pause");
					system("cls");
				}
				break;
			}
			case '4':
				amend_person(&L);
				break;
			case '5':
				delete_person(&L);
				break;
	lable:	case '6': {
				cout << "您确定清空吗?Y/N" << endl;
				char a;
				cin >> a;
				if(a == 'Y')
					clear_List(&L);
				else if (a == 'N') {
					system("pause");
					system("cls");
					break;
				}
				else {
					cout << "输入错误!请重新输入" << endl;
					goto lable;
				}
			}
	default:
			system("pause");
			system("cls");
		}
	}
	return 0;
}




  • 5
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

@Cabbage

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

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

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

打赏作者

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

抵扣说明:

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

余额充值