アドレス帳管理システムの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
<ブロッククオート
コード量が多いので、コードを別のファイルに分けて書いてみることをお勧めします。
実行結果
ブックマークと同じように、何度も繰り返し練習する
関連
-
[解決済み] error: 'if' の前に unqualified-id を期待した。
-
[解決済み】クラスのコンストラクタへの未定義参照、.cppファイルの修正も含む
-
[解決済み】警告 - 符号付き整数式と符号なし整数式の比較
-
[解決済み】"...... "のプロトタイプがクラス"...... "内のどれとも一致しない。
-
[解決済み] C++ - 「Incomplete type not allowed」エラーの意味と修正方法について教えてください。
-
[解決済み] ファイルMSVCP100D.dllがコンピュータから消えている
-
[解決済み] C++11でスレッドオブジェクトの配列を作成する方法は?
-
[解決済み] メッセージボックスで変数を表示する c++
-
[解決済み] include を使用しているにもかかわらず、未定義のクラスを使用している
-
[解決済み] PreprocessorDefinitionsの設定にVSマクロを使用する
最新
-
nginxです。[emerg] 0.0.0.0:80 への bind() に失敗しました (98: アドレスは既に使用中です)
-
htmlページでギリシャ文字を使うには
-
ピュアhtml+cssでの要素読み込み効果
-
純粋なhtml + cssで五輪を実現するサンプルコード
-
ナビゲーションバー・ドロップダウンメニューのHTML+CSSサンプルコード
-
タイピング効果を実現するピュアhtml+css
-
htmlの選択ボックスのプレースホルダー作成に関する質問
-
html css3 伸縮しない 画像表示効果
-
トップナビゲーションバーメニュー作成用HTML+CSS
-
html+css 実装 サイバーパンク風ボタン
おすすめ
-
[解決済み】デバッグアサーションに失敗しました。C++のベクトル添え字が範囲外
-
[解決済み】mpz使用中にtcache 2でダブルフリーが検出されたとはどういう意味ですか?
-
[解決済み】C++ライブラリにmedian関数はありますか?
-
[解決済み】"Invalid operands to binary expression "エラーを修正する方法は?
-
[解決済み] 1回線で複数の入力が可能
-
[解決済み] C++警告 C4018: '<' : 符号化/非符号化の不一致 [重複].
-
[解決済み] c++の宣言が矛盾している
-
[解決済み] 文字列定数から'char*'への変換がCでは有効だが、C++では無効なのはなぜか?
-
[解決済み] 名前空間 'std' の 'vector' が型名でない
-
[解決済み] strcpy' と 'strcpy_s' の違い?