Advertisement
sailorbob74133

Maman 12 Q5 in C

Nov 20th, 2012
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.54 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. // reads till the first non-digit char
  4. int strToInt(char *a0) {
  5.     // find number of digits
  6.     unsigned int t1=0, t2=1, t3=0, t4=0, t5=0;
  7.     t3 = *(a0+t1);
  8.     while ( t3 <= '9' && t3 >= '0' ) {
  9.         t1++;
  10.         t3 = *(a0+t1);
  11.     }
  12.     t1--; // adjust to be an index
  13.     while ( t1 != -1 ) {
  14.         t3 = *(a0+t1);
  15.         t3 -= '0';
  16.         t4 = t3 * t2;
  17.         t5 += t4;
  18.         t1--;
  19.         t2 *= 10;
  20.     }
  21.     return t5;
  22. }
  23.  
  24. int main(void) {
  25.     char *date = "23.11.2012";
  26.     char buf[14];
  27.     int stop = 0x706f7473; // bytes are reversed
  28.     int s1, s2, s3, s4, s5, s6, t1, t2;
  29.  
  30.     printf("%s\n", date);
  31.  
  32.     s1 = strToInt(date);
  33.     s2 = strToInt(date+3);
  34.     s3 = strToInt(date+6);
  35.     // assume each month has 31 days and each year 356
  36.     t1 = s1;
  37.     t1 += ( s2 * 31 ) + ( s3 * 356 );
  38.     while ( fgets(buf, 12, stdin) != NULL ) {
  39.         // check for stop
  40.         if ( *(int*)buf == stop )
  41.             break;
  42.         // check string correctness
  43.         s4 = strToInt(buf);
  44.         s5 = strToInt(buf+3);
  45.         s6 = strToInt(buf+6);
  46.  
  47.         // check that the dates are valid
  48.         if ( s4 <= 1 || s4 >= 31 || s5 <= 1 || s5 >= 12 || s6 >= 5000 )
  49.             goto error;
  50.         // assume each month has 31 days and each year 356
  51.         t2 = s4;
  52.         t2 += ( s5 * 31 ) + ( s6 * 356 );
  53.         if ( t2 <= t1 )
  54.             printf("o.k.\n");
  55.         else
  56.             printf("too late!\n");
  57.     }
  58.  
  59.     return 0;
  60.     error:
  61.         printf("error\n");
  62.     return 1;
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement