C & C++/C언어 예제 및 소스
1/100 초 단위의 시계
언휴
2023. 12. 28. 10:50
유튜브 동영상 강의
소개
안녕하세요. 언제나 휴일입니다.
이번에는 1/100초 단위로 현재 시각을 콘솔 화면에 출력하는 시계 소스 코드입니다.
이를 통해 time.h 에 정의하고 있는 다양한 일시와 시간에 관한 형식과 함수 사용 방법을 알 수 있습니다.
소스 코드
/* https://ehpub.co.kr
언제나 C언어 예제 Center
1/100초 단위의 시계 구현
*/
#include
#include
#include
#include
void print_time(struct tm* now, int tail)
{
COORD CursorPostion = { 0,1 };
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), CursorPostion);
printf("%2d시 %2d분 %2d초 %2d\n", now->tm_hour, now->tm_min, now->tm_sec, tail);
}
int main()
{
clock_t sclock, nclock;
time_t seconds;
struct tm now;
int tail=0;
printf("종료를 원하시면 아무키나 누르세요.\n");
sclock = clock();
time(&seconds);
localtime_s(&now, &seconds);
print_time(&now, tail);
while (1)
{
if (_kbhit())
{
break;
}
nclock = clock();
if (nclock - sclock >= (CLOCKS_PER_SEC / 100))
{
tail++;
if (tail == 100)//1초가 지나면
{
tail = 0;
sclock = clock();
time(&seconds);
localtime_s(&now, &seconds);
}
print_time(&now, tail);
}
}
printf("프로그램을 종료합니다.");
return 0;
}
언제나휴일 여행 및 산책