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 first;
- int second;
- };
- enum PLACE {
- SECONDS,
- MINUTES,
- HOURS
- };
- typedef struct s_position POSITION;
- typedef struct s_digits DIGITS;
- typedef enum PLACE PLACE;
- const char *digits[3] = {
- "TWENTYTHIRTYFORTHYFIFTHYZEROONETWOTHREEFORFIVESIXSEVENEIGHTNINETENELEVENTWELVE-TEEN SECONDS",
- "TWENTYTHIRTYFORTHYFIFTHYZEROONETWOTHREEFORFIVESIXSEVENEIGHTNINETENELEVENTWELVE-TEEN MINUTES",
- "ZEROONETWOTHREEFORFIVESIXSEVENEIGHTNINETENELEVENTWELVE HOURS"
- };
- const char *numToWord[19] = {
- "ZERO",
- "ONE",
- "TWO",
- "THREE",
- "FOR",
- "FIVE",
- "SIX",
- "SEVEN",
- "EIGHT",
- "NINE",
- "TEN",
- "ELEVEN",
- "TWELVE"
- "TWENTY",
- "THIRTY",
- "FORTHY",
- "FIFTHY",
- "-TEEN",
- 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;
- int x = 0;
- if(where != HOURS || !decade)
- x = 29;
- 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;
- if(time > 12 && time < 20)
- digit.first = 17;
- else if(time >= 20 && time < 30)
- digit.first = 13;
- else if(time >= 30 && time < 40)
- digit.first = 14;
- else if(time >= 40 && time < 50)
- digit.first = 15;
- else if(time >= 50 && time < 60)
- digit.first = 16;
- else
- digit.first = time;
- if(time != 11 || time != 12)
- digit.second = get_digit(time, 1);
- return digit;
- }
- void print_time(int time, PLACE where)
- {
- size_t lenght = strlen(digits[where]);
- POSITION wordPos1,
- wordPos2;
- POSITION name;
- DIGITS wordNum = get_wordNum(time);
- switch(where)
- {
- case SECONDS:
- 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.first], where, 1);
- wordPos2 = get_wordPos(numToWord[wordNum.second], where, 0);
- for(int x = 0; x < lenght; x++)
- {
- if(x >= wordPos1.start && x < wordPos1.end)
- putchar(digits[where][x]);
- else if(x >= wordPos2.start && x < wordPos2.end)
- putchar(digits[where][x]);
- else if(x >= name.start && x < name.end)
- putchar(digits[where][x]);
- else
- putchar('-');
- }
- }
- void print_clock(struct tm clock)
- {
- print_time(clock.tm_sec, SECONDS); putchar('\n');
- print_time(clock.tm_min, MINUTES); putchar('\n');
- print_time(clock.tm_hour, HOURS); putchar('\n');
- }
- int main(void)
- {
- POSITION pos;
- time_t seconds;
- struct tm instant;
- while(1) {
- time(&seconds);
- instant = *localtime(&seconds);
- if(instant.tm_hour > 12)
- instant.tm_hour -= 12;
- print_clock(instant);
- wait(1);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement