Advertisement
Le_BuG63

Untitled

Feb 15th, 2015
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.67 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <string.h>
  5. #include <math.h>
  6. #include <stdbool.h>
  7.  
  8. struct s_position {
  9.     int start;
  10.     int end;
  11. };
  12.  
  13. struct s_digits {
  14.     int first;
  15.     int second;
  16. };
  17.  
  18. enum PLACE {
  19.     SECONDS,
  20.     MINUTES,
  21.     HOURS
  22. };
  23.  
  24. typedef struct s_position   POSITION;
  25. typedef struct s_digits     DIGITS;
  26. typedef enum PLACE          PLACE;
  27.  
  28. const char *digits[3] = {
  29.     "TWENTYTHIRTYFORTHYFIFTHYZEROONETWOTHREEFORFIVESIXSEVENEIGHTNINETENELEVENTWELVE-TEEN SECONDS",
  30.     "TWENTYTHIRTYFORTHYFIFTHYZEROONETWOTHREEFORFIVESIXSEVENEIGHTNINETENELEVENTWELVE-TEEN MINUTES",
  31.     "ZEROONETWOTHREEFORFIVESIXSEVENEIGHTNINETENELEVENTWELVE                              HOURS"
  32. };
  33.  
  34. const char *numToWord[20] = {
  35.     "ZERO",
  36.     "ONE",
  37.     "TWO",
  38.     "THREE",
  39.     "FOR",
  40.     "FIVE",
  41.     "SIX",
  42.     "SEVEN",
  43.     "EIGHT",
  44.     "NINE",
  45.     "TEN",
  46.     "ELEVEN",
  47.     "TWELVE",
  48.     "TWENTY",
  49.     "THIRTY",
  50.     "FORTY",
  51.     "FIFTY",
  52.     "-TEEN",
  53.     NULL
  54. };
  55.  
  56. void    wait(int time) {
  57.     clock_t end = clock() + (time * CLOCKS_PER_SEC);
  58.     while(clock()< end);
  59. }
  60.  
  61. POSITION get_wordPos(const char *word, PLACE where, bool decade)
  62. {
  63.     POSITION position = {.start = 0, .end = 0};
  64.    
  65.     size_t  lenght = strlen(digits[where]);
  66.     size_t  lenght_word = strlen(word);
  67.     size_t  lenght_word_found = 0;
  68.    
  69.     int x = 0;
  70.    
  71.     if(where != HOURS && !decade)
  72.         x = 27;
  73.    
  74.     for(; x < lenght; ++x)
  75.     {
  76.         if(digits[where][x] == word[0])
  77.         {
  78.             int k =  0;
  79.            
  80.             position.start = x;
  81.  
  82.             while((lenght_word_found < lenght_word) && (digits[where][x + k] == word[lenght_word_found]))
  83.             {              
  84.                 ++k;
  85.                 ++lenght_word_found;
  86.             }
  87.            
  88.             if(lenght_word_found == lenght_word)
  89.             {
  90.                 position.end = k + position.start;
  91.                
  92.                 return position;
  93.             }
  94.            
  95.             lenght_word_found = 0;
  96.         }
  97.     }
  98.    
  99.     position.start = 0;
  100.     position.end = 0;
  101.    
  102.     return position;
  103. }
  104.  
  105. int get_digit(int number, int x)
  106. {
  107.     return (number/((int)pow(10, x-1)) % 10);
  108. }
  109.  
  110. DIGITS get_wordNum(int time)
  111. {
  112.     DIGITS  digit = {.first = 0, .second = -1};
  113.  
  114.     if(time > 12 && time < 20)
  115.         digit.first = 17;
  116.     else if(time >= 20 && time < 30)
  117.         digit.first = 13;
  118.     else if(time >= 30 && time < 40)
  119.         digit.first = 14;
  120.     else if(time >= 40 && time < 50)
  121.         digit.first = 15;
  122.     else if(time >= 50 && time < 60)
  123.         digit.first = 16;
  124.     else
  125.         digit.first = time;
  126.  
  127.     if(time > 12)
  128.         digit.second = get_digit(time, 1);
  129.    
  130.     return digit;
  131. }
  132.  
  133. void    print_time(int time, PLACE where)
  134. {
  135.     size_t      lenght = strlen(digits[where]);
  136.    
  137.     POSITION    wordPos1 = {.start = 0, .end = 0},
  138.                 wordPos2 = {.start = 0, .end = 0};
  139.     POSITION    name;
  140.    
  141.     DIGITS      wordNum = get_wordNum(time);
  142.    
  143.     switch(where)
  144.     {
  145.         case SECONDS:
  146.             name = get_wordPos("SECONDS", where, 0);
  147.             break;
  148.         case MINUTES:
  149.             name = get_wordPos("MINUTES", where, 0);
  150.             break;
  151.         case HOURS:
  152.             name = get_wordPos("HOURS", where, 0);
  153.             break;
  154.         default:
  155.             ;
  156.     }
  157.    
  158.     wordPos1 = get_wordPos(numToWord[wordNum.first], where, 1);
  159.    
  160.     if(wordNum.second != -1)
  161.         wordPos2 = get_wordPos(numToWord[wordNum.second], where, 0);
  162.    
  163.     for(int x = 0; x < lenght; x++)
  164.     {
  165.         if((x >= wordPos1.start && x < wordPos1.end)
  166.         || (x >= wordPos2.start && x < wordPos2.end)
  167.         || (x >= name.start && x < name.end))
  168.             putchar(digits[where][x]);
  169.         else
  170.             putchar('-');
  171.     }
  172. }
  173.  
  174. void    print_clock(struct tm clock)
  175. {
  176.     print_time(clock.tm_sec, SECONDS); putchar('\n');
  177.     print_time(clock.tm_min, MINUTES); putchar('\n');
  178.     print_time(clock.tm_hour, HOURS); putchar('\n');
  179. }
  180.  
  181. int main(void)
  182. {
  183.     time_t      seconds;
  184.     struct tm   instant;
  185.    
  186.     while(1) {
  187.         time(&seconds);
  188.         instant = *localtime(&seconds);
  189.        
  190.         if(instant.tm_hour > 12)
  191.             instant.tm_hour -= 12;
  192.        
  193.         print_clock(instant);
  194.         puts("");
  195.         wait(1);
  196.     }
  197.     return 0;
  198. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement