全てをまとめる
CoddyのCジャーニー「論理と制御フロー」セクションの一部。レッスン 54/63。
チャレンジ
簡単前のレッスンで作成したすべての関数を統合して、連絡先管理システムを完成させてください。プログラムは次の要件を満たす必要があります。
- 前のレッスンと同じ
Contactstruct 定義を使用し、次のメンバーを含める。- サイズ50の文字配列
name - サイズ20の文字配列
phone - サイズ40の文字配列
email - 整数型の
age
- サイズ50の文字配列
- 前のレッスンと同じ
createContact関数を使用し、次の処理を行う。Contactstruct 用のメモリを動的に確保する- すべてのメンバーをデフォルト値で初期化する
- 連絡先へのポインターを返す
- 前のレッスンと同じ
populateContact関数を使用し、次の処理を行う。- パラメーターとして
Contactstruct へのポインターを受け取る - ユーザー入力を促し、すべての連絡先フィールドに値を設定する
- 入力データを検証する
- パラメーターとして
displayContact関数を作成し、次の処理を行う。- パラメーターとして
Contactstruct への定数ポインターを受け取る - 各フィールドを、次の正確な形式でそれぞれ1行に出力する。
Name: John
Phone: 555-1234
Email: john@email.com
Age: 25
- パラメーターとして
updateContactという名前の関数を作成し、次の処理を行う。- パラメーターとして
Contactstruct へのポインターを受け取る - 何も返さない(void)
- 次の内容を出力して、更新するフィールドをユーザーに選択させる。
What would you like to update?1. Name2. Phone3. Email4. AgeEnter choice (1-4):
- ユーザーの選択を読み取り、対応するフィールドを更新する。
- choice が1の場合:
Enter new name:を出力し、name を更新する - choice が2の場合:
Enter new phone:を出力し、phone を更新する - choice が3の場合:
Enter new email:を出力し、email を更新する - choice が4の場合:
Enter new age:を出力し、age を更新する - choice が無効な場合(1~4以外):
Invalid choiceを出力する
- choice が1の場合:
- 更新に成功した後、
Contact updated successfullyを出力する
- パラメーターとして
- main 関数で、完全な連絡先管理ワークフローを実装する。
createContact関数を使用して連絡先を作成する- 連絡先の作成に成功したか確認する
populateContact関数を呼び出して、Initial の連絡先データを入力する--- Initial Contact ---を出力するdisplayContact関数を呼び出して、Initial の連絡先情報を表示する--- Updating Contact ---を出力するupdateContact関数を呼び出して、1つのフィールドを変更する--- Updated Contact ---を出力するdisplayContact関数を再度呼び出して、更新された連絡先情報を表示する- 動的に確保したメモリを解放する
Contact management completedを出力する
注: このレッスンの displayContact 関数では、シンプルな形式を使用します。各フィールドを追加の詳細や単位なしで1行に出力します(たとえば、年齢は"25 years old"ではなく、単なる数値として出力されます)。テストケースはこの形式に依存しているため、出力が上記の形式と完全に一致するようにしてください。
この最後のチャレンジでは、連絡先管理システムのすべての構成要素を組み合わせます。struct 定義、動的メモリ割り当て、データ入力と検証、整形された表示、データの変更を扱います。作成からクリーンアップまで、動的に割り当てられた struct の完全なライフサイクルを実装しながら、関数の整理と、処理全体を通したポインターの適切な使用方法を実践します。
自分で試してみよう
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Contact {
char name[50];
char phone[20];
char email[40];
int age;
};
struct Contact* createContact() {
struct Contact* contact = (struct Contact*)malloc(sizeof(struct Contact));
if (contact != NULL) {
strcpy(contact->name, "");
strcpy(contact->phone, "");
strcpy(contact->email, "");
contact->age = 0;
}
return contact;
}
void populateContact(struct Contact* contactPtr) {
printf("Enter name: ");
fgets(contactPtr->name, sizeof(contactPtr->name), stdin);
contactPtr->name[strcspn(contactPtr->name, "\n")] = '\0';
printf("Enter phone: ");
fgets(contactPtr->phone, sizeof(contactPtr->phone), stdin);
contactPtr->phone[strcspn(contactPtr->phone, "\n")] = '\0';
printf("Enter email: ");
fgets(contactPtr->email, sizeof(contactPtr->email), stdin);
contactPtr->email[strcspn(contactPtr->email, "\n")] = '\0';
printf("Enter age: ");
scanf("%d", &contactPtr->age);
}
void displayContact(const struct Contact *contactPtr) {
printf("=== CONTACT DETAILS ===\n");
printf("Name: %s\n", contactPtr->name);
printf("Phone: %s\n", contactPtr->phone);
printf("Email: %s\n", contactPtr->email);
printf("Age: %d years old\n", contactPtr->age);
printf("========================\n");
printf("Name length: %lu characters\n", strlen(contactPtr->name));
if (contactPtr->age >= 0 && contactPtr->age <= 12) {
printf("Generation: Child\n");
} else if (contactPtr->age >= 13 && contactPtr->age <= 19) {
printf("Generation: Teenager\n");
} else if (contactPtr->age >= 20 && contactPtr->age <= 39) {
printf("Generation: Young Adult\n");
} else if (contactPtr->age >= 40 && contactPtr->age <= 59) {
printf("Generation: Middle-aged Adult\n");
} else if (contactPtr->age >= 60) {
printf("Generation: Senior\n");
}
if (strchr(contactPtr->email, '@') != NULL) {
printf("Email format: Valid\n");
} else {
printf("Email format: Invalid\n");
}
}
int main() {
struct Contact* contact = createContact();
if (contact != NULL) {
populateContact(contact);
displayContact(contact);
free(contact);
printf("Program completed successfully\n");
}
return 0;
}論理と制御フローのすべてのレッスン
自分で練習してみよう: Cオンラインコンパイラ