samedi 28 septembre 2019

Why does my program think that two completely different if statements are the same?

I am trying to create a program that calculates the difference between two dates (this includes dates with different years). As such, I have to account for leap years. If both dates are leap years or if the first date is a leap year, the program works fine. However, the two if statements below don't seem to work. The first if statement is meant to figure out the difference if the starting date is a non-leap year and the end date is a leap year. The second if statement is meant to determine the difference if both dates are non-leap years. Despite the clear difference, my program seems to think that these statements are logically equivalent.

To solve this problem, I have tried many things. First, in my code, I have placed printf statements to tell me which option is running for each input. For instance, the first statement prints out "the commanding officer" and the second prints out "regular year 2" if it is the correct one. Also, I have attempted to change the bracket notation in the affected if statements. Here are my lines of code:

else if ((yyyy1 % 4 != 0) || ( yyyy1 % 100 == 0 ) && ( yyyy1 % 400 != 0 ) && (yyyy2 % 4 == 0) && ( yyyy2 % 100 != 0 ) && ( yyyy2 % 400 == 0 ) && (argc == 3)) 
startingDate = dd1 + startdayofYear[mm1-1];
endDate = dd2 + leapenddayofYear[mm2-1];
dateDifference = endDate - startingDate;
yearDifference = yyyy2 - yyyy1;
leapBetween = (yyyy2-yyyy1) / 4;
difference = 365 * yearDifference + dateDifference + leapBetween;
printf("%d", difference);
printf("the commanding officer");
}`


`else if ((yyyy1 % 4 != 0) || ( yyyy1 % 100 == 0 ) && ( yyyy1 % 400 != 0 ) && (yyyy2 % 4 != 0) && ( yyyy2 % 100 == 0 ) && ( yyyy2 % 400 == 0 ) && (argc == 3)) {
startingDate = dd1 + startdayofYear[mm1-1];
endDate = dd2 + enddayofYear[mm2-1];
dateDifference = endDate - startingDate;
yearDifference = yyyy2 - yyyy1;
difference = 365 * yearDifference + dateDifference;
printf("%d", difference);
printf("regular year 2");
}`

Aucun commentaire:

Enregistrer un commentaire