ページ 11

unsigned long intについて

Posted: 2011年2月10日(木) 22:33
by ZZZ
このプログラムのunsigned long int timeToStrikeというのは何のために使っているのでしょうか?
よろしければ教えてください

コード:

#include <stdio.h>
#include <stdlib.h>

struct LIST
{
	int year,month,day,h,min;          						/* 具体的なデータ */
	char week,name,course; 
	struct LIST *next;  									/* 次の要素へのポインタ */
};
struct LIST head; 											 /* リストの先頭要素(ダミー) */

															/* プロトタイプ宣言 */
void add_r(void);
void showlist(void);
unsigned long int timeToStrike( struct LIST * p );

int main(void)
{
	char command;
	
	head.next = NULL;
	
	do
	{
		puts("コマンドを入力してください");
		puts("線形リストに要素を追加:a");
		puts("線形リストの内容を示:c");
		puts("終了q");
		scanf( "%c", &command );
		
		switch( command )
		{
		case 'a':   /* 追加 */
			add_r();
			break;
		case 'c':   /* 表示 */
			showlist();
			break;
		case 'q':   /* 終了 */
			break;
		default:    /* 無効 */
			puts( "コマンドが正しくありません" );
			break;
		}
		
		puts( "" );
		fflush( stdin );
		
	}while( command != 'q' );
	
	return 0;
}

unsigned long int timeToStrike( struct LIST * p ) {
	return ( ( ( p->year * 12 + p->month ) * 30 + p->day ) * 24 + p->h ) * 60 + p->min;
}

															/* 線形リストに要素を追加する */
void add_r(void)
{
	struct LIST *p, *q, *newcell;
	char week,name,course;
	int year,month,day,h,min;

	puts("何年ですか?");
	scanf( "%d", &year);
	puts("何月ですか?");
	scanf( "%d", &month);
	puts("何日ですか?");
	scanf("%d",&day);
	puts("何時ですか?");
	scanf("%d",&h);
	puts("何分ですか?");
	scanf("%d",&min);

															/* 新しく追加する要素のためのメモリ領域を確保する */
	newcell = malloc( sizeof(struct LIST) );
	if( newcell == NULL )
	{
		puts( "メモリ不足" );
		return;
	}

	newcell->year = year; 									/* 新しい要素のデータを設定 */
	newcell->month = month;
	newcell->day = day;
	newcell->h = h;
	newcell->min = min;

	p = &head;
	while ( p->next ) {
		if ( timeToStrike( newcell ) < timeToStrike( p->next ) ) {
			break;
		}
		p = p->next;
	}
	newcell->next = p->next;
	p->next = newcell;

	for( p=head.next; p!=NULL; p=p->next )
	{
		printf( "%d年 %d月 %d日 %d時%02d分~\n", p->year,p->month,p->day,p->h,p->min);
	}
}
																	/* 線形リストから要素を削除する */
																						/* 線形リストの内容を表示する */
void showlist(void)
{
	struct LIST *p;
	
	for( p=head.next; p!=NULL; p=p->next )
	{
		printf( "%d年 %d月 %d日 %d時%02d分~\n", p->year,p->month,p->day,p->h,p->min);
	}
}

Re: unsigned long intについて

Posted: 2011年2月10日(木) 23:36
by HolyWings
ZZZ さんが書きました:unsigned long int timeToStrike( struct LIST * p );
と言う関数は、 struct LIST の 'year','month','day','h','min' を分単位に変換する関数です。

例えば、1時間なら60分 ( 1 * 60 ) 、1日なら 1440分( 1 * 24 * 60 ) のように
struct LIST の 時間のデータを分単位に変換しています。