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;
}