カテゴリー
C C++

C++Builder入門、サンプルプログラムAirport.makを動かす

//-------------------------------
#pragma once
//-------------------------------
#define AIRLINER 0
#define COMMUTER 1
#define PRIVATE 2
#define TAKINGOFF 0
#define CRUISING 1
#define LANDING 2
#define ONRAMP 3
#define MSG_CHANGE 0
#define MSG_TAKEOFF 1
#define MSG_LAND 2
#define MSG_REPORT 3

#include <stdio.h>
class Airplane {
  public:
	Airplane(const char* _name, int _type = AIRLINER);
	~Airplane();
	
	virtual int GetStatus(char* statusString);
	int GetStatus() {	return status;	}
	int Speed() { return speed; }
	int Heading() {	return heading;	}
	int Altitude() { return altitude;	}

	void ReportStatus();
	bool SendMessage(int msg, char* response, int spd = -1, int dir = -1, int alt = -1);
	char* name;
  protected:
	virtual void TakeOff(int dir);
	virtual void Land();
  private:
	int speed;
	int altitude;
	int heading;
	int status;
	int type;
	int ceiling;
};
#include <iostream> 
#include <stdio.h>
#include <cstring>
#pragma hdrstop

#include "airplane.h"
using namespace std;

//
// 初期化処理を行うコンストラクタ
//
Airplane::Airplane(const char* _name, int _type) :
	type(_type),
	status(ONRAMP),
	speed(0),
	altitude(0),
	heading(0)
{
	switch (type) {
	case AIRLINER: ceiling = 35000; break;
	case COMMUTER: ceiling = 20000; break;
	case PRIVATE: ceiling = 8000; break;
	//default: ceiling = 0;
	}
	name = new char[50];
	strcpy(name, _name);
}
//
// 終了処理を行うデストラクタ
//
Airplane::~Airplane()
{
	delete[] name;
}
//
// ユーザからメッセージを取得する
//
bool
Airplane::SendMessage(int msg, char* rtnMsg, int spd, int dir, int alt)
{
	// 不正なコマンドを検査する
	//
	strcpy(rtnMsg, " ");
	if (spd > 500) {
		char w[] = "Speed cannot be more than 500.";
		strcpy(rtnMsg, w);
		return false;
	}
	if (dir > 360) {
		char w[] = "Heading cannot be over 360 degrees.";
		strcpy(rtnMsg, w);
		return false;
	}
	if (alt < 100 && alt != -1) {
		char w[] = "I'd crash, bonehead!";
		strcpy(rtnMsg, w);
		return false;
	}
	if (alt > ceiling) {
		char w[] = "I can't go that high.";
		strcpy(rtnMsg, w);
		return false;
	}
	//
	// 送られたコマンドに基づいて各処理を実行する
	//
	switch (msg) {
	case MSG_TAKEOFF: {
		// すでに上空にいる場合は離陸できない
		if (status != ONRAMP) {
			char w[] = "I'm already in the air!";
			strcpy(rtnMsg, w);
			return false;
		}
		TakeOff(dir);
		break;
	}
	case MSG_CHANGE: {
		// 地上にいる場合は何も変更できない
		if (status == ONRAMP) {
			char w[] = "I'm on the ground";
			strcpy(rtnMsg, w);
			return false;
		}
		// 正の値が渡された場合のみ変更する
		if (spd != -1) speed = spd;
		if (dir != -1) heading = dir;
		if (alt != -1) altitude = alt;
		status = CRUISING;
		break;
	}
	case MSG_LAND: {
		if (status == ONRAMP) {
			char w[] = "I'm already on the ground.";
			strcpy(rtnMsg, w);
			return false;
		}
		Land();
		break;
	}
	case MSG_REPORT: ReportStatus();
	}
	//
	// すべてうまくいっている場合の標準的な応答
	//	
	char w[] = "Roger.";
	strcpy(rtnMsg, w);
	return true;
}
//
// 離陸处理
//
void
Airplane::TakeOff(int dir)
{
	heading = dir;
	status = TAKINGOFF;
}
//
//着陸処理
//
void
Airplane::Land()
{
	speed = heading = altitude = 0;
	status = ONRAMP;
}
//
// 飛行機のステータスを報告する文字列を作成する
//
int
Airplane::GetStatus(char* statusString)
{
	sprintf(statusString, "%s, Altitude: %d, Heading: %d, "
		"Speed: %d\n", name, altitude, heading, speed);
	return status;
}
//
// ステータス文字列を取得して、それを画面に表示する
//
void
Airplane::ReportStatus()
{
	char buffer[100];
	strcpy(buffer, " ");
	GetStatus(buffer);
	cout << endl <<buffer << endl;
}
//追加
//
// メッセージをセット
//
/*
void 
Airplane::SetMessage(char* msg) {
	strcpy_s(msg, strlen(msg), msg);
}
*/
#include <iostream>
#include <conio.h>
#pragma hdrstop

#include "airplane.h"
using namespace std;
int getInput(int max);
void getItems(int& speed, int& dir, int& alt);
int main(int argc, char** argv)
{
	char returnMsg[100];
	// Airpleのポインタ配列を用意し、 
	// 3つのAirplaneオブジェクトを作成する
	//
	Airplane* planes[3];
	planes[0] = new Airplane("TWA 1040");
	planes[1] = new Airplane("United Express 749", COMMUTER);
	planes[2] = new Airplane("Cessna 3238T", PRIVATE);
	//
	//ループ開始
	//
	do {
		int plane, message, speed, altitude, direction;
		speed = altitude = direction = -1;
		// メッセージを送るべき飛行機を取得する
		// 飛行機の一覧を表示し、 ユーザに1つ選択してもらう
		//
		cout << "Who do you want to send a message to?" << endl;
		cout << "0. Quit" << endl;
		for (int i = 0; i < 3; i++)
			cout << i + 1 << ". " << planes[i] -> name << endl;

		//
		// getInput ( ) 関数を呼び出して、 機体番号を取得する
		//
		plane = getInput(4);
		//
		// ユーザが項目を選択した場合はループを終了する
		//
		if (plane == -1) break;
		//
		// 飛行機の確認 47:
		//
		cout << endl << planes[plane]-> name << ", roger.";
		cout << endl << endl;
		//
		// 送信したいメッセージをユーザに選択してもらう 
		//
		cout << "What message do you want to send?" << endl;
		cout << endl << "0. Quit" << endl;;
		cout << "1. State Change" << endl;
		cout << "2. Take Off" << endl;
		cout << "3. Land" << endl;
		cout << "4. Report Status" << endl;
		message = getInput(5);
		//
		// ユーザが0を選択した場合はループを終了する
		//
		if (message == -1) break;
		//
		// ユーザが項目1を選択した場合は、
		// getItems ( ) 関数を呼び出して、
		// 新しい速度、機首角度、 高度を入力してもらう
		if (message == 0)
			getItems(speed, direction, altitude);
		//
		// 飛行機にメッセージを送信
		//
		bool goodMsg = planes[plane]->SendMessage(message, returnMsg, speed, direction, altitude);
		//
		//メッセージが不適切な場合
		//
		if (!goodMsg) cout << endl << "Unable to comply.";
		//
		// 飛行機の応答を表示
		//
		cout << endl << returnMsg << endl;
	} while (1);
	//
	// Airplainオブジェクトを削除する
	//
	for (int i = 0; i < 3; i++) delete planes[i];
}
int getInput(int max)
{
	int choice;

	do {
		choice = _getch();
		choice -= 49;

	} while (choice < -1 || choice > max);
	return choice;
}
void getItems(int& speed, int& dir, int& alt)
{
	cout << endl << "Enter new speed: ";
	//_getch();
	cin >> speed;
	cout << "Enter new heading: ";
	cin >> dir;
	cout << "Enter new altitude: ";
	cin >> alt;
	cout << endl;
}
inserted by FC2 system