プログラム演習2レポート(5〜8週)
情報理工学部・メディア情報学科
一回生 西山大輔
学籍番号:2630040115-4
1. 課題5-2
解説
キーボードから入力された文字を一文字ずつスタックに Push し、同じくキーボードからの入力によって Push・Pop・終了の各処理を行うプログラム。処理のあとには print_stack_matrx 関数によって、スタックの中身を表示する。
初期スタックの構築に /lesson/nishiura/prog2/data.dat 、スタックの内容の表示に /lesson/nishiura/prog2/print_stack_matrx.o を使う。
フローチャートは、レポートの最後の図1に示す。
結果
% ./kadai --- Contents of Stack --- N < == Top (11) A K I E M U S T I R ------------------------- input char<< X --- Contents of Stack --- X < == Top (12) N A K I E M U S T I R ------------------------- input char<< 1 Pushed: X --- Contents of Stack --- N < == Top (11) A K I E M U S T I R ------------------------- input char<< 0 quit.
2. 課題7-2 キーボードからのキュー操作
解説
キーボードから入力された文字を一文字ずつキューに Enqueue し、同じくキーボードからの入力によって Dequeue ・終了の処理を行うプログラム。処理のあとには print_queue_matrx 関数によって、キューの中身を表示する。
初期キューの構築に /lesson/nishiura/prog2/data.dat 、キューの内容の表示に /lesson/nishiura/prog2/print_queue_matrx.o を使う。
フローチャートは、レポートの最後の図2に示す。
結果
--- Contents of Queue --- N < -- Rear (11) A K I E M U S T I R < -- Top (1) ------------------------- input char<< X --- Contents of Queue --- X < -- Rear (12) N A K I E M U S T I R < -- Top (1) ------------------------- input char<< 1 Dequeued: R --- Contents of Queue --- X < -- Rear (12) N A K I E M U S T I < -- Top (2) ------------------------- input char<< 0 exit.
3. 感想
課題自体の難易度は高くなかった。しかし、getc(getchar)を使ったキーボードからの入力の際に入る \n の存在を失念していたため、かなり長い間バグに悩まされることとなった。
非常に切羽詰った状態でのレポート執筆となってしまったため、コメントや解説などが今ひとつ。次は余裕を持ってレポートを仕上げたい。
4. 参考文献
BohYoh.com 【C言語講座】
http://www.bohyoh.com/index.html
5. ソースコード
課題5-2
/* Stack with matrix */ #include <stdio.h> #define MAX 100 char s[MAX]; int top; char c; FILE* fp; initstack(){ top = 0; } char pop(){ int cl_pop; cl_pop = s[top-1]; top--; return cl_pop; } void push(char cl_push){ s[top] = cl_push; top++; printf("Pushed: %c\n", cl_push); } main() { initstack(); fp = fopen("/lesson/nishiura/prog2/data.dat", "r"); while((c = getc(fp)) != EOF){ s[top] = c; top++; } /* /lesson/nishiura/prog2/data.dat からgetc()を用いて 1文字づつ読み込みスタックsに格納. ただしスタックの出入り口を示す top の値も監視すること */ /* 以下は変更しない */ print_stack_mtrx( s, top ); /* この関数は/lesson/nishiura/prog2/print_stack_mtrx.o にあり,スタック(配列)の内容を表示する関数である.*/ ready: c = getchar(); switch(c){ case '\n': break; case '0': printf("\n"); return(0); case '1': printf("Poped: %c\n", pop()); print_stack_mtrx(s, top); printf("\n"); break; default: push(c); print_stack_mtrx(s,top); break; } goto ready; }
課題7-2
#include <stdio.h> #define MAX 100 char q[MAX]; char c; /* temporary var */ int top, rear; FILE *fp; initqueue(){ top = 0; rear = 0; } void enqueue(char cl){ q[rear] = cl; rear++; } char dequeue(){ top++; return q[top-1]; } int main(){ initqueue(); fp = fopen("/lesson/nishiura/prog2/data.dat", "r"); while((c = fgetc(fp)) != EOF){ enqueue(c); } fclose(fp); print_queue_mtrx(q, top, rear); while((c = getchar()) != '0'){ switch(c) { case '\n': /* Skiped */ break; case '1': printf("\nDequeued : %c\n", dequeue()); print_queue_mtrx(q, top, rear); break; default: enqueue(c); printf("\nEnqueued : %c\n", c); print_queue_mtrx(q, top, rear); } } printf("Exit.\n\n"); }
penis extender devices penis extender http://www.cheappenisextenders.com