Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- // reads till the first non-digit char
- int strToInt(char *a0) {
- // find number of digits
- unsigned int t1=0, t2=1, t3=0, t4=0, t5=0;
- t3 = *(a0+t1);
- while ( t3 <= '9' && t3 >= '0' ) {
- t1++;
- t3 = *(a0+t1);
- }
- t1--; // adjust to be an index
- while ( t1 != -1 ) {
- t3 = *(a0+t1);
- t3 -= '0';
- t4 = t3 * t2;
- t5 += t4;
- t1--;
- t2 *= 10;
- }
- return t5;
- }
- int main(void) {
- char *date = "23.11.2012";
- char buf[14];
- int stop = 0x706f7473; // bytes are reversed
- int s1, s2, s3, s4, s5, s6, t1, t2;
- printf("%s\n", date);
- s1 = strToInt(date);
- s2 = strToInt(date+3);
- s3 = strToInt(date+6);
- // assume each month has 31 days and each year 356
- t1 = s1;
- t1 += ( s2 * 31 ) + ( s3 * 356 );
- while ( fgets(buf, 12, stdin) != NULL ) {
- // check for stop
- if ( *(int*)buf == stop )
- break;
- // check string correctness
- s4 = strToInt(buf);
- s5 = strToInt(buf+3);
- s6 = strToInt(buf+6);
- // check that the dates are valid
- if ( s4 <= 1 || s4 >= 31 || s5 <= 1 || s5 >= 12 || s6 >= 5000 )
- goto error;
- // assume each month has 31 days and each year 356
- t2 = s4;
- t2 += ( s5 * 31 ) + ( s6 * 356 );
- if ( t2 <= t1 )
- printf("o.k.\n");
- else
- printf("too late!\n");
- }
- return 0;
- error:
- printf("error\n");
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement