当日のOHP資料です。
/*
* icell.c
*/
#include <stdio.h>
#include <stdlib.h>
/*
*
*/
#include "xalloc.h"
#include "icell.h"
/*
* new_ICell : 新しい ICell を確保 ( NULL は、確保に失敗した場合.. )
* int value; -- この Cell が保持する整数値データ
*/
ICell *new_ICell ( int value ) {
ICell *newObject = xalloc ( ICell ); /* 新しい ICell を確保 */
if ( newObject != NULL ) { /* 確保できれば.. */
newObject -> next = NULL; /* 個々の ICell は最初は「次」がない */
newObject -> data = value;
/*
newObject
|
| +-------+
+--> | *--------> NULL
+-------+
|value |
+-------+
*/
}
return newObject; /* 確保した領域へのポインター値を返す */
}
/*
* free_ICell
*/
void free_ICell ( ICell *object ) {
if ( object != NULL ) { /* NULL の可能性があるのでチェック */
xfree ( object );
}
}
/*
* printf_ICell
*/
void printf_ICell ( char *format, ICell *object ) {
if ( object != NULL ) { /* NULL の可能性があるのでチェック */
printf ( format, object -> data );
/* object -> data == (*object).data */
}
}
/*
* print_ICell
*/
void print_ICell ( ICell *object ) {
printf_ICell ( " %d", object ); /* 標準の出力形式は 「%d」で.. */
}
/*
* scan_ICell
*/
ICell *scan_ICell ( char *svalue ) {
return new_ICell ( atoi ( svalue ) );
/* atoi は整数を表す文字列から、
その文字列が表現する整数値を値として返する関数 */
}
/*
*
*/
Can not access : program/icell.out
/*
* ilist.c
*/
#include <stdio.h>
#include <stdlib.h>
/*
*
*/
#include "xalloc.h"
#include "icell.h"
#include "boolean.h"
#include "ilist.h"
/*
* new_IList : 新しい IList を確保 ( NULL は、確保に失敗した場合.. )
*/
IList *new_IList () {
IList *newObject = xalloc ( IList ); /* 新しい IList を確保 */
if ( newObject != NULL ) { /* 確保できれば.. */
newObject -> top = NULL; /* 個々の IList は最初は「空」になる */
}
/*
+---------+
| *---------> NULL; 空っぽのリスト
+---------+
*/
return newObject; /* 確保した領域へのポインター値を返す */
}
/*
* isEmpty_IList
*/
Boolean isEmpty_IList ( IList *object ) {
if ( object != NULL ) { /* NULL でなく.. */
if ( object -> top != NULL ) { /* 先頭の要素があれば.. */
return TRUE; /* 「真(TRUE)」を返す */
}
}
return FALSE; /* その他の場合は「偽(FALSE)」を返す */
}
/*
* free_IList
*/
void free_IList ( IList *object ) {
if ( object != NULL ) { /* NULL の可能性があるのでチェック */
ICell *cp = object -> top; /* 先頭のデータを取り出し */
/* リストの要素を管理するセルも開放する必要がある */
while ( cp != NULL ) { /* まだ要素があれば.. */
ICell *nx = cp -> next; /* 次の要素を予め取り出しておいて .. */
free_ICell ( cp ); /* その ICell を開放し.. */
cp = nx; /* 次の要素を考える.. */
}
xfree ( object ); /* 中身が開放されたので安心して自分の開放 */
}
}
/*
* print_IList
*/
void print_IList ( IList *object ) {
if ( object != NULL ) { /* NULL の可能性があるのでチェック */
ICell *cp = object -> top; /* 先頭のデータを取出.. */
while ( cp != NULL ) { /* まだ要素があれば.. */
print_ICell ( cp ); /* その要素を出力し */
cp = cp -> next; /* 次の要素をみる */
}
}
}
/*
* insert_IList
*/
IList *insert_IList ( IList *object, int data ) {
if ( object != NULL ) { /* NULL の可能性があるのでチェック */
ICell *cp = new_ICell ( data ); /* 新しい ICell を作成し.. */
if ( cp != NULL ) { /* それが作れれば.. */
cp -> next = object -> top; /* その要素を先頭に挿入 */
object -> top = cp;
}
}
return object;
}
/*
* delete_top_IList
*/
void delete_top_IList ( IList *object ) {
if ( object != NULL ) {
ICell *cp = object -> top; /* 先頭の要素 */
if ( cp != NULL ) { /* 先頭の ICell があれば.. */
object -> top = cp -> next; /* その ICell を取り外し.. */
free_ICell ( cp ); /* その ICell を開放 */
}
}
}
/*
* append_IList
*/
IList *append_IList ( IList *object, int data ) {
if ( object != NULL ) { /* NULL の可能性があるのでチェック */
ICell *newICell = new_ICell ( data ); /* 新しい ICell を作成 */
if ( newICell != NULL ) { /* 新しい ICell が作れれば.. */
/* 追加のために、まず、最後の要素の ICell を搜す */
ICell *last = NULL; /* 最後の要素候補へのポインター */
ICell *cp = object -> top; /* 先頭の要素 */
while ( cp != NULL ) { /* 最後の要素をさがす */
last = cp; /* 最後の要素候補を更新 */
cp = cp -> next; /* 次の要素を確認 */
}
if ( last == NULL ) { /* 実は List は空だった */
object -> top = newICell; /* 先頭に追加 */
} else {
last -> next = newICell; /* 最後の要素の次に追加 */
}
}
}
return object;
}
/*
* delete_end_IList
*/
void delete_end_IList ( IList *object ) {
if ( object != NULL ) { /* NULL の可能性があるのでチェック */
ICell *cp = object -> top; /* 先頭の要素 */
if ( cp != NULL ) { /* 要素があった場合に初めて処理開始 */
/* 削除のために、まず、最後の要素の前の ICell を搜す */
ICell *pre = NULL; /* 最後の要素の前の要素候補へのポインター */
ICell *last = cp; /* 最後の要素候補へのポインター */
cp = cp -> next;
while ( cp != NULL ) { /* 要素がある限り.. */
pre = last; /* pre は last の前 */
last = cp; /* last は cp の前 */
cp = cp -> next; /* cp を次の要素に.. */
}
if ( pre == NULL ) { /* 要素が一つしかなかった */
object -> top = NULL; /* List は空に */
} else {
pre -> next = NULL; /* 最後の要素を取り外す */
}
free_ICell ( last ); /* 最後の要素を開放 */
}
}
}
/*
* search_IList
*/
ICell *search_IList ( IList *object, int number ) {
if ( object != NULL ) { /* NULL の可能性があるのでチェック */
ICell *cp = object -> top;
while ( cp != NULL ) { /* 要素があれば.. */
if ( cp -> data == number ) { /* データを比較 */
return cp; /* 見付かったのでそれを返す */
}
cp = cp -> next; /* ちがったら、次を調べる */
}
}
return NULL; /* 見付からなかった場合は NULL を返す */
}
/*
*
*/
Can not access : program/ilist.out
/*
* dcell.c
*/
#include <stdio.h>
#include <stdlib.h>
/*
*
*/
#include "xalloc.h"
#include "dcell.h"
/*
* new_DCell : 新しい DCell を確保 ( NULL は、確保に失敗した場合.. )
* double value; -- この Cell が保持する整数値データ
*/
DCell *new_DCell ( double value ) {
DCell *newObject = xalloc ( DCell ); /* 新しい DCell を確保 */
if ( newObject != NULL ) { /* 確保できれば.. */
newObject -> next = NULL; /* 個々の DCell は最初は「次」がない */
newObject -> data = value;
/*
newObject
|
| +-------+
+--> | *--------> NULL
+-------+
|value |
+-------+
*/
}
return newObject; /* 確保した領域へのポインター値を返す */
}
/*
* free_DCell
*/
void free_DCell ( DCell *object ) {
if ( object != NULL ) { /* NULL の可能性があるのでチェック */
xfree ( object );
}
}
/*
* printf_DCell
*/
void printf_DCell ( char *format, DCell *object ) {
if ( object != NULL ) { /* NULL の可能性があるのでチェック */
printf ( format, object -> data );
}
}
/*
* print_DCell
*/
void print_DCell ( DCell *object ) {
printf_DCell ( " %f", object ); /* 標準の出力形式は 「%f」で.. */
}
/*
* scan_DCell
*/
DCell *scan_DCell ( char *svalue ) {
return new_DCell ( atof ( svalue ) );
}
/*
*
*/
Can not access : program/dcell.out
/*
* dlist.c
*/
#include <stdio.h>
#include <stdlib.h>
/*
*
*/
#include "xalloc.h"
#include "dcell.h"
#include "boolean.h"
#include "dlist.h"
/*
* new_DList : 新しい DList を確保 ( NULL は、確保に失敗した場合.. )
*/
DList *new_DList () {
DList *newObject = xalloc ( DList ); /* 新しい DList を確保 */
if ( newObject != NULL ) { /* 確保できれば.. */
newObject -> top = NULL; /* 個々の DList は最初は「空」になる */
}
return newObject; /* 確保した領域へのポインター値を返す */
}
/*
* isEmpty_DList
*/
Boolean isEmpty_DList ( DList *object ) {
if ( object != NULL ) { /* NULL でなく.. */
if ( object -> top != NULL ) { /* 先頭の要素があれば.. */
return TRUE; /* 「真(TRUE)」を返す */
}
}
return FALSE; /* その他の場合は「偽(FALSE)」を返す */
}
/*
* free_DList
*/
void free_DList ( DList *object ) {
if ( object != NULL ) { /* NULL の可能性があるのでチェック */
DCell *cp = object -> top; /* 先頭のデータを取り出し */
while ( cp != NULL ) { /* まだ要素があれば.. */
DCell *nx = cp -> next; /* 次の要素を予め取り出しておいて .. */
free_DCell ( cp ); /* その DCell を開放し.. */
cp = nx; /* 次の要素を考える.. */
}
xfree ( object ); /* 中身が開放されたので安心して自分の開放 */
}
}
/*
* print_DList
*/
void print_DList ( DList *object ) {
if ( object != NULL ) { /* NULL の可能性があるのでチェック */
DCell *cp = object -> top; /* 先頭のデータを取出.. */
while ( cp != NULL ) { /* まだ要素があれば.. */
print_DCell ( cp ); /* その要素を出力し */
cp = cp -> next; /* 次の要素をみる */
}
}
}
/*
* insert_DList
*/
DList *insert_DList ( DList *object, double data ) {
if ( object != NULL ) { /* NULL の可能性があるのでチェック */
DCell *cp = new_DCell ( data ); /* 新しい DCell を作成し.. */
if ( cp != NULL ) { /* それが作れれば.. */
cp -> next = object -> top; /* その要素を先頭に挿入 */
object -> top = cp;
}
}
return object;
}
/*
* delete_top_DList
*/
void delete_top_DList ( DList *object ) {
if ( object != NULL ) {
DCell *cp = object -> top; /* 先頭の要素 */
if ( cp != NULL ) { /* 先頭の DCell があれば.. */
object -> top = cp -> next; /* その DCell を取り外し.. */
free_DCell ( cp ); /* その DCell を開放 */
}
}
}
/*
* append_DList
*/
DList *append_DList ( DList *object, double data ) {
if ( object != NULL ) { /* NULL の可能性があるのでチェック */
DCell *newDCell = new_DCell ( data ); /* 新しい DCell を作成 */
if ( newDCell != NULL ) { /* 新しい DCell が作れれば.. */
/* 追加のために、まず、最後の要素の DCell を搜す */
DCell *last = NULL; /* 最後の要素候補へのポインター */
DCell *cp = object -> top; /* 先頭の要素 */
while ( cp != NULL ) { /* 最後の要素をさがす */
last = cp; /* 最後の要素候補を更新 */
cp = cp -> next; /* 次の要素を確認 */
}
if ( last == NULL ) { /* 実は List は空だった */
object -> top = newDCell; /* 先頭に追加 */
} else {
last -> next = newDCell; /* 最後の要素の次に追加 */
}
}
}
return object;
}
/*
* delete_end_DList
*/
void delete_end_DList ( DList *object ) {
if ( object != NULL ) { /* NULL の可能性があるのでチェック */
DCell *cp = object -> top; /* 先頭の要素 */
if ( cp != NULL ) { /* 要素があった場合に初めて処理開始 */
/* 削除のために、まず、最後の要素の前の DCell を搜す */
DCell *pre = NULL; /* 最後の要素の前の要素候補へのポインター */
DCell *last = cp; /* 最後の要素候補へのポインター */
cp = cp -> next;
while ( cp != NULL ) { /* 要素がある限り.. */
pre = last; /* pre は last の前 */
last = cp; /* last は cp の前 */
cp = cp -> next; /* cp を次の要素に.. */
}
if ( pre == NULL ) { /* 要素が一つしかなかった */
object -> top = NULL; /* List は空に */
} else {
pre -> next = NULL; /* 最後の要素を取り外す */
}
free_DCell ( last ); /* 最後の要素を開放 */
}
}
}
/*
* search_DList
*/
DCell *search_DList ( DList *object, double number ) {
if ( object != NULL ) { /* NULL の可能性があるのでチェック */
DCell *cp = object -> top;
while ( cp != NULL ) { /* 要素があれば.. */
if ( cp -> data == number ) { /* データを比較 */
return cp; /* 見付かったのでそれを返す */
}
cp = cp -> next; /* ちがったら、次を調べる */
}
}
return NULL; /* 見付からなかった場合は NULL を返す */
}
/*
*
*/
Can not access : program/dlist.out
Download : sample-001.c ( SJIS 版 )
/*
* 2011/12/09 sample-001.c
*/
/*
*
*/
#include <stdio.h>
#include <stdlib.h>
/*
*
*/
#include "boolean.h"
#include "ilist.h"
/*
*
*/
#define COMMAND_LINE_SIZE 1024
/*
*
*/
int main ( void ) {
char cmdline [ COMMAND_LINE_SIZE ];
IList *list = new_IList();
int cmd = 'h';
int number = 0;
/*
*
*/
if ( list == NULL ) {
printf ( "List が確保できませんでしたので終了します\n" );
exit ( -1 ); /* プログラムを終了 */
}
/*
*
*/
while ( cmd != 'q' ) {
/* コマンドの実行 */
switch ( cmd ) {
default:
printf ( "I don't know %c command\n", cmd );
// break;
case 'h':
printf ( "Command Help, Quit, Print, Insert #, Append #, Check, delete \
Top, delete End, Find #\n" );
break;
case 'q':
break;
case 'p':
print_IList ( list );
printf ( "\n" );
break;
case 'i':
insert_IList ( list, number );
break;
case 'a':
append_IList ( list, number );
break;
case 'c':
if ( isEmpty_IList ( list ) == FALSE ) {
printf ( "Empty\n" );
} else {
printf ( "No Empty\n" );
}
break;
case 't':
delete_top_IList ( list );
break;
case 'e':
delete_end_IList ( list );
break;
case 'f':
if ( search_IList ( list, number ) != NULL ) {
printf ( "Found\n" );
} else {
printf ( "Not Found\n" );
}
break;
}
/* コマンドの入力 */
printf ( "cmd> " );
fgets ( cmdline, COMMAND_LINE_SIZE, stdin ); /* 一行入力 */
cmd = cmdline[0]; /* 先頭の一文字がコマンド */
number = atoi ( cmdline + 2 ); /* 3 文字目以後は引数 */
}
/*
*
*/
free_IList ( list );
/*
*
*/
return 0;
}
/*
*
*/
p i 10 i 20 p a -10 a -20 p t p e p q
C:\usr\c\> sample-001< sample-001.in
Command Help, Quit, Print, Insert #, Append #, Check, delete Top, \
delete End, Find #
cmd> p
cmd> i 10
cmd> i 20
cmd> p
20 10
cmd> a -10
cmd> a -20
cmd> p
20 10 -10 -20
cmd> t
cmd> p
10 -10 -20
cmd> e
cmd> p
10 -10
cmd> q
C:\usr\c\>
Download : sample-002.c ( SJIS 版 )
/*
* 2011/12/09 sample-002.c
*/
/*
*
*/
#include <stdio.h>
#include <stdlib.h>
/*
*
*/
int main ( int argc, char *argv[] ) {
int i;
/*
*
*/
if ( argc > 1 ) { /* コマンドライン引数がある */
srand ( atoi ( argv[1] ) );
/* それは多分整数値を表す文字列とし..
その数値で、乱数を初期化する。
*/
}
/*
*
*/
for ( i = 0; i < 10; i++ ) {
printf ( "%d\n", rand() );
}
/*
*
*/
return 0;
}
/*
*
*/
C:\usr\c\> sample-002 1804289383 846930886 1681692777 1714636915 1957747793 424238335 719885386 1649760492 596516649 1189641421 C:\usr\c\>
Download : sample-003.c ( SJIS 版 )
/*
* 2011/12/16 sample-003.c
*/
/*
*
*/
#include <stdio.h>
#include <stdlib.h>
/*
*
*/
#include "boolean.h"
#include "dlist.h"
/*
*
*/
#define COMMAND_LINE_SIZE 1024
/*
*
*/
int main ( void ) {
char cmdline [ COMMAND_LINE_SIZE ];
DList *list = new_DList();
int cmd = 'h';
double number = 0;
/*
*
*/
if ( list == NULL ) {
printf ( "List が確保できませんでしたので終了します\n" );
exit ( -1 ); /* プログラムを終了 */
}
/*
*
*/
while ( cmd != 'q' ) {
/* コマンドの実行 */
switch ( cmd ) {
default:
printf ( "I don't know %c command\n", cmd );
// break;
case 'h':
printf ( "Command Help, Quit, Print, Insert #, Append #, Check, delete \
Top, delete End, Find #\n" );
break;
case 'q':
break;
case 'p':
print_DList ( list );
printf ( "\n" );
break;
case 'i':
insert_DList ( list, number );
break;
case 'a':
append_DList ( list, number );
break;
case 'c':
if ( isEmpty_DList ( list ) == FALSE ) {
printf ( "Empty\n" );
} else {
printf ( "No Empty\n" );
}
break;
case 't':
delete_top_DList ( list );
break;
case 'e':
delete_end_DList ( list );
break;
case 'f':
if ( search_DList ( list, number ) != NULL ) {
printf ( "Found\n" );
} else {
printf ( "Not Found\n" );
}
break;
}
/* コマンドの入力 */
printf ( "cmd> " );
fgets ( cmdline, COMMAND_LINE_SIZE, stdin ); /* 一行入力 */
cmd = cmdline[0]; /* 先頭の一文字がコマンド */
number = atof ( cmdline + 2 ); /* 3 文字目以後は引数 */
}
/*
*
*/
free_DList ( list );
/*
*
*/
return 0;
}
/*
*
*/
p i 1.2 i 2.3 p a -3.4 a -4.5 p t p e p q
C:\usr\c\> sample-003< sample-003.in
Command Help, Quit, Print, Insert #, Append #, Check, delete Top, \
delete End, Find #
cmd> p
cmd> i 1.2
cmd> i 2.3
cmd> p
2.3000001.200000
cmd> a -3.4
cmd> a -4.5
cmd> p
2.3000001.200000-3.400000-4.500000
cmd> t
cmd> p
1.200000-3.400000-4.500000
cmd> e
cmd> p
1.200000-3.400000
cmd> q
C:\usr\c\>
Download : sample-004.c ( SJIS 版 )
/*
* 2011/12/16 sample-004.c
*/
/*
*
*/
#include <stdio.h>
#include <stdlib.h>
/*
*
*/
int add ( int a, int b ) {
return a + b;
}
/*
*
*/
int mul ( int a, int b ) {
return a * b;
}
/*
*
*/
int self ( int (*func)(int,int), int value ) {
/*
self は 二つの整数値から、一つの整数値を計算する
関数ポインターと、
一つの整数値を受け取る
*/
return (*func)( value, value );
/*
その関数を、引数に同じものを指定して呼び出し、
その返り値を値として返す。
*/
}
/*
*
*/
int add2 ( int value ) {
return value + 2;
}
/*
*
*/
int mul2 ( int value ) {
return value * 2;
}
/*
*
*/
int twice ( int (*func)(int), int value ) {
return (*func)( (*func)( value ) );
}
int forth ( int (*func)(int), int value ) {
return twice ( func, twice ( func, value ) );
}
/*
*
*/
int main ( void ) {
/*
*
*/
printf ( "self ( add, 3 ) = %d\n", self ( add, 3 ) );
/*
func <- add
value <- 3
=> add ( 3, 3 ) => 6
*/
printf ( "self ( mul, 3 ) = %d\n", self ( mul, 3 ) );
/*
func <- mul
value <- 3
=> mul ( 3, 3 ) => 9
*/
/*
*
*/
printf ( "add2 ( 3 ) = %d\n", add2 ( 3 ) ); /* => 5 */
printf ( "mul2 ( 3 ) = %d\n", mul2 ( 3 ) ); /* => 6 */
printf ( "twice ( add2, 3 ) = %d\n", twice ( add2, 3 ) );
/*
func <- add2
data <- 3
=> add2 ( add2 ( 3 ) ) => 7
*/
printf ( "twice ( mul2, 3 ) = %d\n", twice ( mul2, 3 ) );
/*
func <- mul2
data <- 3
=> mul2 ( mul2 ( 3 ) ) => 12
*/
printf ( "forth ( add2, 3 ) = %d\n", forth ( add2, 3 ) );
printf ( "forth ( mul2, 3 ) = %d\n", forth ( mul2, 3 ) );
/*
*
*/
return 0;
}
/*
*
*/
C:\usr\c\> sample-004 self ( add, 3 ) = 6 self ( mul, 3 ) = 9 add2 ( 3 ) = 5 mul2 ( 3 ) = 6 twice ( add2, 3 ) = 7 twice ( mul2, 3 ) = 12 forth ( add2, 3 ) = 11 forth ( mul2, 3 ) = 48 C:\usr\c\>
Download : sample-005.c ( SJIS 版 )
/*
* 2011/12/16 sample-005.c
*/
/*
*
*/
#include <stdio.h>
#include <stdlib.h>
/*
*
*/
int scountup () {
static int counter = 0; /* 静的変数として定義されている */
/* 初期化は一度だけ */
counter++; /* 関数が呼び出されると +1 される */
return counter; /* counter の値を返す : 毎回異なる */
}
/*
*
*/
int acountup () {
int counter = 0; /* static がないので auto 変数 */
/* 初期化は関数が呼び出される度に行われる */
counter++; /* 関数が呼び出されると +1 される */
return counter; /* 毎回同じ値 ( 1 ) を返す */
}
/*
*
*/
int main ( void ) {
int i;
/*
*
*/
for ( i = 0; i < 10; i++ ) {
printf ( "i=%d\n", i );
printf ( "\tacountup () = %d\n", acountup () );
printf ( "\tscountup () = %d\n", scountup () );
}
/*
*
*/
return 0;
}
/*
*
*/
C:\usr\c\> sample-005 i=0 acountup () = 1 scountup () = 1 i=1 acountup () = 1 scountup () = 2 i=2 acountup () = 1 scountup () = 3 i=3 acountup () = 1 scountup () = 4 i=4 acountup () = 1 scountup () = 5 i=5 acountup () = 1 scountup () = 6 i=6 acountup () = 1 scountup () = 7 i=7 acountup () = 1 scountup () = 8 i=8 acountup () = 1 scountup () = 9 i=9 acountup () = 1 scountup () = 10 C:\usr\c\>
Download : sample-006.c ( SJIS 版 )
/*
* 2011/12/09 sample-006.c
*/
/*
*
*/
#include <stdio.h>
#include <stdlib.h>
/*
*
*/
#include "boolean.h"
#include "icell.h"
#include "functbl.h"
#include "vlist.h"
/*
*
*/
#define COMMAND_LINE_SIZE 1024
/*
*
*/
int main ( void ) {
static Functbl ftbl = {
(ScanCell)scan_ICell,
(FreeCell)free_ICell,
(PrintCell)print_ICell
};
char cmdline [ COMMAND_LINE_SIZE ];
VList *list = new_VList( &ftbl );
int cmd = 'h';
char *number = "";
/*
*
*/
if ( list == NULL ) {
printf ( "List が確保できませんでしたので終了します\n" );
exit ( -1 ); /* プログラムを終了 */
}
/*
*
*/
while ( cmd != 'q' ) {
/* コマンドの実行 */
switch ( cmd ) {
default:
printf ( "I don't know %c command\n", cmd );
// break;
case 'h':
printf ( "Command Help, Quit, Print, Insert #, Append #, Check, delete \
Top, delete End\n" );
break;
case 'q':
break;
case 'p':
print_VList ( list );
printf ( "\n" );
break;
case 'i':
insert_VList ( list, number );
break;
case 'a':
append_VList ( list, number );
break;
case 'c':
if ( isEmpty_VList ( list ) == FALSE ) {
printf ( "Empty\n" );
} else {
printf ( "No Empty\n" );
}
break;
case 't':
delete_top_VList ( list );
break;
case 'e':
delete_end_VList ( list );
break;
}
/* コマンドの入力 */
printf ( "cmd> " );
fgets ( cmdline, COMMAND_LINE_SIZE, stdin ); /* 一行入力 */
cmd = cmdline[0]; /* 先頭の一文字がコマンド */
number = cmdline + 2; /* 3 文字目以後は引数 */
}
/*
*
*/
free_VList ( list );
/*
*
*/
return 0;
}
/*
*
*/
p i 10 i 20 p a -10 a -20 p t p e p q
C:\usr\c\> sample-006< sample-006.in
Command Help, Quit, Print, Insert #, Append #, Check, delete Top, \
delete End
cmd> p
cmd> i 10
cmd> i 20
cmd> p
20 10
cmd> a -10
cmd> a -20
cmd> p
20 10 -10 -20
cmd> t
cmd> p
10 -10 -20
cmd> e
cmd> p
10 -10
cmd> q
C:\usr\c\>
Download : sample-007.c ( SJIS 版 )
/*
* 2011/12/09 sample-007.c
*/
/*
*
*/
#include <stdio.h>
#include <stdlib.h>
/*
*
*/
#include "boolean.h"
#include "dcell.h"
#include "functbl.h"
#include "vlist.h"
/*
*
*/
#define COMMAND_LINE_SIZE 1024
/*
*
*/
int main ( void ) {
static Functbl ftbl = {
(ScanCell)scan_DCell,
(FreeCell)free_DCell,
(PrintCell)print_DCell
};
char cmdline [ COMMAND_LINE_SIZE ];
VList *list = new_VList( &ftbl );
int cmd = 'h';
char *number = "";
/*
*
*/
if ( list == NULL ) {
printf ( "List が確保できませんでしたので終了します\n" );
exit ( -1 ); /* プログラムを終了 */
}
/*
*
*/
while ( cmd != 'q' ) {
/* コマンドの実行 */
switch ( cmd ) {
default:
printf ( "I don't know %c command\n", cmd );
// break;
case 'h':
printf ( "Command Help, Quit, Print, Insert #, Append #, Check, delete \
Top, delete End\n" );
break;
case 'q':
break;
case 'p':
print_VList ( list );
printf ( "\n" );
break;
case 'i':
insert_VList ( list, number );
break;
case 'a':
append_VList ( list, number );
break;
case 'c':
if ( isEmpty_VList ( list ) == FALSE ) {
printf ( "Empty\n" );
} else {
printf ( "No Empty\n" );
}
break;
case 't':
delete_top_VList ( list );
break;
case 'e':
delete_end_VList ( list );
break;
}
/* コマンドの入力 */
printf ( "cmd> " );
fgets ( cmdline, COMMAND_LINE_SIZE, stdin ); /* 一行入力 */
cmd = cmdline[0]; /* 先頭の一文字がコマンド */
number = cmdline + 2; /* 3 文字目以後は引数 */
}
/*
*
*/
free_VList ( list );
/*
*
*/
return 0;
}
/*
*
*/
p i 1.2 i 2.3 p a -3.4 a -4.5 p t p e p q
C:\usr\c\> sample-007< sample-007.in
Command Help, Quit, Print, Insert #, Append #, Check, delete Top, \
delete End
cmd> p
cmd> i 1.2
cmd> i 2.3
cmd> p
2.300000 1.200000
cmd> a -3.4
cmd> a -4.5
cmd> p
2.300000 1.200000 -3.400000 -4.500000
cmd> t
cmd> p
1.200000 -3.400000 -4.500000
cmd> e
cmd> p
1.200000 -3.400000
cmd> q
C:\usr\c\>
Download : xalloc.h ( SJIS 版 )
/* * xalloc.h */ #ifndef __XALLOC_H__ #define __XALLOC_H__ /* * */ #define xalloc(Type) ((Type *)malloc(sizeof(Type))) /* 指定した型の領域を一つ分 alloc し、かつその型にキャストして返す */ #define xfree(PTR) (free(PTR)) /* * */ #endif
Download : boolean.h ( SJIS 版 )
/* * boolean.h */ #ifndef __BOOLEAN_H__ #define __BOOLEAN_H__ /* * */ #define FALSE (0) /* 偽 (0) */ #define TRUE (!FALSE) /* 真 (0 以外) */ /* * */ #define Boolean int /* 真偽値は、単なる整数値 */ /* * */ #endif
/*
* icell.h
*/
#ifndef __ICELL_H__
#define __ICELL_H__
/*
*
*/
typedef struct icell { /* 構造体には構造体を区別するタグ名を付ける事ができる */
struct icell *next; /* 次の ICell へのポインター */
/* 本当は「ICell *」と書きたいが、この時点では存在しないので.. */
int data; /* 要素 ( int 型 ) */
} ICell;
/*
*
*/
extern ICell *new_ICell ( int value );
extern void free_ICell ( ICell *object );
extern void print_ICell ( ICell *object );
extern ICell *scan_ICell ( char *svalue );
/*
*
*/
#endif
Can not access : program/icell.out
/*
* ilist.h
*/
#ifndef __ILIST_H__
#define __ILIST_H__
/*
*
*/
#ifndef __ICELL_H__
#include "icell.h"
#endif
#ifndef __BOOLEAN_H__
#include "boolean.h"
#endif
/*
*
*/
typedef struct {
ICell *top;
} IList;
/*
*
*/
extern IList *new_IList (void);
extern Boolean isEmpty_IList ( IList *object );
extern void free_IList ( IList *object );
extern void print_IList ( IList *object );
extern IList *insert_IList ( IList *object, int data );
extern void delete_top_IList ( IList *object );
extern IList *append_IList ( IList *object, int data );
extern void delete_end_IList ( IList *object );
extern ICell *search_IList ( IList *object, int number );
/*
*
*/
#endif
Can not access : program/ilist.out
/*
* dcell.h
*/
#ifndef __DCELL_H__
#define __DCELL_H__
/*
*
*/
typedef struct dcell { /* 構造体には構造体を区別するタグ名を付ける事ができる */
struct dcell *next; /* 次の DCell へのポインター */
/* 本当は「DCell *」と書きたいが、この時点では存在しないので.. */
double data; /* 要素 ( double 型 ) */
} DCell;
/*
*
*/
extern DCell *new_DCell ( double value );
extern void free_DCell ( DCell *object );
extern void print_DCell ( DCell *object );
extern DCell *scan_DCell ( char *svalue );
/*
*
*/
#endif
Can not access : program/dcell.out
/*
* dlist.h
*/
#ifndef __DLIST_H__
#define __DLIST_H__
/*
*
*/
#ifndef __DCELL_H__
#include "dcell.h"
#endif
#ifndef __BOOLEAN_H__
#include "boolean.h"
#endif
/*
*
*/
typedef struct {
DCell *top;
} DList;
/*
*
*/
extern DList *new_DList (void);
extern Boolean isEmpty_DList ( DList *object );
extern void free_DList ( DList *object );
extern void print_DList ( DList *object );
extern DList *insert_DList ( DList *object, double data );
extern void delete_top_DList ( DList *object );
extern DList *append_DList ( DList *object, double data );
extern void delete_end_DList ( DList *object );
extern DCell *search_DList ( DList *object, double number );
/*
*
*/
#endif
Can not access : program/dlist.out
先週やらなかった課題を今週の課題とする