1. ホーム
  2. c++

アドレス帳管理システムのC++実装

2022-02-09 17:13:40
<パス

目次

前文

**

手書きでコードを書いて、ポインターの魔法を体験するための提案集です。
知識を定着させ、ポインタ、構造体、関数を併用することで親しみが持てるようになります

**

住所録管理システムを完成させるために必要な知識体系
1. 構造物
2. ポインター
3. 関数のラッピング
4. ポインタと関数の複合的な利用
5. ポインタと構造体の併用

構造

お問い合わせの構成

struct person
{
	string name;//name
	string sex; //gender
	int age; //age
	string phone;//cell phone number
	string home;//address

};

struct addressbook
{
	struct person personArray[MAX]; //addressbook extended to 100.
	int size=0; //number of current contacts (followed by i++)
};


void menu();//menu
void putit(addressbook* add);// add a contact
void showperson(addressbook* add);// show contact
int if_include(addressbook* add, string name);// determine the contact
void deleteperson(addressbook* add, int i);// Delete the contact
void findPerson(addressbook* add);//find a contact
void cleanperson(struct addressbook* add);//clean all contacts

void menu()
{
	cout << endl;
	cout << "**********[Main Menu]************" << endl;
	cout << "---------1. Add contact:----------" << endl;
	cout << "---------2. Show contacts: ---------" << endl;
	cout << "---------3. Delete contact: ---------" << endl;
	cout << "---------4. Find contacts: ---------" << endl;
	cout << "---------5. Modify contact: ---------" << endl;
	cout << "---------6. Empty contacts: ---------" << endl;
	cout << "---------0. Exit Contacts: ---------" << endl;
	cout << "*******************************" << endl;
}

void putit(addressbook* add)//add contact function
{
	if (add->size == MAX)
		cout << "Addressbook is full" << endl;
	else
	{
		string name;
		string sex;
		int age;
		string phone;
		string home;
		cout << "Please enter your name" << endl;
		cin >> name;
		add-> personArray[add-> size].name = name;
		cout << "Please enter last name" << endl;
		cin >> sex;
		add-> personArray[add-> size].sex = sex;
		cout << "Please enter age" << endl;
		cin >> age;
		add-> personArray[add-> size].age = age;
		cout << "Please enter the phone number" << endl;
		cin >> phone;
		add-> personArray[add-> size].phone = phone;
		cout << "Please enter your home address" << endl;
		cin >> home;
		add-> personArray[add-> size].home = home;
		add->size++;
		cout << "Added contact successfully" << endl;
		
	}
	system("pause");
	system("cls");
	menu();
}


アドレス帳の構成


void showperson(addressbook* add)
{
	for (int i = 0; i <add->size; i++)
	{
		cout << "name: " << add-> personArray[i].name;
		cout << "\t last name: " << < add-> personArray[i].sex;
		cout << "\t age: " << < add->personArray[i].age;
		cout << "\t phone number: " << add->personArray[i].phone;
		cout << "\t home address: " << < add->personArray[i].home << endl;
	}
	system("pause");
	system("cls");
	menu();
}

int if_include(addressbook* add, string name)
{
	for (int i = 0; i < add->size; i++)
	{
		if (name == add-> personArray[i].name)
		{
			return i;
		}
		else
		{
			return -1;
		}
	}

}

void deleteperson(addressbook* add, int i)
{
	for (; i < add->size; i++)
	{
		add->personArray[i] = add->personArray[i + 1];
	}
	system("pause");
	system("cls");
}




void findPerson( addressbook* add)
{
	cout << "Please enter the contact you want to find: " << endl;
	string name;
	cin >> name;
	int ret = if_include(add, name);
	if (ret == -1)
	{
		cout << "Check no such person" << endl;
	}
	else
	{ //find the person, display the operation
		int i = ret;
		cout << "name:" << add->personArray[i].name << "\t";
		cout << "gender:" << add->personArray[i].sex << "\t";
		cout << "age:" << add->personArray[i].age << "\t";
		cout << "Contact:" << add->personArray[i].phone << "\t";
		cout << "address:" << < add->personArray[i].home << endl;
	}
	// Press any key to clear the screen
	system("pause");
	system("cls");
}


void cleanperson(struct addressbook* add)//clean all contacts
{
	cout << "Confirm clean? " << endl;
	cout << "1 --- Yes" << endl;
	cout << "2 --- No" << endl;
	int a;
	cin >> a;
	if (a == 1)
	{
		add->size = 0;//set the current number of record contacts to 0, do a logical clear operation
		cout << "Address book is empty" << endl;
	}
	system("pause");
	system("cls");
}

int main()
{
	menu();
	addressbook add;//Define an addressbook
	int choice=1;
	while (choice ! = 0)
	{
		cin >> choice;
		switch (choice)//choice
		{
		case 1: putit(&add);
			break;
		case 2: showperson(&add);
			break;
		case 3: {cout << "Please enter the name of the person you want to delete" << endl;
			string aname;
			cin >> aname;
			if (if_include(&add, aname) == -1)
			{
				cout << "Check no such person" << endl;
				break;
			}
			if (if_include(&add, aname))
			{
				deleteperson(&add, if_include(&add, aname));
			}
		}
			break; 
		case 4:
			break;
		case 5:
			break;
		case 6:
			break;
		case 0: cout << "Welcome to next time" << endl;
			return 0;
			break;
		default: {cout << "Input not legal, please re-enter" << endl;
			break; }
		}
	}
}

#include

using namespace std;
#include

#define MAX 100

struct person
{
	string name;//name
	string sex; //gender
	int age; //age
	string phone;//cell phone number
	string home;//address

};
struct addressbook
{
	struct person personArray[MAX]; //addressbook extended to 100.
	int size=0; //the number of current contacts (the latter is equivalent to i++)
};
void menu();//menu
void putit(addressbook* add);
void showperson(addressbook* add); 
int if_include(addressbook* add, string name);
void deleteperson(addressbook* add, int i);
void findPerson(struct addressbooks* add);//find a contact
void cleanperson(struct addressbook* add);//clean all contacts
int main()
{
	menu();
	addressbook add;
	int choice=1;
	while (choice ! = 0)
	{
		cin >> choice;
		switch (choice)
		{
		case 1: putit(&add);
			break;
		case 2: showperson(&add);
			break;
		case 3: {
			cout << "Please enter the name of the person you want to delete" << endl;
			string aname;
			cin >> aname;
			if (if_include(&add, aname) == -1)
			{
				cout << "Check no such person" << endl;
				break;
			}
			if (if_include(&add, aname))
			{
				deleteperson(&add, if_include(&add, aname));
			}
		}
			break; 
		case 4:
			break;
		case 5:
			break;
		case 6:
			break;
		case 0: cout << "Welcome to next time" << endl;
			return 0;
			break;
		default: {cout << "Input not legal, please re-enter" << endl;
			break; }
		}
	}
}
void menu()
{
	cout << endl;
	cout << "***********[Main Menu] ***********" << endl;
	cout << "---------1. Add contact:----------" << endl;
	cout << "---------2. Show contacts: ---------" << endl;
	cout << "---------3. Delete contact: ---------" << endl;
	cout << "---------4. Find contacts: ---------" << endl;
	cout << "---------5. Modify contact: ---------" << endl;
	cout << "---------6. Empty contacts: ---------" << endl;
	cout << "---------0. Exit Contacts: ---------" << endl;
	cout << "********************************" << endl;
}
void putit(addressbook* add)//add contact function
{
	if (add->size == MAX)
		cout << "Addressbook is full" << endl;
	else
	{
		string name;
		string sex;
		int age;
		string phone;
		string home;
		cout << "Please enter your name" << endl;
		cin >> name;
		add-> personArray[add-> size].name = name;
		cout << "Please enter last name" << endl;
		cin >> sex;
		add-> personArray[add-> size].sex = sex;
		cout << "Please enter age" << endl;
		cin >> age;
		add-> personArray[add-> size].age = age;
		cout << "Please enter the phone number" << endl;
		cin >> phone;
		add-> personArray[add-> size].phone = phone;
		cout << "Please enter your home address" << endl;
		cin >> home;
		add-> personArray[add-> size].home = home;
		add->size++;
		cout << "Added contact successfully" << endl;
		
	}
	system("pause");
	system("cls");
	menu();
}
void showperson(addressbook* add)
{
	for (int i = 0; i <add->size; i++)
	{
		cout << "name: " << add-> personArray[i].name;
		cout << "\t last name: " << < add-> personArray[i].sex;
		cout << "\t age: " << < add->personArray[i].age;
		cout << "\t phone number: " << add->personArray[i].phone;
		cout << "\t home address: " << < add->personArray[i].home << endl;
	}
	system("pause");
	system("cls");
	menu();
}
int if_include(addressbook* add, string name)
{
	for (int i = 0; i < add-> size; i++)
	{
		if (name == add-> personArray[i].name)
		{
			return i;
		}
		else
		{
			return -1;
		}
	}

}
void deleteperson(addressbook* add, int i)
{
	for (; i < add->size; i++)
	{
		add->personArray[i] = add->personArray[i + 1];
	}
	system("pause");
	system("cls");
}

void findPerson( addressbook* add)
{
	cout << "Please enter the contact you want to find:" << endl;
	string name;
	cin >> name;
	int ret = if_include(add, name);
	if (ret == -1)
	{
		cout << "Check no such person" << endl;
	}
	else
	{ //find the person, display the operation
		int i = ret;
		cout << "name:" << add->personArray[i].name << "\t";
		cout << "gender:" &l

機能モジュール

void menu();//menu
void putit(addressbook* add);// add a contact
void showperson(addressbook* add);// show contact
int if_include(addressbook* add, string name);// determine the contact
void deleteperson(addressbook* add, int i);// Delete the contact
void findPerson(addressbook* add);//find a contact
void cleanperson(struct addressbook* add);//clean all contacts


メニュー

void menu()
{
	cout << endl;
	cout << "**********[Main Menu]************" << endl;
	cout << "---------1. Add contact:----------" << endl;
	cout << "---------2. Show contacts: ---------" << endl;
	cout << "---------3. Delete contact: ---------" << endl;
	cout << "---------4. Find contacts: ---------" << endl;
	cout << "---------5. Modify contact: ---------" << endl;
	cout << "---------6. Empty contacts: ---------" << endl;
	cout << "---------0. Exit Contacts: ---------" << endl;
	cout << "*******************************" << endl;
}


連絡先の追加

void putit(addressbook* add)//add contact function
{
	if (add->size == MAX)
		cout << "Addressbook is full" << endl;
	else
	{
		string name;
		string sex;
		int age;
		string phone;
		string home;
		cout << "Please enter your name" << endl;
		cin >> name;
		add-> personArray[add-> size].name = name;
		cout << "Please enter last name" << endl;
		cin >> sex;
		add-> personArray[add-> size].sex = sex;
		cout << "Please enter age" << endl;
		cin >> age;
		add-> personArray[add-> size].age = age;
		cout << "Please enter the phone number" << endl;
		cin >> phone;
		add-> personArray[add-> size].phone = phone;
		cout << "Please enter your home address" << endl;
		cin >> home;
		add-> personArray[add-> size].home = home;
		add->size++;
		cout << "Added contact successfully" << endl;
		
	}
	system("pause");
	system("cls");
	menu();
}


コンタクト追加機能でポインターを使用する理由についての質問がありました。
なぜなら、値渡しのフォーム・パラメータは、実際のパラメータを変更することができないからです。
一方、ポインタを使ったアドレス渡しでは、関数内の形式参照を通じて実参照を変更することができます。
正確な原理は、ブログ主が以前に紹介した ポインターの基礎とポインターの応用コンテンツ ]

連絡先を表示する


void showperson(addressbook* add)
{
	for (int i = 0; i <add->size; i++)
	{
		cout << "name: " << add-> personArray[i].name;
		cout << "\t last name: " << < add-> personArray[i].sex;
		cout << "\t age: " << < add->personArray[i].age;
		cout << "\t phone number: " << add->personArray[i].phone;
		cout << "\t home address: " << < add->personArray[i].home << endl;
	}
	system("pause");
	system("cls");
	menu();
}


連絡先を決定する

int if_include(addressbook* add, string name)
{
	for (int i = 0; i < add->size; i++)
	{
		if (name == add-> personArray[i].name)
		{
			return i;
		}
		else
		{
			return -1;
		}
	}

}


コンタクトの削除

void deleteperson(addressbook* add, int i)
{
	for (; i < add->size; i++)
	{
		add->personArray[i] = add->personArray[i + 1];
	}
	system("pause");
	system("cls");
}





連絡先を探す

void findPerson( addressbook* add)
{
	cout << "Please enter the contact you want to find: " << endl;
	string name;
	cin >> name;
	int ret = if_include(add, name);
	if (ret == -1)
	{
		cout << "Check no such person" << endl;
	}
	else
	{ //find the person, display the operation
		int i = ret;
		cout << "name:" << add->personArray[i].name << "\t";
		cout << "gender:" << add->personArray[i].sex << "\t";
		cout << "age:" << add->personArray[i].age << "\t";
		cout << "Contact:" << add->personArray[i].phone << "\t";
		cout << "address:" << < add->personArray[i].home << endl;
	}
	// Press any key to clear the screen
	system("pause");
	system("cls");
}



すべての連絡先を消去する

void cleanperson(struct addressbook* add)//clean all contacts
{
	cout << "Confirm clean? " << endl;
	cout << "1 --- Yes" << endl;
	cout << "2 --- No" << endl;
	int a;
	cin >> a;
	if (a == 1)
	{
		add->size = 0;//set the current number of record contacts to 0, do a logical clear operation
		cout << "Address book is empty" << endl;
	}
	system("pause");
	system("cls");
}


主な機能

int main()
{
	menu();
	addressbook add;//Define an addressbook
	int choice=1;
	while (choice ! = 0)
	{
		cin >> choice;
		switch (choice)//choice
		{
		case 1: putit(&add);
			break;
		case 2: showperson(&add);
			break;
		case 3: {cout << "Please enter the name of the person you want to delete" << endl;
			string aname;
			cin >> aname;
			if (if_include(&add, aname) == -1)
			{
				cout << "Check no such person" << endl;
				break;
			}
			if (if_include(&add, aname))
			{
				deleteperson(&add, if_include(&add, aname));
			}
		}
			break; 
		case 4:
			break;
		case 5:
			break;
		case 6:
			break;
		case 0: cout << "Welcome to next time" << endl;
			return 0;
			break;
		default: {cout << "Input not legal, please re-enter" << endl;
			break; }
		}
	}
}


ソースコード

#include

using namespace std;
#include

#define MAX 100

struct person
{
	string name;//name
	string sex; //gender
	int age; //age
	string phone;//cell phone number
	string home;//address

};
struct addressbook
{
	struct person personArray[MAX]; //addressbook extended to 100.
	int size=0; //the number of current contacts (the latter is equivalent to i++)
};
void menu();//menu
void putit(addressbook* add);
void showperson(addressbook* add); 
int if_include(addressbook* add, string name);
void deleteperson(addressbook* add, int i);
void findPerson(struct addressbooks* add);//find a contact
void cleanperson(struct addressbook* add);//clean all contacts
int main()
{
	menu();
	addressbook add;
	int choice=1;
	while (choice ! = 0)
	{
		cin >> choice;
		switch (choice)
		{
		case 1: putit(&add);
			break;
		case 2: showperson(&add);
			break;
		case 3: {
			cout << "Please enter the name of the person you want to delete" << endl;
			string aname;
			cin >> aname;
			if (if_include(&add, aname) == -1)
			{
				cout << "Check no such person" << endl;
				break;
			}
			if (if_include(&add, aname))
			{
				deleteperson(&add, if_include(&add, aname));
			}
		}
			break; 
		case 4:
			break;
		case 5:
			break;
		case 6:
			break;
		case 0: cout << "Welcome to next time" << endl;
			return 0;
			break;
		default: {cout << "Input not legal, please re-enter" << endl;
			break; }
		}
	}
}
void menu()
{
	cout << endl;
	cout << "***********[Main Menu] ***********" << endl;
	cout << "---------1. Add contact:----------" << endl;
	cout << "---------2. Show contacts: ---------" << endl;
	cout << "---------3. Delete contact: ---------" << endl;
	cout << "---------4. Find contacts: ---------" << endl;
	cout << "---------5. Modify contact: ---------" << endl;
	cout << "---------6. Empty contacts: ---------" << endl;
	cout << "---------0. Exit Contacts: ---------" << endl;
	cout << "********************************" << endl;
}
void putit(addressbook* add)//add contact function
{
	if (add->size == MAX)
		cout << "Addressbook is full" << endl;
	else
	{
		string name;
		string sex;
		int age;
		string phone;
		string home;
		cout << "Please enter your name" << endl;
		cin >> name;
		add-> personArray[add-> size].name = name;
		cout << "Please enter last name" << endl;
		cin >> sex;
		add-> personArray[add-> size].sex = sex;
		cout << "Please enter age" << endl;
		cin >> age;
		add-> personArray[add-> size].age = age;
		cout << "Please enter the phone number" << endl;
		cin >> phone;
		add-> personArray[add-> size].phone = phone;
		cout << "Please enter your home address" << endl;
		cin >> home;
		add-> personArray[add-> size].home = home;
		add->size++;
		cout << "Added contact successfully" << endl;
		
	}
	system("pause");
	system("cls");
	menu();
}
void showperson(addressbook* add)
{
	for (int i = 0; i <add->size; i++)
	{
		cout << "name: " << add-> personArray[i].name;
		cout << "\t last name: " << < add-> personArray[i].sex;
		cout << "\t age: " << < add->personArray[i].age;
		cout << "\t phone number: " << add->personArray[i].phone;
		cout << "\t home address: " << < add->personArray[i].home << endl;
	}
	system("pause");
	system("cls");
	menu();
}
int if_include(addressbook* add, string name)
{
	for (int i = 0; i < add-> size; i++)
	{
		if (name == add-> personArray[i].name)
		{
			return i;
		}
		else
		{
			return -1;
		}
	}

}
void deleteperson(addressbook* add, int i)
{
	for (; i < add->size; i++)
	{
		add->personArray[i] = add->personArray[i + 1];
	}
	system("pause");
	system("cls");
}

void findPerson( addressbook* add)
{
	cout << "Please enter the contact you want to find:" << endl;
	string name;
	cin >> name;
	int ret = if_include(add, name);
	if (ret == -1)
	{
		cout << "Check no such person" << endl;
	}
	else
	{ //find the person, display the operation
		int i = ret;
		cout << "name:" << add->personArray[i].name << "\t";
		cout << "gender:" &l

<ブロッククオート

コード量が多いので、コードを別のファイルに分けて書いてみることをお勧めします。

実行結果



ブックマークと同じように、何度も繰り返し練習する