カウントダウン時計の作り方2
C言語ケーススタディ

Study C Ver2販売開始のお知らせ

 C言語の勉強や教育用に最適です。
 2004/8/23よりStudy C Ver2の販売を開始しました。
 Study C Ver2の新しい機能については
 こちらをご参照ください。
C言語でゲームプログラマーを目指す方へ
 ■C言語ゲームプログラミング講座
 C言語でのゲームプログラミングを解説する講座
 始めました。

前回作成したプログラムを改造しループで残りの時刻をカウントするように改造します。 時計の作り方1の時と同様に残り秒数が変化しないときは表示しないようにします。


#include <stdio.h>
#include <time.h>

time_t  get_caltime(char *tstr);

main()
{
	time_t  cal_time, now_time, inter, prev;
	char    buff[100];
	int	day, hour, min, sec;
	struct tm
		*tm_now;

	printf("YYYYMMDDHHMMSS : ");
	scanf("%s", buff);
	cal_time = get_caltime(buff);
	if(cal_time == -1){
		printf("変換失敗\n");
		return;
	}
	now_time = time(NULL);
	if(cal_time <= now_time){
		printf("入力した日時が不適切です.\n");
		return;
	}

	printf("\033[2J");
	tm_now = localtime(&cal_time);
	printf("%4d年%2d月%2d日 %2d時%2d分%2d秒まで\n",
		tm_now->tm_year+1900, tm_now->tm_mon+1, tm_now->tm_mday,
		tm_now->tm_hour, tm_now->tm_min, tm_now->tm_sec);

	prev = -1;
	for(;;){
		now_time = time(NULL);
		if(prev == now_time)
			continue;
		prev = now_time;
		inter = cal_time - now_time;
		if(inter < 0)
			break;
		sec = inter % 60;
		inter /= 60;
		min = inter % 60;
		inter /= 60;
		hour = inter % 24;
		day = inter / 24;
		printf("あと%d日と%2d時間%2d分%2d秒\n", day, hour, min, sec);       
	}
}


time_t  get_caltime(char *tstr)
{
	char    buf[20];
	time_t  cal_time;
	struct tm
		work_tm;

	if(strlen(tstr) != 14)
		return(-1);
	strncpy(buf, tstr, 4);
	buf[4] = '\0';
	work_tm.tm_year = atoi(buf) - 1900;
	strncpy(buf, tstr+4, 2);
	buf[2] = '\0';
	work_tm.tm_mon = atoi(buf) - 1;
	strncpy(buf, tstr+4+2, 2);
	work_tm.tm_mday = atoi(buf);
	strncpy(buf, tstr+4+2+2, 2);
	work_tm.tm_hour = atoi(buf);
	strncpy(buf, tstr+4+2+2+2, 2);
	work_tm.tm_min = atoi(buf);
	strncpy(buf, tstr+4+2+2+2+2, 2);
	work_tm.tm_sec = atoi(buf);
	work_tm.tm_isdst = -1;
	if((cal_time = mktime(&work_tm)) == -1){
		return(-1);
	}
	return(cal_time);
}
Study Cにロードする Study Cにロードし編集する Study Cにロードし実行する ブラウザとの連携機能が使用可能なStudy Cのバージョンなどについて...

最後に時計の作り方2と同じように画面の中央に表示し同様に残り秒数が変化しないときは表示しないように改造します。


#include <stdio.h>
#include <time.h>

time_t  get_caltime(char *tstr);

main()
{
	time_t  cal_time, now_time, inter, prev;
	char    buff[100];
	int	day, hour, min, sec;
	struct tm
		*tm_now;

	printf("YYYYMMDDHHMMSS : ");
	scanf("%s", buff);
	cal_time = get_caltime(buff);
	if(cal_time == -1){
		printf("変換失敗\n");
		return;
	}
	now_time = time(NULL);
	if(cal_time <= now_time){
		printf("入力した日時が不適切です.\n");
		return;
	}

	printf("\033[2J");
	tm_now = localtime(&cal_time);
	printf("\033[10;25H");
	printf("%4d年%2d月%2d日 %2d時%2d分%2d秒まで\n",
		tm_now->tm_year+1900, tm_now->tm_mon+1, tm_now->tm_mday,
		tm_now->tm_hour, tm_now->tm_min, tm_now->tm_sec);

	prev = -1;
	for(;;){
		if(kbhit()){
			if(getch() == 0x1B)
				break;
		}
		now_time = time(NULL);
		if(prev == now_time)
			continue;
		prev = now_time;
		inter = cal_time - now_time;
		if(inter < 0)
			break;
		sec = inter % 60;
		inter /= 60;
		min = inter % 60;
		inter /= 60;
		hour = inter % 24;
		day = inter / 24;
		printf("\033[11;25H");
		printf("あと%d日と%2d時間%2d分%2d秒", day, hour, min, sec);        
		printf("\033[0K");
	}
}


time_t  get_caltime(char *tstr)
{
	char    buf[20];
	time_t  cal_time;
	struct tm
		work_tm;

	if(strlen(tstr) != 14)
		return(-1);
	strncpy(buf, tstr, 4);
	buf[4] = '\0';
	work_tm.tm_year = atoi(buf) - 1900;
	strncpy(buf, tstr+4, 2);
	buf[2] = '\0';
	work_tm.tm_mon = atoi(buf) - 1;
	strncpy(buf, tstr+4+2, 2);
	work_tm.tm_mday = atoi(buf);
	strncpy(buf, tstr+4+2+2, 2);
	work_tm.tm_hour = atoi(buf);
	strncpy(buf, tstr+4+2+2+2, 2);
	work_tm.tm_min = atoi(buf);
	strncpy(buf, tstr+4+2+2+2+2, 2);
	work_tm.tm_sec = atoi(buf);
	work_tm.tm_isdst = -1;
	if((cal_time = mktime(&work_tm)) == -1){
		return(-1);
	}
	return(cal_time);
}
Study Cにロードする Study Cにロードし編集する Study Cにロードし実行する ブラウザとの連携機能が使用可能なStudy Cのバージョンなどについて...

お問い合わせ先 C言語のトップページに戻る Copyright(C) 2003 潟Iーキッド