NAVER

질문 c언어 실시간 시계
비공개 조회수 1,224 작성일2019.05.29
아래와 같은 코드를 실행하면 현재 시간이 실시간으로 나오고 
키보드를 입력받으면 꺼지게 됩니다.

이 함수를 시간 위에 당일 날짜도 함께 디지털로 출력되게끔하고 ex)2019/05/29
00:00:00
현재 이 함수를 다른함수를 실행해도 시간이 계속 지나가게끔 할 수 있나요?
방법이나 코드좀 알려주세요 !




#include <Windows.h>
#include <stdio.h>
#include <time.h>
#include <conio.h>
#define N 256*256
#pragma warning(disable:4996)
char digits[10][5][4][3] =//0~9까지 출력할 정보
{
{
{ "■","■","■","■" },
{ "■","  ","  ","■" },
{ "■","  ","  ","■" },
{ "■","  ","  ","■" },
{ "■","■","■","■" }
},
{
{ "  ","  ","  ","■" },
{ "  ","  ","  ","■" },
{ "  ","  ","  ","■" },
{ "  ","  ","  ","■" },
{ "  ","  ","  ","■" }
},
{
{ "■","■","■","■" },
{ "  ","  ","  ","■" },
{ "■","■","■","■" },
{ "■","  ","  ","  " },
{ "■","■","■","■" }
},
{
{ "■","■","■","■" },
{ "  ","  ","  ","■" },
{ "■","■","■","■" },
{ "  ","  ","  ","■" },
{ "■","■","■","■" }
},
{
{ "■","  ","■","  " },
{ "■","  ","■","  " },
{ "■","■","■","■" },
{ "  ","  ","■","  " },
{ "  ","  ","■","  " }
},
{
{ "■","■","■","■" },
{ "■","  ","  ","  " },
{ "■","■","■","■" },
{ "  ","  ","  ","■" },
{ "■","■","■","■" },
},
{
{ "■","  ","  ","  " },
{ "■","  ","  ","  " },
{ "■","■","■","■" },
{ "■","  ","  ","■" },
{ "■","■","■","■" }
},
{
{ "■","■","■","■" },
{ "■","  ","  ","■" },
{ "■","  ","  ","■" },
{ "  ","  ","  ","■" },
{ "  ","  ","  ","■" }
},
{
{ "■","■","■","■" },
{ "■","  ","  ","■" },
{ "■","■","■","■" },
{ "■","  ","  ","■" },
{ "■","■","■","■" }
},
{
{ "■","■","■","■" },
{ "■","  ","  ","■" },
{ "■","■","■","■" },
{ "  ","  ","  ","■" },
{ "  ","  ","  ","■" }
}
};

int sx[6] = { 0,10,24,34,48,58 };//시분초를 출력할 x좌표 

char *colons[5] = { " ","■","  ","■","  " };
int sx2[6] = { 20,44 };//콜론을 출력할 좌표

void gotoxy1(int x, int y)
{
COORD Pos = { x,y };
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), Pos);
}


//dn자리에 정수 n을 출력하는 함수
void DrawNum(int dn, int n)
{
int y, x;

for (y = 0; y < 5; y++)
{
for (x = 0; x < 4; x++)
{
gotoxy1(sx[dn] + x * 2, y);
printf("%s", digits[n][y][x]);
}
printf("\n");
}
}


//콜론을 출력하는함수 n이 0이면 시와 분사이, 1이면 분과 초사이
void DrawColon(int n)
{
int y = 0;


for (y = 0; y < 5; y++)
{
gotoxy1(sx2[n], y);
printf("%s\n", colons[y]);
}

}
void callender()
{
time_t timer;
struct tm *t;

timer = time(NULL);
t = localtime(&timer);

int year = (t->tm_year + 1900) % 10000;
int month = t->tm_mon + 1;
int day = t->tm_mday;

char digitaldate[11];
sprintf(digitaldate, "%04d.%02d.%02d", year, month, day);

//digital_print(digitaldate);
printf("\n");
}
void DrawTime(int h, int m, int s)
{
DrawNum(0, h / 10);//시의 앞자리
DrawNum(1, h % 10);//시의 뒷자리
DrawColon(0);//콜론
DrawNum(2, m / 10);//분의 앞자리
DrawNum(3, m % 10);//분의 뒷자리
DrawColon(1);//콜론
DrawNum(4, s / 10);//초의 앞자리
DrawNum(5, s % 10);//초의 뒷자리
}


void now_time()
{

time_t now, before;
struct tm nt;

gotoxy1(0, 8);

now = before = time(0); //초 단위 시간을 구함
localtime_s(&nt, &now); //지역 시각을 구함
DrawTime(nt.tm_hour, nt.tm_min, nt.tm_sec);//현재 시각을 출력
while (kbhit() == 0)
{
now = time(0);//초단위 시간을 구함
if (now != before)//다르면
{
before = now;//현재 초단위 시간을 기억
localtime_s(&nt, &now);//지역 시각을 구함
DrawTime(nt.tm_hour, nt.tm_min, nt.tm_sec);//현재 시각을 출력
}
}
}
void main()
{
now_time();
}




프로필 사진

답변자님,

정보를 공유해 주세요.

1 개 답변
1번째 답변
프로필 사진
NoOneElseME
달신
IT/인터넷업 #IT #C #Car C, C++ 45위, 컴퓨터주변기기, 전기, 전자 공학 분야에서 활동
본인 입력 포함 정보

아래 소스를 참고하세요.

다른 함수를 불러도 계속 시간에 가게 한다는건 시간을 출력하는 함수와 다른 함수가 같은 루프 안에 들어있어야 합니다.

소스상으로는 now_time()함수의 while (kbhit() == 0) 에서 무한루프를 돌면서 시간을 출력하고 있으므로 해당 루프 안에 다른 함수를 부르면 되겠네요.(단 부르는 함수에서 scanf 등의 함수를 사용해서 뭔가 사용자 입력을 기다리거나 혹은 수행이 오래 걸리는 부분이 없어야 시간이 제대로 출력되겠지요.)

#include <Windows.h> #include <stdio.h> #include <time.h> #include <conio.h> #define N 256*256 #pragma warning(disable:4996) char digits[11][5][4][3] =//0~9까지 출력할 정보 { { { "■","■","■","■" }, { "■"," "," ","■" }, { "■"," "," ","■" }, { "■"," "," ","■" }, { "■","■","■","■" } }, { { " "," "," ","■" }, { " "," "," ","■" }, { " "," "," ","■" }, { " "," "," ","■" }, { " "," "," ","■" } }, { { "■","■","■","■" }, { " "," "," ","■" }, { "■","■","■","■" }, { "■"," "," "," " }, { "■","■","■","■" } }, { { "■","■","■","■" }, { " "," "," ","■" }, { "■","■","■","■" }, { " "," "," ","■" }, { "■","■","■","■" } }, { { "■"," ","■"," " }, { "■"," ","■"," " }, { "■","■","■","■" }, { " "," ","■"," " }, { " "," ","■"," " } }, { { "■","■","■","■" }, { "■"," "," "," " }, { "■","■","■","■" }, { " "," "," ","■" }, { "■","■","■","■" }, }, { { "■"," "," "," " }, { "■"," "," "," " }, { "■","■","■","■" }, { "■"," "," ","■" }, { "■","■","■","■" } }, { { "■","■","■","■" }, { "■"," "," ","■" }, { "■"," "," ","■" }, { " "," "," ","■" }, { " "," "," ","■" } }, { { "■","■","■","■" }, { "■"," "," ","■" }, { "■","■","■","■" }, { "■"," "," ","■" }, { "■","■","■","■" } }, { { "■","■","■","■" }, { "■"," "," ","■" }, { "■","■","■","■" }, { " "," "," ","■" }, { " "," "," ","■" } }, { { " "," "," ","■" }, { " "," ","■"," " }, { " ","■","■"," " }, { " ","■"," "," " }, { "■"," "," "," " } } }; int sx[6] = { 0,10,24,34,48,58 };//시분초를 출력할 x좌표 int sx3[10] = { 0,10,20,30,40,50,60,70,80,90 }; const char *colons[5] = { " ","■"," ","■"," " }; int sx2[6] = { 20,44 };//콜론을 출력할 좌표 void gotoxy1(int x, int y) { COORD Pos = { x,y }; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), Pos); } //dn자리에 정수 n을 출력하는 함수 void DrawNum(int dn, int n) { int y, x; for (y = 0; y < 5; y++) { for (x = 0; x < 4; x++) { gotoxy1(sx[dn] + x * 2, y+8); printf("%s", digits[n][y][x]); } // printf("\n"); } } void DrawNum2(int dn, int n) { int y, x; for (y = 0; y < 5; y++) { for (x = 0; x < 4; x++) { gotoxy1(sx3[dn] + x * 2, y); printf("%s", digits[n][y][x]); } //printf("\n"); } } //콜론을 출력하는함수 n이 0이면 시와 분사이, 1이면 분과 초사이 void DrawColon(int n) { int y = 0; for (y = 0; y < 5; y++) { gotoxy1(sx2[n], y+8); printf("%s\n", colons[y]); } } void callender() { time_t timer; struct tm *t; timer = time(NULL); t = localtime(&timer); int year = (t->tm_year + 1900) % 10000; int month = t->tm_mon + 1; int day = t->tm_mday; char digitaldate[11]; sprintf(digitaldate, "%04d:%02d:%02d", year, month, day); for(int x=0;x<10;x++) DrawNum2(x, digitaldate[x]-48); //digital_print(digitaldate); //printf("\n"); } void DrawTime(int h, int m, int s) { DrawNum(0, h / 10);//시의 앞자리 DrawNum(1, h % 10);//시의 뒷자리 DrawColon(0);//콜론 DrawNum(2, m / 10);//분의 앞자리 DrawNum(3, m % 10);//분의 뒷자리 DrawColon(1);//콜론 DrawNum(4, s / 10);//초의 앞자리 DrawNum(5, s % 10);//초의 뒷자리 } void now_time() { time_t now, before; struct tm nt; gotoxy1(0, 8); now = before = time(0); //초 단위 시간을 구함 localtime_s(&nt, &now); //지역 시각을 구함 callender(); DrawTime(nt.tm_hour, nt.tm_min, nt.tm_sec);//현재 시각을 출력 while (kbhit() == 0) { now = time(0);//초단위 시간을 구함 if (now != before)//다르면 { callender(); before = now;//현재 초단위 시간을 기억 localtime_s(&nt, &now);//지역 시각을 구함 DrawTime(nt.tm_hour, nt.tm_min, nt.tm_sec);//현재 시각을 출력 } } } void main() { system("mode con cols=110 lines=30"); now_time(); }

2019.05.29.

  • 채택

    질문자가 채택한 답변입니다.

도움이 되었다면 UP 눌러주세요!
UP이 많은 답변일수록 사용자들에게 더 많이 노출됩니다.