ソリューション・エクスプローラー構成
bank.c
#include <stdio.h> #include <string.h> #include <stdlib.h> /* 個人情報構造体の宣言 */ struct private_info { int amount; // 残高 int password; // パスワード }; /* 個人情報構造体の外部変数の定義 */ struct private_info info = { 10000, 1234 }; /* 関数プロトタイプ (bank_common.h) */ void insert_card(void); void enter_password(void); void show_amount(void); /* 関数プロトタイプ (bank_operation.h) */ void deposit(void); void withdraw(void); void check(void); /* メイン関数 */ int main(void) { char buff[2]; printf("ご利用内容を選択してください [ 預入:d, 引出:w, 照会:s, 終了:f ]\n>>"); while (fgets(buff, sizeof(buff), stdin) != NULL) { if (strcmp(buff, "d") == 0) { deposit(); // 預入 } else if (strcmp(buff, "w") == 0) { withdraw(); // 引出 } else if (strcmp(buff, "s") == 0) { check(); // 照会 } else if (strcmp(buff, "f") == 0) { break; } printf("ご利用内容を選択してください [ 預入:d, 引出:w, 照会:s, 終了:f ]\n>>"); rewind(stdin); } return EXIT_SUCCESS; } /***************** * bank_common.c *****************/ /* カード挿入関数 */ void insert_card(void) { printf("カードが挿入されました\n"); } /* パスワード入力関数 */ void enter_password(void) { char pass[5]; printf("パスワードを入力してださい\n>>"); rewind(stdin); while (fgets(pass, sizeof(pass), stdin) != NULL) { if (info.password != atoi(pass)) { printf("パスワードが間違っています.もう一度入力してください\n>>"); rewind(stdin); } else { break; } } printf("認証に成功しました\n"); } /* 残高表示関数 */ void show_amount(void) { printf("現在の残高は %d 円です\n", info.amount); } /******************** * bank_operation.c ********************/ /* 預入関数 */ void deposit(void) { char buff[128]; int dep; insert_card(); // カード挿入 printf("預ける金額を入力してください\n>>"); rewind(stdin); while (fgets(buff, sizeof(buff), stdin) != NULL) { dep = atoi(buff); if (dep < 0) { printf("不正な金額です.再度,預ける金額を入力してください\n>>"); } else { info.amount += dep; break; } } show_amount(); // 残高表示 } /* 引出関数 */ void withdraw(void) { char buff[128]; int dep; insert_card(); // カード挿入 enter_password(); // パスワード入力 printf("引き出す金額を入力してください\n>>"); rewind(stdin); while (fgets(buff, sizeof(buff), stdin) != NULL) { dep = atoi(buff); if ((dep < 0) || (dep > info.amount)) { printf("不正な金額です.再度,引き出す金額を入力してください\n>>"); } else { info.amount -= dep; break; } } show_amount(); // 残高表示 } /* 残高照会関数 */ void check(void) { insert_card(); // カード挿入 enter_password(); // パスワード入力 show_amount(); // 残高表示 }
ソリューション・エクスプローラー構成
bank.c #include <stdio.h> #include <string.h> #include <stdlib.h> #include "bank_common.h" /* 関数プロトタイプ (bank_operation.h) */ void deposit(void); void withdraw(void); void check(void); /* メイン関数 */ int main(void) { // 変更なし } /******************** * bank_operation.c ********************/ /* 預入関数 */ void deposit(void) { // 変更なし } /* 引出関数 */ void withdraw(void) { // 変更なし } /* 残高照会関数 */ void check(void) { // 変更なし } |
|
bank_common.h #include <stdio.h> #include <stdlib.h> /* 個人情報構造体の宣言 */ struct private_info { int amount; // 残高 int password; // パスワード }; /* 個人情報構造体の外部変数の定義 */ struct private_info info = {10000, 1234}; /* 関数プロトタイプ (bank_common.h) */ void insert_card(void); void enter_password(void); void show_amount(void); |
bank_common.c #include "bank_common.h" /***************** * bank_common.c *****************/ /* カード挿入関数 */ void insert_card(void) { // 変更なし } /* パスワード入力関数 */ void enter_password(void) { // 変更なし } /* 残高表示関数 */ void show_amount(void) { // 変更なし } |
bank.c #include <stdio.h> #include <string.h> #include <stdlib.h> #include "bank_common.h" /* 個人情報構造体の外部変数の定義 */ struct private_info info = {10000, 1234}; /* 関数プロトタイプ (bank_operation.h) */ void deposit(void); void withdraw(void); void check(void); /* メイン関数 */ int main(void) { // 変更なし } /******************** * bank_operation.c ********************/ /* 預入関数 */ void deposit(void) { // 変更なし } /* 引出関数 */ void withdraw(void) { // 変更なし } /* 残高照会関数 */ void check(void) { // 変更なし } |
bank_common.h #include <stdio.h> #include <stdlib.h> /* 個人情報構造体の宣言 */ struct private_info { int amount; // 残高 int password; // パスワード }; /* 個人情報構造体の外部変数の宣言 */ extern struct private_info info; /* 関数プロトタイプ (bank_common.h) */ void insert_card(void); void enter_password(void); void show_amount(void); |
main.c #include <stdlib.h> #include "sub_A.h" #include "sub_B.h" int main(void) { func_A(10, 20); func_B(30, 40); return EXIT_SUCCESS; } |
|
sub_A.h #include <stdio.h> void func_A(int x, int y); |
sub_A.c #include "sub_A.h" static int add(int x, int y) { return x + y; } void func_A(int x, int y) { printf("%d + %d = %d\n", x, y, add(x, y)); } |
sub_B.h #include <stdio.h> void func_B(int x, int y); |
sub_B.c #include "sub_B.h" static int add(int x, int y) { return x + y; } void func_B(int x, int y) { printf("%d + %d = %d\n", x, y, add(x, y)); } |
ソリューション・エクスプローラー構成
bank.c #include <stdio.h> #include <string.h> #include <stdlib.h> #include "bank_common.h" #include "bank_operation.h" /* 個人情報構造体の外部変数の定義 */ struct private_info info = {10000, 1234}; /* メイン関数 */ int main(void) { // 変更なし } |
|
bank_operation.h #include "bank_common.h" /* 関数プロトタイプ (bank_operation.h) */ void deposit(void); void withdraw(void); void check(void); |
bank_operation.c #include "bank_operation.h" /******************** * bank_operation.c ********************/ /* 預入関数 */ void deposit(void) { // 変更なし } /* 引出関数 */ void withdraw(void) { // 変更なし } /* 残高照会関数 */ void check(void) { // 変更なし } |
bank_common.h
#ifndef BANK_COMMON_H #define BANK_COMMON_H #include <stdio.h> #include <stdlib.h> /* 個人情報構造体の宣言 */ struct private_info { int amount; // 残高 int password; // パスワード }; /* 個人情報構造体の外部変数の宣言 */ extern struct private_info info; /* 関数プロトタイプ (bank_common.h) */ void insert_card(void); void enter_password(void); void show_amount(void); #endif
bank_operation.h
#ifndef BANK_OPERATION_H #define BANK_OPERATION_H #include "bank_common.h" /* 関数プロトタイプ (bank_operation.h) */ void deposit(void); void withdraw(void); void check(void); #endif
goods.h |
goods.c #include "goods.h" void registration(struct goods* item, char* name, int value) { strcpy_s(item->name, sizeof(item->name), name); item->value = value; } void print(struct goods item) { printf("品名:%s\n", item.name); printf("価格:%d\n", item.value); } |
main.c #include <stdlib.h> #include "goods.h" int main(void) { int i; struct goods item[3]; registration(&item[0], "book", 1000); registration(&item[1], "DVD", 3000); registration(&item[2], "pen", 100); for (i = 0; i < 3; ++i) { print(item[i]); } return EXIT_SUCCESS; } |