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

명품 C언어 프로젝트 8.3장 디지털 스탑워치

꾸준함. 2017. 9. 1. 18:56

/*

디지털 stopwatch의 시간 표시부분을 디지털 숫자로 출력하도록 프로그램을 수정하시오

*/

#include <stdio.h>

#include <stdlib.h>

#include <time.h>

#include <math.h>

#include <conio.h>

 

void time_pass(time_t start);

void display_time(long nhour, long nmin, long nsecond);

void control_watch(void);

void number_check(int k, int i, int count);

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

 

//디지털 숫자를 출력하기 위해 필요한 배열 (4*5)

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 colon[20] = { 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0 };

 

int main(void)

{

        display_time(0, 0, 0);

        getch();

        control_watch();

        return 0;

}

 

/*

스탑워치 명령문 control_watch

*/

void control_watch(void)

{

        char key;

        time_t start;

        start = time(NULL);

        time_pass(start);

        do

        {

               key = getch();

               switch (key)

               {

               case 10:

                       time_pass(start);

               case 32:

                       start = time(NULL);

                       time_pass(start);

                       break;

               case 27:

                       exit(0);

                       break;

               }

        } while (key != 27);

}

 

/*

시작 시간과 현재 시간 사이의 차를 시분초로 계산하는 함수 time_pass

*/

void time_pass(time_t start)

{

        double hour, min, second;

        long nhour, nmin, nsecond;

        time_t now;

        while (!kbhit())

        {

               now = time(NULL);

               second = difftime(now, start);

               hour = (second / 3600.);

               nhour = (long)hour;

               min = fmod(second, 3600.) / 60;

               nmin = (long)min;

               second = fmod(second, 60);

               nsecond = (long)second;

               display_time(nhour, nmin, nsecond);

        }

}

 

/*

화면에 시간을 출력하는 함수 display_time

*/

void display_time(long nhour, long nmin, long nsecond)

{

        system("cls");

        printf("stopwatch\n\n");

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

        {

               number_check(nhour, line, 0);

               digit_print(colon, line);

               number_check(nmin, line, 0);

               digit_print(colon, line);

               number_check(nsecond, line, 0);

               printf("\n");

        }

        printf("Enter:(시작/정지), SPACE:재시작, Esc:종료\n");

}

 

//디지털 숫자 출력하는 함수

void digit_print(int dim[], int line)

{

        int i;

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

        {

               if (dim[i] == 1)

                       printf("");

               else

                       printf("  ");

        }

        printf("    ");

}

 

//어떤 숫자를 출력할지 정하는 함수

void number_check(int k, int i, int count) //count는 초가 9초 이내일 때와 그 이상일 때를 구분짓기 위해

{

        if (k >= 1 && k < 10 && !count)

        {

               switch (k % 10)

               {

               case 0:

                       digit_print(zero, i);

                       break;

               case 1:

                       digit_print(zero, i);

                       digit_print(one, i);

                       break;

               case 2:

                       digit_print(zero, i);

                       digit_print(two, i);

                       break;

               case 3:

                       digit_print(zero, i);

                       digit_print(three, i);

                       break;

               case 4:

                       digit_print(zero, i);

                       digit_print(four, i);

                       break;

               case 5:

                       digit_print(zero, i);

                       digit_print(five, i);

                       break;

               case 6:

                       digit_print(zero, i);

                       digit_print(six, i);

                       break;

               case 7:

                       digit_print(zero, i);

                       digit_print(seven, i);

                       break;

               case 8:

                       digit_print(zero, i);

                       digit_print(eight, i);

                       break;

               case 9:

                       digit_print(zero, i);

                       digit_print(nine, i);

                       break;

               }

        }

        else if (k >= 1 && k < 10 && count)

        {

               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 >= 10)

        {

               number_check(k / 10, i, 1);

               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 //0일 때 00 출력

        {

               digit_print(zero, i);

               digit_print(zero, i);

        }

}



개발환경:Visual Studio 2017


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


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


*system("cls") 문이 계속 실행되어 깜빡임 현상이 나타나는데 수정하는 방법을 생각해보도록 하겠습니다


반응형