作業1) bankという名前のプロジェクトを作成し,下記のようなbank.cというソースファイルを作成する. ビルドし,実行して,動作を確認する.
ソースファイル名(bank.c)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
/* 個人情報構造体の宣言 */
struct private_info{
	int amount; // 残高
	int password; // パスワード
};
/* 個人情報構造体の実体 */
struct private_info info={10000,1111};
/* 関数プロトタイプ */
void insertCard();
void enterPassword();
void showAmount();
void deposit();
void withdraw();
void check();
/* メイン関数 */
int
main(void){
	char buff[2];
	printf("ご利用内容を選択してください [ 預入:d, 引出:w, 照会:s, 終了:f ]\n>>");
	while(fgets(buff,sizeof(buff),stdin) != NULL) {
		fflush(stdin);
		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>>");
	}
	return (EXIT_SUCCESS);
}
/* カード挿入関数 */
void
insertCard(){
	printf("カードが挿入されました\n");
}
/* パスワード入力関数 */
void
enterPassword(){
	char pass[5];
	printf("パスワードを入力してださい\n>>");
	while(fgets(pass,sizeof(pass),stdin) != NULL) {
		fflush(stdin);
		if(info.password != atoi(pass)) {
			printf("パスワードが間違っています.もう一度入力してください\n>>");
		} else {
			break;
		}
	}
	printf("認証に成功しました\n");
}
/* 残高表示関数 */
void
showAmount(){
	printf("現在の残高は %d 円です\n",info.amount);
}
/* 預入関数 */
void
deposit(){
	char buff[128];
	int dep;
	insertCard(); // カード挿入
	printf("預ける金額を入力してください\n>>");
	while(fgets(buff,sizeof(buff),stdin) != NULL) {
		fflush(stdin);
		dep = atoi(buff);
		if(dep < 0) {
			printf("不正な金額です.再度,預ける金額を入力してください\n>>");
		} else {
			info.amount += dep;
			break;
		}
	}
	showAmount(); // 残高表示
}
/* 引出関数 */
void
withdraw(){
	char buff[128];
	int dep;
	insertCard(); // カード挿入
	enterPassword(); // パスワード入力
	printf("引き出す金額を入力してください\n>>");
	while(fgets(buff,sizeof(buff),stdin) != NULL) {
		fflush(stdin);
		dep = atoi(buff);
		if((dep < 0) || (dep > info.amount)) {
			printf("不正な金額です.再度,引き出す金額を入力してください\n>>");
		} else {
			info.amount -= dep;
			break;
		}
	}
	showAmount(); // 残高表示
}
/* 残高照会関数 */
void
check()
{
	insertCard(); // カード挿入
	enterPassword(); // パスワード入力
	showAmount(); // 残高表示
}
ソリューションエクスプローラ内
作業2) ソースファイルフォルダ内のbank.cを下記のように変更する. ソースファイルフォルダ内にbank_common.cを,ヘッダーファイルフォルダ内に bank_common.hを下記の内容で新たに作成し,(ビルドではなく)リビルドする.
ソースファイル名(bank.c)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "bank_common.h"
void deposit();
void withdraw();
void check();
int
main(void){
/*変更なし*/
}
void
deposit(){
/*変更なし*/
}
void
withdraw(){
/*変更なし*/
}
void
check(){
/*変更なし*/
}
ヘッダファイル名(bank_common.h)
#include <stdio.h>
#include <stdlib.h>
struct private_info{
	int amount;
	int password;
};
struct private_info info={10000,1111};
void insertCard();
void enterPassword();
void showAmount();
ソースファイル名(bank_common.c)
#include "bank_common.h"
void
insertCard(){
/*変更なし*/
}
void
enterPassword(){
/*変更なし*/
}
void
showAmount(){
/*変更なし*/
}
ソリューションエクスプローラ内
作業3) リビルドに失敗(infoは既にbank.objで定義されているというエラー)するので,以下の様にプログラムを修正し,再度リビルドする.
ソースファイル名(bank.c)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "bank_common.h"
struct private_info info={10000,1111};
void deposit();
void withdraw();
void check();
int
main(void){
/*変更なし*/
}
void
deposit(){
/*変更なし*/
}
void
withdraw(){
/*変更なし*/
}
void
check(){
/*変更なし*/
}
ヘッダファイル名(bank_common.h)
#include <stdio.h>
#include <stdlib.h>
struct private_info{
	int amount;
	int password;
};
extern struct private_info info;
extern void insertCard();
extern void enterPassword();
extern void showAmount();
void function(void);
int a;
int main(void){
	int b;
}
void function(void){
	int c;
	int b;
	int a;
}
#include "subA.h"
#include "subB.h"
int main(void){
	funcA(10,20);
	funcB(30,40);
}
ヘッダファイル名(subA.h)
#include <stdio.h> void funcA(int x,int y);ヘッダファイル名(subB.h)
#include <stdio.h> void funcB(int x,int y);ソースファイル名(subA.c)
#include "subA.h"
static int add(int x,int y){
	return (x+y);
}
void funcA(int x,int y){
	printf("%d + %d = %d\n",x,y,add(x,y));
}
ソースファイル名(subB.c)
#include "subB.h"
static int add(int x,int y){
	return (x+y);
}
void funcB(int x,int y){
	printf("%d + %d = %d\n",x,y,add(x,y));
}
作業4) ソースファイルフォルダ内のbank.cを下記のように変更する. ソースファイルフォルダ内にbank_operation.cを,ヘッダーファイルフォルダ内に bank_operation.hを下記の内容で新たに作成し,リビルドする.
ソースファイル名(bank.c)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "bank_common.h"
#include "bank_operation.h"
struct private_info info={10000,1111};
int
main(void){
/*変更なし*/
}
ヘッダファイル名(bank_operation.h)
#include <stdio.h> #include <stdlib.h> #include "bank_common.h" /* depositなどの関数内でinsertCardなどの関数を使用しているのでincludeしなければならない */ extern void deposit(); extern void withdraw(); extern void check();ソースファイル名(bank_operation.c)
#include "bank_operation.h"
void deposit(){
/*変更なし*/
}
void withdraw(){
/*変更なし*/
}
void check(){
/*変更なし*/
}
ソリューションエクスプローラ内
作業5) リビルドに失敗(private_infoがの再定義エラー)するので,以下の様にプログラムを修正し,再度リビルドする.
ヘッダファイル名(bank_common.h)
#ifndef BANK_COMMON_H
#define BANK_COMMON_H
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct private_info{
	int amount;
	int password;
};
extern struct private_info info;
extern void insertCard();
extern void enterPassword();
extern void showAmount();
#endif
ヘッダファイル名(bank_operation.h)
#ifndef BANK_OPERATION_H #define BANK_OPERATION_H #include <stdio.h> #include <stdlib.h> #include "bank_common.h" extern void deposit(); extern void withdraw(); extern void check(); #endif
練習問題1)下記のプログラムが動作するようにsub.cを修正せよ.(main.c,sub.hは修正不可)
ソースファイル(main.c)
#include <stdio.h>
#include "sub.h"
int a=10;
int b=20;
int
main(void){
	printf("%d + %d = %d\n",a,b,add());
}
ヘッダファイル(sub.h)
extern int a; int add();ソースファイル(sub.c)
#include "sub.h"
int add(){
	return (a+b);
}
練習問題2)下記のmain.c(変更不可)を動作させるためにsub.cに追加を行い, 対応するsub.hを作成せよ.(strcpyに関するコンパイルエラーは無視しても良い.但しstring.hが必要)
ソースファイル(main.c)
#include "sub.h"
int
main(void){
	struct student member;
	init(&member,"宇大 太郎",20);
	print(member);
}
ソースファイル(sub.c)
void
init(struct student *member,char *name,int age){
	strcpy(member->name,name);
	member->age  = age;
}
void
print(struct student member){
	printf("名前:%s\n",member.name);
	printf("年齢:%d\n",member.age);
}
ヘッダファイル(sub.h)