c++實現簡單通訊錄系統(黑馬課程)

2020-08-12 22:52:16

程式碼非原創,程式碼非原創,程式碼非原創!發在網上的原因僅僅是學c++時想起了自己大一做課設時候的無助感,所以希望能夠幫助做課設的同學。此程式碼雖然是c++實現,但是很方便就可以改成c語言,如果有人改c語言實現的遇到問題,可以評論或者私聊我。
最後給查程式碼時看到的同學一些建議,此程式碼是江湖救急用的,如果你剛開始課設,那我建議你自己能夠認真思考一下課設如何實現,不管後面你是否選擇程式設計行業,通過課設鞏固一下自己學過的知識,順便瞭解一下程式設計思想也是極好的。
此程式爲黑馬C++免費課程上面的程式碼復現(PS:復現=照着課件敲,個人已經成功執行。),原課程鏈接。如果需要詳細的課件可以去上述鏈接獲取,或者在這篇文章下評論或者私聊我。同時有什麼問題也歡迎大家在評論區討論。

程式碼執行環境:
win10,vs2019.

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

#define MAX 1000 //最大人數

//聯繫人結構體
struct Person
{
	string m_Name; //姓名
	int m_Sex; //性別:1男 2女
	int m_Age; //年齡
	string m_Phone; //電話
	string m_Addr; //住址
};

//通訊錄結構體
struct Addressbooks
{
	struct Person personArray[MAX]; //通訊錄中儲存的聯繫人陣列
	int m_Size; //通訊錄中人員個數
};

//1、新增聯繫人資訊
void addPerson(Addressbooks* abs)
{
	//判斷電話本是否滿了
	if (abs->m_Size == MAX)
	{
		cout << "通訊錄已滿,無法新增" << endl;
		return;
	}
	else
	{
		//姓名
		string name;
		cout << "請輸入姓名:" << endl;
		cin >> name;
		abs->personArray[abs->m_Size].m_Name = name;

		cout << "請輸入性別:" << endl;
		cout << "1 -- 男" << endl;
		cout << "2 -- 女" << endl;

		//性別
		int sex = 0;
		while (true)
		{
			cin >> sex;
			if (sex == 1 || sex == 2)
			{
				abs->personArray[abs->m_Size].m_Sex = sex;
				break;
			}
			cout << "輸入有誤,請重新輸入";
		}

		//年齡
		cout << "請輸入年齡:" << endl;
		int age = 0;
		cin >> age;
		abs->personArray[abs->m_Size].m_Age = age;

		//聯繫電話
		cout << "請輸入聯繫電話:" << endl;
		string phone = "";
		cin >> phone;
		abs->personArray[abs->m_Size].m_Phone = phone;

		//家庭住址
		cout << "請輸入家庭住址:" << endl;
		string address;
		cin >> address;
		abs->personArray[abs->m_Size].m_Addr = address;

		//更新通訊錄人數
		abs->m_Size++;

		cout << "新增成功" << endl;
		system("pause");
		system("cls");
	}
}

//2、顯示所有聯繫人資訊
void showPerson(Addressbooks* abs)
{
	if (abs->m_Size == 0)
	{
		cout << "當前記錄爲空" << endl;
	}
	else
	{
		for (int i = 0; i < abs->m_Size; i++)
		{
			cout << "姓名:" << abs->personArray[i].m_Name << "\t";
			cout << "性別:" << (abs->personArray[i].m_Sex == 1 ? "男" : "女") << "\t";
			cout << "年齡:" << abs->personArray[i].m_Age << "\t";
			cout << "電話:" << abs->personArray[i].m_Phone << "\t";
			cout << "住址:" << abs->personArray[i].m_Addr << endl;
		}
	}

	system("pause");
	system("cls");

}

//判斷是否存在查詢的人員,存在返回在陣列中索引位置,不存在返回-1
int isExist(Addressbooks* abs, string name)
{
	for (int i = 0; i < abs->m_Size; i++)
	{
		if (abs->personArray[i].m_Name == name)
		{
			return i;
		}
	}
	return -1;
}

//3、刪除指定聯繫人資訊
void deletePerson(Addressbooks* abs)
{
	cout << "請輸入您要刪除的聯繫人" << endl;
	string name;
	cin >> name;

	int ret = isExist(abs, name);
	if (ret != -1)
	{
		for (int i = ret; i < abs->m_Size; i++)
		{
			abs->personArray[i] = abs->personArray[i + 1];
		}
		abs->m_Size--;
		cout << "刪除成功" << endl;
	}
	else
	{
		cout << "查無此人" << endl;
	}

	system("pause");
	system("cls");
}

//4、查詢指定聯繫人資訊
void findPerson(Addressbooks* abs)
{
	cout << "請輸入您要查詢的聯繫人" << endl;
	string name;
	cin >> name;

	int ret = isExist(abs, name);
	if (ret != -1)
	{
		cout << "姓名:" << abs->personArray[ret].m_Name << "\t";
		cout << "性別:" << abs->personArray[ret].m_Sex << "\t";
		cout << "年齡:" << abs->personArray[ret].m_Age << "\t";
		cout << "電話:" << abs->personArray[ret].m_Phone << "\t";
		cout << "住址:" << abs->personArray[ret].m_Addr << endl;
	}
	else
	{
		cout << "查無此人" << endl;
	}

	system("pause");
	system("cls");

}
//5、修改指定聯繫人資訊
void modifyPerson(Addressbooks* abs)
{
	cout << "請輸入您要修改的聯繫人" << endl;
	string name;
	cin >> name;

	int ret = isExist(abs, name);
	if (ret != -1)
	{
		//姓名
		string name;
		cout << "請輸入姓名:" << endl;
		cin >> name;
		abs->personArray[ret].m_Name = name;

		cout << "請輸入性別:" << endl;
		cout << "1 -- 男" << endl;
		cout << "2 -- 女" << endl;

		//性別
		int sex = 0;
		while (true)
		{
			cin >> sex;
			if (sex == 1 || sex == 2)
			{
				abs->personArray[ret].m_Sex = sex;
				break;
			}
			cout << "輸入有誤,請重新輸入";
		}

		//年齡
		cout << "請輸入年齡:" << endl;
		int age = 0;
		cin >> age;
		abs->personArray[ret].m_Age = age;

		//聯繫電話
		cout << "請輸入聯繫電話:" << endl;
		string phone = "";
		cin >> phone;
		abs->personArray[ret].m_Phone = phone;

		//家庭住址
		cout << "請輸入家庭住址:" << endl;
		string address;
		cin >> address;
		abs->personArray[ret].m_Addr = address;

		cout << "修改成功" << endl;
	}
	else
	{
		cout << "查無此人" << endl;
	}

	system("pause");
	system("cls");

}
//6、清空所有聯繫人
void cleanPerson(Addressbooks* abs)
{
	abs->m_Size = 0;
	cout << "通訊錄已清空" << endl;
	system("pause");
	system("cls");
}

//選單介面
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;
}

int main() 
{
	int select = 0; //選項

	//建立通訊錄
	Addressbooks abs;
	//初始化通訊錄中人數
	abs.m_Size = 0;

	while (true)
	{
		showMenu();

		cin >> select;

		switch (select)
		{
		case 1:  //新增聯繫人
			addPerson(&abs);
			break;
		case 2:  //顯示聯繫人
			showPerson(&abs);
			break;
		case 3:  //刪除聯繫人
			deletePerson(&abs);
			break;
		case 4:  //查詢聯繫人
			findPerson(&abs);
			break;
		case 5: //修改聯繫人
			modifyPerson(&abs);
			break;
		case 6: //情況聯繫人
			cleanPerson(&abs);
			break;
		case 0: //退出通訊錄
			cout << "歡迎下次使用" << endl;
			system("pause");
			return 0;
			break;
		default:
			cout << "輸入選項錯誤,請重新輸入!" << endl;
			break;
		}
	}
	

	system("pause");

	return 0;
}