C/명품 C언어 프로젝트(안기수 저)

명품 C언어 프로젝트 10.1장 디지털 시계

꾸준함. 2017. 9. 25. 23:08

/*

현재의 시간을 디지털 숫자로 출력

오전 오후 표시하고 시간은 항상 6자리로 출력합니다!

*/

#include <stdio.h>

#include <time.h>

#include <stdlib.h>

#include <conio.h>

#include <Windows.h>

 

long time_to_number(void);

void number_check(int k, int i);

void digit_print(int dim[], int line);

void gotoxy(int x, int y);

void dot_print(int hour);

 

int zero[20] = { 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1 };

int one[20] = { 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1 };

int two[20] = { 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1 };

int three[20] = { 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1 };

int four[20] = { 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1 };

int five[20] = { 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1 };

int six[20] = { 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1 };

int seven[20] = { 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1 };

int eight[20] = { 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1 };

int nine[20] = { 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1 };

 

int main(void)

{

        int line;

        long time_number;

        printf("디지털 시계 프로그램\n\n");

        printf("아무키나 누르면 현재시간을 출력합니다.\n");

        getch();

        system("cls");

        while (!kbhit())

        {

               gotoxy(35, 1);

               time_number = time_to_number();

               if (time_number >= 120000)

                       printf("오후");

               else

                       printf("오전");

               gotoxy(1, 3);

               for (line = 0; line <= 4; line++)

               {

                       number_check(time_number, line);

                       printf("\n");

               }

               dot_print(time_number);

               printf("\n\n\n\n아무키나 누르면 종료합니다.\n");

        }

        return 0;

}

 

void gotoxy(int x, int y)

{

        COORD Pos = { x - 1, y - 1 };

        SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), Pos);

}

 

/*

시분초 단위의 값을 하나의 정수로 변환하는 함수 time_to_number

*/

long time_to_number(void)

{

        time_t current;

        struct tm *d;

        int hour, min, sec;

        current = time(NULL);

        d = localtime(&current);

        hour = d->tm_hour * 10000;

        min = d->tm_min * 100;

        sec = d->tm_sec;

        return hour + min + sec;

}

 

//디지털 숫자의 출력

void digit_print(int dim[], int line)

{

        for (int i = line * 4; i <= line * 4 + 3; i++)

               if (dim[i] == 1)

                       printf("");

               else

                       printf("  ");

        printf("    ");

}

 

//숫자에 대한 분리

void number_check(int k, int i)

{

        if (!k>=100000) //시간이 두자리수가 아닌 경우

        {

               digit_print(zero, i); //0을 출력

               number_check(k / 10, i);

               switch (k % 10)

               {

               case 0:

                       digit_print(zero, i);

                       break;

               case 1:

                       digit_print(one, i);

                       break;

               case 2:

                       digit_print(two, i);

                       break;

               case 3:

                       digit_print(three, i);

                       break;

               case 4:

                       digit_print(four, i);

                       break;

               case 5:

                       digit_print(five, i);

                       break;

               case 6:

                       digit_print(six, i);

                       break;

               case 7:

                       digit_print(seven, i);

                       break;

               case 8:

                       digit_print(eight, i);

                       break;

               case 9:

                       digit_print(nine, i);

                       break;

               }

        }

        else if (k >= 1)

        {

               number_check(k / 10, i);

               switch (k % 10)

               {

               case 0:

                       digit_print(zero, i);

                       break;

               case 1:

                       digit_print(one, i);

                       break;

               case 2:

                       digit_print(two, i);

                       break;

               case 3:

                       digit_print(three, i);

                       break;

               case 4:

                       digit_print(four, i);

                       break;

               case 5:

                       digit_print(five, i);

                       break;

               case 6:

                       digit_print(six, i);

                       break;

               case 7:

                       digit_print(seven, i);

                       break;

               case 8:

                       digit_print(eight, i);

                       break;

               case 9:

                       digit_print(nine, i);

                       break;

               }

        }

}

 

/*

시분초 사이의 구분을 위한 도트 출력 함수 dot_print

*/

void dot_print(int hour)

{

        int i;

        for (i = 0; i < 2; i++)

        {

               gotoxy(22 + (i * 24), 4);

               printf("");

               gotoxy(22 + (i * 24), 6);

               printf("");

        }

}

 


개발환경:Visual Studio 2017


지적, 조언, 질문 환영입니다! 댓글 남겨주세요~


[참고] 명품 C언어 프로젝트 안기수 저

반응형