I've got myself in a terrible situation her. Can anyone refactor this C code for me? for (i = 0; i < year1; ++i) { !(i % 4) ? (!(i % 100) ? (!(i % 400) ? (days1 += 366) : (days1 += 365)) : (days1 += 366)) : (days1 += 365);
it's supposed to compute leap years
it should go like if i is divisible by 4 then if i is divisible by 100 then if i is divisible by 400 then days1 += 366 else days1 += 365 else days1 += 366 else days1 += 365
better use i % 4 == 0 instead of !(i % 4) so I won't get confused :-P
why won't you just code it that way?
I got too crazy with the ternary operator :-P
otherwise it will take 10 lines of if-else stuff
for (i = 0; i < year1; ++i) { if (i%400 = 0) { days1+= 366 } else if (i%100 = 0) { days1 += 365 } else if (i% ...... ......
I always hated reading code that was trying to save lines of text and cram too much into one fancy line. Compilers will take care of optimizing all that stuff. Just write simple readable code.
this is the whole routine: void days_between_dates(void) { int month1, day1, year1, month2, day2, year2; int days1, days2; int i; days1 = 0; days2 = 0; printf("Enter a date (dd/mm/yyyy): "); scanf("%d/%d/%d", &day1, &month1, &year1); printf("Enter another date (dd/mm/yyyy): "); scanf("%d/%d/%d", &day2, &month2, &year2); for (i = 0; i < year1; ++i) { if (i % 4 == 0) { if (i % 100 == 0) { if (i % 400 == 0) { days1 += 366; } else { days1 += 365; } } else { days1 += 366; } } else { days1 += 365; } } for (i = 0; i < year2; ++i) { if (i % 4 == 0) { if (i % 100 == 0) { if (i % 400 == 0) { days2 += 366; } else { days2 += 365; } } else { days2 += 366; } } else { days2 += 365; } } for (i = 1; i <= month1; ++i) { switch(i) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: days1 += 31; break; case 2: !(i % 4) ? (days1 += 29) : (days1 += 30); default: days1 += 30; break; } } for (i = 1; i <= month2; ++i) { switch(i) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: days2 += 31; break; case 2: !(i % 4) ? (days2 += 29) : (days2 += 30); default: days2 += 30; break; } } days1 += day1; days2 += day2; printf("\nThere is/are %d day(s) between those dates.\n", abs(days2 - days1)); }
how do I make it smaller?
Put this code in a function: for (i = 0; i < year1; ++i) { if (i % 4 == 0) { if (i % 100 == 0) { if (i % 400 == 0) { days1 += 366; } else { days1 += 365; } } else { days1 += 366; } } else { days1 += 365; } }
I wish there was some easy way to do this whole thing, like call some C standard library time function that returns dates, find their difference, and then return that difference etc.
Your whole switch statement seems like an overkill. You just have one exception case (where it is 2nd month). Just write: if (i=2) { do this stuff; } else { do this other stuff }
I've got two different cases: a month has 30 days or 31 days, and one exception case for february
I'm just glad the whole thing works :-P
Apparently, difftime() routine that is part of the ctime library returns number of seconds between two dates. Take a look.
Sorry, I now see your switch statement logic. Again, place that into a function instead of repeating same code twice.
If only C had lambda expressions :(
Join our real-time social learning platform and learn together with your friends!