一个简单的通讯录管理系统--C++

C++编写一个简单的通讯录管理系统,为了完成黑马课程的小作业

功能说明:添加联系人、显示联系人、删除联系人、修改联系人、查找联系人、清空联系人、退出

demo1.cpp

// demo1.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//添加联系人、显示联系人、删除联系人、修改联系人、查找联系人、清空联系人、退出

#include <iostream>
#include <math.h>
#include <string.h>
#include "Structs.h"
#include "Deploy.h"
using namespace std;

int main()
{
	constexpr int arrayLen = 10;
	Person arrayContent[arrayLen];
	Person *pCurContent = arrayContent;
	int mode;

	do
	{
		cout << "**********" << endl;
		cout << "1.添加" << endl << "2.显示" << endl << "3.删除" << endl
			<< "4.修改" << endl << "5.查找" << endl << "6.清空" << endl
			<< "0.退出" << endl;
		cout << "**********" << endl;
		cin >> mode;
		switch (mode)
		{
		case 1:pCurContent = addContent(arrayContent, arrayLen, pCurContent);
			break;
		case 2:displayContent(arrayContent, arrayLen, pCurContent);
			break;
		case 3:pCurContent = removeContent(arrayContent, arrayLen, pCurContent);
			break;
		case 4:updateContent(arrayContent, arrayLen, pCurContent);
			break;
		case 5:findContent(arrayContent, arrayLen, pCurContent);
			break;
		case 6:pCurContent = clearContent(arrayContent, arrayLen);
			break;
		default :break;
		}
	} while (mode>0);
}

Structs.h

#pragma once
#include <string>
using namespace std;
struct Person
{
	string name;
	int age;
	string address;
};

Deploy.h

#pragma once
#include "Structs.h"
Person* addContent(Person arrayContent[], int len, Person *pCurContent);
bool displayContent(Person arrayContent[], int len, Person* pCurContent);
Person* removeContent(Person arrayContent[], int len, Person* pCurContent);
bool updateContent(Person arrayContent[], int len, Person* pCurContent);
Person* findContent(Person arrayContent[], int len, Person* pCurContent, string name="");
Person* clearContent(Person arrayContent[], int len);

Deploy.cpp

#include "Deploy.h"
#include <iostream>
Person* addContent(Person arrayContent[], int len, Person* pCurContent)
{
	if (pCurContent < &arrayContent[len])
	{
		string name;
		int age;
		string address;
		cout << "please input name:";
		cin >> name;
		cout << "please input age:";
		cin >> age;
		cout << "please input address:";
		cin >> address;
		pCurContent->name = name;
		pCurContent->age = age;
		pCurContent->address = address;
		pCurContent = pCurContent + 1;
		return pCurContent;
	}
	else
	{
		cout << "invalid addition" << endl;
		return NULL;
	}
}

bool displayContent(Person arrayContent[], int len, Person* pCurContent)
{
	Person* pBegin = arrayContent;
	if (pCurContent != NULL)
	{
		while (pBegin < pCurContent)
		{
			cout << pBegin->name << "\t" << pBegin->age << "\t" << pBegin->address << endl;
			pBegin++;
		}
	}
	return true;
}

Person* removeContent(Person arrayContent[], int len, Person* pCurContent)
{
	string name;
	cout << "Please input the person\'name you want to remove:";
	cin >> name;
	Person* pRemoved = findContent(arrayContent, len, pCurContent, name);
	if (pRemoved == NULL)
	{
		cout << "The content does not contain the person!!" << endl;
	}
	else
	{
		while (pRemoved < pCurContent - 1)
		{
			pRemoved->address = (pRemoved + 1)->address;
			pRemoved->age = (pRemoved + 1)->age;
			pRemoved->name = (pRemoved + 1)->name;
			pRemoved++;
		}
		pCurContent->address = "";
		pCurContent->age = 0;
		pCurContent->name = "";
		pCurContent--;
	}
	return pCurContent;
}

bool updateContent(Person arrayContent[], int len, Person* pCurContent)
{
	string name;
	cout << "Please input the person\'name you want to remove:";
	cin >> name;
	Person* pUpdate = findContent(arrayContent, len, pCurContent, name);
	int chmode;
	cout << "1.change address\t2.change age" << endl;
	cin >> chmode;
	if (chmode == 1)
	{
		cout << "input new address:";
		cin >> pUpdate->address;
		cout << "change successfully" << endl;
	}
	else
	{
		cout << "input new age:";
		cin >> pUpdate->age;
		cout << "change successfully" << endl;
	}
	return true;
}

Person* findContent(Person arrayContent[], int len, Person* pCurContent, string name)
{
	if (name == "")
	{
		cout << "Please input the person\'name you want to find:";
		cin >> name;
	}
	Person* pBegin = arrayContent;
	while (pBegin < pCurContent)
	{
		if (pBegin->name == name)
		{
			cout << pBegin->name << "\t" << pBegin->age << "\t" << pBegin->address << endl;
			return pBegin;
		}
		pBegin++;
	}
	return NULL;
}

Person* clearContent(Person arrayContent[], int len)
{
	for (int i = 0; i < len; i++)
	{
		arrayContent[i].address = "";
		arrayContent[i].age = 0;
		arrayContent[i].name = "";
	}
	return arrayContent;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值