バス時刻表を格納する一次元リストとその反復子を操作するプログラム

フォーラム(掲示板)ルール
フォーラム(掲示板)ルールはこちら  ※コードを貼り付ける場合は [code][/code] で囲って下さい。詳しくはこちら
あや
記事: 28
登録日時: 4年前

バス時刻表を格納する一次元リストとその反復子を操作するプログラム

#1

投稿記事 by あや » 4年前

バス時刻表を格納する一次元リストとその反復子を操作するプログラムを作成しています.
・バス時刻表(busSchedule)は各バス停の到着情報(Arrival)をリスト構造につなげたものです
バス停タイプ(0:乗車のみ、1:乗降可能、2:降車のみ)
バス停名
通過時刻(hh:mmの形式)
・初期状態では空であり、反復子は末尾を参照します
・反復子が参照すべき要素が存在しない場合にはend()を参照とします
・SHOWの場合には各要素の内容を各一行に表示します
・反復子が参照する要素には、行の先頭に「 > 」のマークを付けます
(表示例)
0 kokunai 10:15
0 daisan 10:17
>0 kokusai 10:27
1 tsukushino 10:41
1 kiyama 10:45

・INSERTの場合には、追加する要素のバス停名、バス停タイプ、通過時刻の時と分をキー入力から取得します
・「反復子がbegin()を参照するときのPREVIOUSコマンド」および「反復子がend()を参照するときのNEXTコマンド」は無効とします
・SHOWとINSERTの処理のみ関数にします
・listとその反復子は、大域変数として定義します

以下のコードまで作成したのですが,わからない点があります.
・SHOWを実行すると全ての要素に「 > 」のマークが付いてしまいます.反復子が参照する要素の行の先頭のみに付けるには,どのようなコードを書けばいいですか?
・コマンド番号の3と4の処理が正しく実行されないのですが,コードをどのように修正したらよいでしょうか?
以上の2点について詳しく教えていただきたいです.
また,現在のコードで修正点があれば,それも教えてください.よろしくお願いします.

コード:

#include <iostream>
#include <list>
#include <string>
using namespace std;

#define SHOW     1        //表示 
#define INSERT   2        //追加
#define DELETE   3        //削除
#define NEXT     4        //次の要素 
#define PREVIOUS 5       //前の要素  
#define END      6      //終了

//バス停到着情報クラス
class Arrival {
public:
	int busStopType;        //バス停タイプ
	string busStopName;  //バス停名
	string time;   //通過時刻

	// コンストラクタ
	Arrival(int type, string name, string t) {
		busStopType = type;
		busStopName = name;
		time = t;
	}
};

void showMenu(void);        //機能の一覧を表示する。
void show();
void insert(string busStopName, int busStopType, string time);

// 大域変数
list<Arrival> busSchedule;    //バス時刻表リスト
list<Arrival>::iterator  p;   //上記を参照する反復子

/****
メイン関数
****/
void main() {
	int type=0;
	string name, time;
	string t;
	int command;
	int busStopType=0;        //バス停タイプ
	string busStopName;  //バス停名
	
	p = busSchedule.end();  //反復子がリスト末尾を指すようにする

	//機能の一覧を表示する。
	showMenu();
	
	//コマンド番号を入力する。
	cout << "コマンド番号=";
	cin >> command;

	//コマンドが「終了」でない限り、以下の処理を繰り返す。
	while (command != END) {

		//コマンドが「表示」であれば、以下を実行する。
		switch (command) {
		case SHOW:
			show();
			break;

			//コマンドが「追加」であれば、以下を実行する。
		case INSERT:		
			insert(busStopName,busStopType,time);
			break;

			//コマンドが「削除」であれば、以下を実行する。
		case DELETE:
			p = busSchedule.erase(p);    //pの参照先を削除、戻り値は次の要素を参照
			break;

			//コマンドが「次の要素」であれば、以下を実行する。
		case NEXT:
			if (p == busSchedule.end()) {
				return;
			}
			p++;   //反復子が次の要素を参照するようにする
			break;

			//コマンドが「前の要素」であれば、以下を実行する。
		case PREVIOUS:
			if (p == busSchedule.begin()) {
				return;
			}
			p--;   //反復子が前の要素を参照するようにする
			break;
		}
		
		// 機能の一覧を表示する。
		showMenu();

		// コマンド番号を入力する。
		cout << "コマンド番号=";
		cin >> command;
	}		
}

	/*****************************************
 機能の一覧を表示する。
*****************************************/
	void showMenu(void){

		// 1. 「コマンド番号 1 は リストに登録されている要素を一覧表示する。」と表示する。
		cout << "コマンド番号 1 は リストに登録されている要素を一覧表示する。" << endl;

		// 2. 「コマンド番号 2 は 反復子の直前に新規要素を追加する。」と表示する。
		cout << "コマンド番号 2 は 反復子の直前に新規要素を追加する。" << endl;

		// 3. 「コマンド番号 3 は 反復子が参照する要素を削除する。」と表示する。
		cout << "コマンド番号 3 は 反復子が参照する要素を削除する。" << endl;

		// 4. 「コマンド番号 4 は 反復子を次の要素に進める。」と表示する。
		cout << "コマンド番号 4 は 反復子を次の要素に進める。" << endl;

		// 5. 「コマンド番号 5 は 反復子を前の要素に進める。」と表示する。 
		cout << "コマンド番号 5 は 反復子を前の要素に進める。" << endl;

		// 6. 「コマンド番号 6 は プログラムを終了する。」と表示する。 
		cout << "コマンド番号 6 は プログラムを終了する。" << endl;
	}

	void show() {
		list<Arrival>::iterator  q;
		p = busSchedule.begin();
		for (q = busSchedule.begin(); q != busSchedule.end(); q++) {
			if (q == p) {
				cout << ">";
			}
			cout << p->busStopType << " " << p->busStopName << " " << p->time << endl;
			p++;
		}
	}

	void insert(string busStopName, int busStopType, string time){
		int type=0;
		// 入力を受け取る
		cout << "バス停タイプ(0:乗車のみ、1:乗降可能、2:降車のみ),バス停名,通過時刻(hh : mmの形式)> ";
		cin >> busStopType;        //バス停タイプ
		cin >> busStopName;  //バス停名
		cin >> time;		
		p = busSchedule.insert(p, Arrival(busStopType, busStopName, time)); //pの参照先の直前に挿入、戻り値は挿入要素を参照
	}

dic
記事: 657
登録日時: 13年前
住所: 宮崎県
連絡を取る:

Re: バス時刻表を格納する一次元リストとその反復子を操作するプログラム

#2

投稿記事 by dic » 4年前

コード:

void show() {
	list<Arrival>::iterator  q;
	p = busSchedule.begin();
	for (q = busSchedule.begin(); q != busSchedule.end(); q++) {
		if (q == p) {
			cout << ">";
		}
		cout << q->busStopType << " " << q->busStopName << " " << q->time << endl;
//		p++;
	}
}
cout << p // ピー ではなく q
p++; がいらない

dic
記事: 657
登録日時: 13年前
住所: 宮崎県
連絡を取る:

Re: バス時刻表を格納する一次元リストとその反復子を操作するプログラム

#3

投稿記事 by dic » 4年前

// p = busSchedule.begin();
あとこれもいらないですね

あや
記事: 28
登録日時: 4年前

Re: バス時刻表を格納する一次元リストとその反復子を操作するプログラム

#4

投稿記事 by あや » 4年前

以下のコードに修正したところ,コマンド番号4を選択すると反復子の「 > 」のマークが消えてしまいます.反復子を次の要素に進めるには,コードをどのように修正したらよいでしょうか?
教えてください.よろしくお願いします.

コード:

#include <iostream>
#include <list>
#include <string>
using namespace std;

#define SHOW     1        //表示 
#define INSERT   2        //追加
#define DELETE   3        //削除
#define NEXT     4        //次の要素 
#define PREVIOUS 5       //前の要素  
#define END      6      //終了

//バス停到着情報クラス
class Arrival {
public:
	int busStopType;        //バス停タイプ
	string busStopName;  //バス停名
	string time;   //通過時刻

	// コンストラクタ
	Arrival(int type, string name, string t) {
		busStopType = type;
		busStopName = name;
		time = t;
	}
};

void showMenu(void);        //機能の一覧を表示する。
void show();
void insert(string busStopName, int busStopType, string time);

// 大域変数
list<Arrival> busSchedule;    //バス時刻表リスト
list<Arrival>::iterator  p;   //上記を参照する反復子

/****
メイン関数
****/
void main() {
	int type=0;
	string name, time;
	string t;
	int command;
	int busStopType=0;        //バス停タイプ
	string busStopName;  //バス停名
	
	p = busSchedule.end();  //反復子がリスト末尾を指すようにする

	//機能の一覧を表示する。
	showMenu();
	
	//コマンド番号を入力する。
	cout << "コマンド番号=";
	cin >> command;

	//コマンドが「終了」でない限り、以下の処理を繰り返す。
	while (command != END) {

		//コマンドが「表示」であれば、以下を実行する。
		switch (command) {
		case SHOW:
			show();
			break;

			//コマンドが「追加」であれば、以下を実行する。
		case INSERT:		
			insert(busStopName,busStopType,time);
			break;

			//コマンドが「削除」であれば、以下を実行する。
		case DELETE:
			p = busSchedule.erase(p);    //pの参照先を削除、戻り値は次の要素を参照
			break;

			//コマンドが「次の要素」であれば、以下を実行する。
		case NEXT:
			while (p != busSchedule.end()) {
				p++;		//反復子が次の要素を参照するようにする
			}
			break;

			//コマンドが「前の要素」であれば、以下を実行する。
		case PREVIOUS:
			while (p != busSchedule.begin()) {
				p--;   //反復子が前の要素を参照するようにする
			}
			break;
		}
		
		// 機能の一覧を表示する。
		showMenu();

		// コマンド番号を入力する。
		cout << "コマンド番号=";
		cin >> command;
	}		
}

	/*****************************************
 機能の一覧を表示する。
*****************************************/
	void showMenu(void){

		// 1. 「コマンド番号 1 は リストに登録されている要素を一覧表示する。」と表示する。
		cout << "コマンド番号 1 は リストに登録されている要素を一覧表示する。" << endl;

		// 2. 「コマンド番号 2 は 反復子の直前に新規要素を追加する。」と表示する。
		cout << "コマンド番号 2 は 反復子の直前に新規要素を追加する。" << endl;

		// 3. 「コマンド番号 3 は 反復子が参照する要素を削除する。」と表示する。
		cout << "コマンド番号 3 は 反復子が参照する要素を削除する。" << endl;

		// 4. 「コマンド番号 4 は 反復子を次の要素に進める。」と表示する。
		cout << "コマンド番号 4 は 反復子を次の要素に進める。" << endl;

		// 5. 「コマンド番号 5 は 反復子を前の要素に進める。」と表示する。 
		cout << "コマンド番号 5 は 反復子を前の要素に進める。" << endl;

		// 6. 「コマンド番号 6 は プログラムを終了する。」と表示する。 
		cout << "コマンド番号 6 は プログラムを終了する。" << endl;
	}

	void show() {
		list<Arrival>::iterator  q;
		for (q = busSchedule.begin(); q != busSchedule.end(); q++) {
			if (q == p) {
				cout << ">";
			}
			cout << q->busStopType << " " << q->busStopName << " " << q->time << endl;		
		}
	}

	void insert(string busStopName, int busStopType, string time){
		int type=0;
		// 入力を受け取る
		cout << "バス停タイプ(0:乗車のみ、1:乗降可能、2:降車のみ),バス停名,通過時刻(hh : mmの形式)> ";
		cin >> busStopType;        //バス停タイプ
		cin >> busStopName;  //バス停名
		cin >> time;		
		p = busSchedule.insert(p, Arrival(busStopType, busStopName, time)); //pの参照先の直前に挿入、戻り値は挿入要素を参照
	}

アバター
asd
記事: 319
登録日時: 13年前

Re: バス時刻表を格納する一次元リストとその反復子を操作するプログラム

#5

投稿記事 by asd » 4年前

Advanced Supporting Developer
無理やりこじつけ(ぉ

返信

“C言語何でも質問掲示板” へ戻る