Powered by SmartDoc

ソフトウェア概論A/B (2017/12/22)
Ver. 1.0

2017年12月22日
栗野 俊一
kurino@math.cst.nihon-u.ac.jp
http://edu-gw2.math.cst.nihon-u.ac.jp/~kurino/2017/soft/soft.html
ソフトウェア概論 A/B2017年12月22日 の資料

目次

講義資料

当日の OHP 資料

講議中に作成したプログラム

本日の課題

課題 20171222-01 : 整数値のキュー(queue)

Download : 20171222-01.c

20171222-01.c
/*
 * 課題 CNAME-01
 *
 * 2017/12/22 FILENAME
 *
 *	整数のキュー(queue)
 */

#include <stdio.h>
#include <malloc.h>

/*
 *  整数のキュー(queue)
 *
 * 利用方法
 *		コンパイル
 *			cc -o BASENAME.exe FILENAME
 *		実行
 *			./BASENAME.exe
 */

/*
 * Queue は、List の先頭に追加し、最後の要素を削除する事で実現
 */

typedef struct icell {
	struct icell *next;
	int value;
} ICell;

typedef struct {
	ICell *head;
	ICell *tail;
} IQueue;

/*
*/

#define	NORMAL	(0)
#define	ERROR	(-1)

/*
*/

#define	FALSE	(0)
#define	TRUE	(!FALSE)

/*
 * alloc_icell
 */

ICell *alloc_icell ( int data ) {
	ICell *result = (ICell *)malloc(sizeof(ICell));

	if ( result != NULL ) {
		result -> next = NULL;

	/*
	**	 この部分を完成させなさい
	*/

	}

	return result;
}

/*
 * free_icell
 */

void free_icell ( ICell *icp ) {

	free ( icp );
}

/*
 * alloc_iqueue
 */

IQueue *alloc_iqueue ( ) {
	IQueue *result = (IQueue *)malloc(sizeof(IQueue));

	if ( result != NULL ) {
		result -> head = result -> tail = NULL;
	}

	return result;
}

/*
 * is_empty
 */

int is_empty ( IQueue *iqp ) {

	if ( iqp != NULL ) {
		return iqp -> head == NULL;
	}

	return FALSE;
}

/*
 * enque
 */

int enque ( IQueue *iqp, int data ) {
	int result = ERROR;

	if ( iqp != NULL ) {
		ICell *icp = alloc_icell ( data );

		if ( icp != NULL ) {
			if ( is_empty ( iqp ) ) {
				iqp -> head = icp;
			} else {

	/*
	**	 この部分を完成させなさい
	*/

			}
			iqp -> tail = icp;
			result = NORMAL;
		}
	}

	return result;
}

int deque ( IQueue *iqp ) {
	int result = ERROR;

	if ( !is_empty ( iqp ) ) {
		ICell *top = iqp -> head;

		result = top -> value;
		if ( ( iqp -> head = top -> next) == NULL ) {
		   iqp -> tail = NULL;
		}
		free ( top );
	}

	return result;
}

void free_que ( IQueue *iqp ) {

	if ( iqp != NULL ) {
		ICell *icp = iqp -> head;
		ICell *next;

		while ( icp != NULL ) {
			next = icp -> next;
			free_icell ( icp );
			icp = next;
		}

		free ( iqp );
	}
}

void print_que ( IQueue *iqp ) {

	if ( iqp != NULL ) {
		ICell *icp = iqp -> head;
		ICell *next;

		while ( icp != NULL ) {
			next = icp -> next;
			printf ( "%d ", icp -> value );

	/*
	**	 この部分を完成させなさい
	*/

		}
	}
}

/*
 * main
 */

int main ( void ) {
	
	IQueue *iqp = alloc_iqueue();

	print_que ( iqp ); putchar ( '\n' );

	enque ( iqp, 1 );

	print_que ( iqp ); putchar ( '\n' );

	enque ( iqp, 2 );
	enque ( iqp, 3 );
	print_que ( iqp ); putchar ( '\n' );

	printf ( "%d\n", deque ( iqp ) );
	print_que ( iqp ); putchar ( '\n' );

	free_que ( iqp );

  return 0;
}
20171222-01.c の実行結果
$ ./20171222-01-QQQQ.exe

1 
1 2 3 
1
2 3 
$ 

課題 20171222-02 : 二つのファイルを比較して最初に異る場所を表示する

Download : 20171222-02.c

20171222-02.c
/*
 * 20171222-02-QQQQ.c
 *	二つのファイルを比較して最初に異る場所を表示する
 */

#include <stdio.h>

/*
 * main
 */

int main ( int argc, char *argv[] ) {

  if ( argc != 3 ) {
  	 printf ( "ファイル名を二つ指定して下さい。\n" );
  } else {
    FILE *fp1;

    if ( ( fp1 = fopen ( argv[1], "r" ) ) == NULL ) {
  	  printf ( "ファイル(%s)を開く事ができませんでした。\n", argv[1] );
    } else {
	  FILE *fp2;

	  if ( ( fp2 = fopen ( argv[2], "r" ) ) == NULL ) {
  	  	 printf ( "ファイル(%s)を開く事ができませんでした。\n", argv[2] );
	  } else {
	  	  int position;
		  int ch1;
		  int ch2;

		  for ( position = 0; (ch1 = fgetc (fp1)) != EOF; position++ ) {
		  	  ch2 = fgetc( fp2 );

			  if ( ch1 != ch2 ) {
			  	 break;
			  }
		  }

		  if ( ch1 == EOF ) {
		  	  ch2 = fgetc( fp2 );
		  }

		  if ( ch1 == ch2 ) {
		  	 printf ( "二つのファイル(%s,%s)は同じ内容です。\n", argv[1], argv[2] );
		  } else if ( ch1 == EOF ) {

	/*
	**	 この部分を完成させなさい
	*/

		  } else if ( ch2 == EOF ) {
			printf ( "ファイル(%s) の方がサイズが大きいです。\n", argv[1] );
		  } else {
			printf ( "%d byte 目で %s は %c, %s は %c という違いがありました。\n", position, argv[1], ch1, argv[2], ch2 );
		  }

	  	  fclose ( fp2 );
	  }

	  fclose ( fp1 );
    }
  }

  return 0;
}
20171222-02.c の実行結果
$ ./20171222-02-QQQQ.exe data.01 data.02
6 byte 目で data.01 は y, data.02 は Y という違いがありました。
$ 

Links

関連 Link