Advertisement
Le_BuG63

Untitled

Feb 15th, 2015
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.75 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 decade;
  15.     int unit;
  16. };
  17.  
  18. enum PLACE {
  19.     unitS,
  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.     "TWENTYTHIRTYFORTHYFIFTHYZEROONETWOTHREEFORFIVEFIFSIXSEVENEIGHTNINETENELEVENTWELVETEEN SECONDS",
  30.     "TWENTYTHIRTYFORTHYFIFTHYZEROONETWOTHREEFORFIVEFIFSIXSEVENEIGHTNINETENELEVENTWELVETEEN 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.     "FIF",
  54.     NULL
  55. };
  56.  
  57. void    wait(int time) {
  58.     clock_t end = clock() + (time * CLOCKS_PER_SEC);
  59.     while(clock()< end);
  60. }
  61.  
  62. POSITION get_wordPos(const char *word, PLACE where, bool decade)
  63. {
  64.     POSITION position = {.start = 0, .end = 0};
  65.    
  66.     size_t  lenght = strlen(digits[where]);
  67.     size_t  lenght_word = strlen(word);
  68.     size_t  lenght_word_found = 0;
  69.    
  70.     unsigned int x = 0;
  71.    
  72.     if(where != HOURS && !decade)
  73.         x = 27;
  74.    
  75.     for(; x < lenght; ++x)
  76.     {
  77.         if(digits[where][x] == word[0])
  78.         {
  79.             int k =  0;
  80.            
  81.             position.start = x;
  82.  
  83.             while((lenght_word_found < lenght_word) && (digits[where][x + k] == word[lenght_word_found]))
  84.             {              
  85.                 ++k;
  86.                 ++lenght_word_found;
  87.             }
  88.            
  89.             if(lenght_word_found == lenght_word)
  90.             {
  91.                 position.end = k + position.start;
  92.                
  93.                 return position;
  94.             }
  95.            
  96.             lenght_word_found = 0;
  97.         }
  98.     }
  99.    
  100.     position.start = 0;
  101.     position.end = 0;
  102.    
  103.     return position;
  104. }
  105.  
  106. int get_digit(int number, int x)
  107. {
  108.     return (number/((int)pow(10, x-1)) % 10);
  109. }
  110.  
  111. DIGITS get_wordNum(int time)
  112. {
  113.     DIGITS  digit = {.decade = 0, .unit = -1};
  114.  
  115.     if(time > 12 && time < 20)
  116.         /*if(time == 15)
  117.             digit.decade = 18;
  118.         else*/
  119.         digit.decade = 17;
  120.     else if(time >= 20 && time < 30)
  121.         digit.decade = 13;
  122.     else if(time >= 30 && time < 40)
  123.         digit.decade = 14;
  124.     else if(time >= 40 && time < 50)
  125.         digit.decade = 15;
  126.     else if(time >= 50 && time < 60)
  127.         digit.decade = 16;
  128.     else
  129.         digit.decade = time;
  130.  
  131.     if(time > 12)
  132.         digit.unit = get_digit(time, 1);
  133.    
  134.     return digit;
  135. }
  136.  
  137. void    print_time(int time, PLACE where)
  138. {
  139.     size_t      lenght = strlen(digits[where]);
  140.    
  141.     POSITION    wordPos1 = {.start = 0, .end = 0},
  142.                 wordPos2 = {.start = 0, .end = 0};
  143.     POSITION    name;
  144.    
  145.     DIGITS      wordNum = get_wordNum(time);
  146.    
  147.     switch(where)
  148.     {
  149.         case unitS:
  150.             name = get_wordPos("SECONDS", where, 0);
  151.             break;
  152.         case MINUTES:
  153.             name = get_wordPos("MINUTES", where, 0);
  154.             break;
  155.         case HOURS:
  156.             name = get_wordPos("HOURS", where, 0);
  157.             break;
  158.         default:
  159.             ;
  160.     }
  161.    
  162.     wordPos1 = get_wordPos(numToWord[wordNum.decade], where, 1);
  163.    
  164.     if(wordNum.unit != -1)
  165.         wordPos2 = get_wordPos(numToWord[wordNum.unit], where, 0);
  166.    
  167.     for(int x = 0; x < (int)lenght; x++)
  168.     {
  169.         if((x >= wordPos1.start && x < wordPos1.end)
  170.         || (x >= wordPos2.start && x < wordPos2.end)
  171.         || (x >= name.start && x < name.end))
  172.             putchar(digits[where][x]);
  173.         else
  174.             putchar('-');
  175.     }
  176. }
  177.  
  178. void    print_clock(struct tm clock)
  179. {
  180.     print_time(clock.tm_sec, unitS); putchar('\n');
  181.     print_time(clock.tm_min, MINUTES); putchar('\n');
  182.     print_time(clock.tm_hour, HOURS); putchar('\n');
  183.    
  184.     printf("\n");
  185. }
  186.  
  187. int main(void)
  188. {
  189.     time_t      seconds;
  190.     struct tm   nowTime;
  191.    
  192.     while(1) {
  193.         time(&seconds);
  194.         nowTime = *localtime(&seconds);
  195.        
  196.         if(nowTime.tm_hour > 12)
  197.             nowTime.tm_hour -= 12;
  198.        
  199.         print_clock(nowTime);
  200.        
  201.         wait(1);
  202.     }
  203.     return 0;
  204. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement