Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- #include <string.h>
- #include <math.h>
- #include <stdbool.h>
- struct s_position {
- int start;
- int end;
- };
- struct s_digits {
- int decade;
- int unit;
- };
- enum PLACE {
- unitS,
- MINUTES,
- HOURS
- };
- typedef struct s_position POSITION;
- typedef struct s_digits DIGITS;
- typedef enum PLACE PLACE;
- const char *digits[3] = {
- "TWENTYTHIRTYFORTHYFIFTHYZEROONETWOTHREEFORFIVEFIFSIXSEVENEIGHTNINETENELEVENTWELVETEEN SECONDS",
- "TWENTYTHIRTYFORTHYFIFTHYZEROONETWOTHREEFORFIVEFIFSIXSEVENEIGHTNINETENELEVENTWELVETEEN MINUTES",
- "ZEROONETWOTHREEFORFIVESIXSEVENEIGHTNINETENELEVENTWELVE HOURS"
- };
- const char *numToWord[20] = {
- "ZERO",
- "ONE",
- "TWO",
- "THREE",
- "FOR",
- "FIVE",
- "SIX",
- "SEVEN",
- "EIGHT",
- "NINE",
- "TEN",
- "ELEVEN",
- "TWELVE",
- "TWENTY",
- "THIRTY",
- "FORTY",
- "FIFTY",
- "-TEEN",
- "FIF",
- NULL
- };
- void wait(int time) {
- clock_t end = clock() + (time * CLOCKS_PER_SEC);
- while(clock()< end);
- }
- POSITION get_wordPos(const char *word, PLACE where, bool decade)
- {
- POSITION position = {.start = 0, .end = 0};
- size_t lenght = strlen(digits[where]);
- size_t lenght_word = strlen(word);
- size_t lenght_word_found = 0;
- unsigned int x = 0;
- if(where != HOURS && !decade)
- x = 27;
- for(; x < lenght; ++x)
- {
- if(digits[where][x] == word[0])
- {
- int k = 0;
- position.start = x;
- while((lenght_word_found < lenght_word) && (digits[where][x + k] == word[lenght_word_found]))
- {
- ++k;
- ++lenght_word_found;
- }
- if(lenght_word_found == lenght_word)
- {
- position.end = k + position.start;
- return position;
- }
- lenght_word_found = 0;
- }
- }
- position.start = 0;
- position.end = 0;
- return position;
- }
- int get_digit(int number, int x)
- {
- return (number/((int)pow(10, x-1)) % 10);
- }
- DIGITS get_wordNum(int time)
- {
- DIGITS digit = {.decade = 0, .unit = -1};
- if(time > 12 && time < 20)
- /*if(time == 15)
- digit.decade = 18;
- else*/
- digit.decade = 17;
- else if(time >= 20 && time < 30)
- digit.decade = 13;
- else if(time >= 30 && time < 40)
- digit.decade = 14;
- else if(time >= 40 && time < 50)
- digit.decade = 15;
- else if(time >= 50 && time < 60)
- digit.decade = 16;
- else
- digit.decade = time;
- if(time > 12)
- digit.unit = get_digit(time, 1);
- return digit;
- }
- void print_time(int time, PLACE where)
- {
- size_t lenght = strlen(digits[where]);
- POSITION wordPos1 = {.start = 0, .end = 0},
- wordPos2 = {.start = 0, .end = 0};
- POSITION name;
- DIGITS wordNum = get_wordNum(time);
- switch(where)
- {
- case unitS:
- name = get_wordPos("SECONDS", where, 0);
- break;
- case MINUTES:
- name = get_wordPos("MINUTES", where, 0);
- break;
- case HOURS:
- name = get_wordPos("HOURS", where, 0);
- break;
- default:
- ;
- }
- wordPos1 = get_wordPos(numToWord[wordNum.decade], where, 1);
- if(wordNum.unit != -1)
- wordPos2 = get_wordPos(numToWord[wordNum.unit], where, 0);
- for(int x = 0; x < (int)lenght; x++)
- {
- if((x >= wordPos1.start && x < wordPos1.end)
- || (x >= wordPos2.start && x < wordPos2.end)
- || (x >= name.start && x < name.end))
- putchar(digits[where][x]);
- else
- putchar('-');
- }
- }
- void print_clock(struct tm clock)
- {
- print_time(clock.tm_sec, unitS); putchar('\n');
- print_time(clock.tm_min, MINUTES); putchar('\n');
- print_time(clock.tm_hour, HOURS); putchar('\n');
- printf("\n");
- }
- int main(void)
- {
- time_t seconds;
- struct tm nowTime;
- while(1) {
- time(&seconds);
- nowTime = *localtime(&seconds);
- if(nowTime.tm_hour > 12)
- nowTime.tm_hour -= 12;
- print_clock(nowTime);
- wait(1);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement