2011-11-30, 05:01 AM
Okay, so with the help of Alex and a few others for the past couple of days I managed to produce:
I run into a problem whenever the dates input into the program are not in the same year, though, with the program usually being about 6 or more days over what the actual day count is.
I figure it has something to do with the algorithm of my calc_days function, but I can't seem to find the error.
If you can help in any way I would greatly appreciate it.
For ease of access, here's the calc_days function:
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int check_and_assign_input(char *date, int *month, int *day, int *year)
{
int slashcheck = 0;
char *stepper = date;
while(*stepper)
{
if(*stepper == '/')
{
slashcheck++;
stepper++;
}
else stepper++;
}
if(slashcheck != 2)
return 0;
*month = atoi(strtok(date, "/"));
*day = atoi(strtok(NULL, "/"));
*year = atoi(strtok(NULL, "/"));
return 1;
}
int check_chron(int year1, int year2, int month1, int month2, int day1, int day2)
{
if(year1 < year2)
return 1;
else if(year1 > year2)
return 0;
else if(year1 = year2)
{
if(month1 < month2)
return 1;
else if(month1 > month2)
return 0;
else if(month1 = month2)
{
if(day1 < day2)
return 1;
else if(day1 > day2)
return 0;
else if(day1 = day2)
{
printf("There are zero days between the two dates.");
exit(1);
}
}
}
}
int check_days_in_month(int month_check, int year_check)
{
int days;
switch(month_check)
{
case 1: case 3: case 5: case 7: case 8: case 10: case 12:
days = 31;
break;
case 4: case 6: case 9: case 11:
days = 30;
break;
case 2:
if((year_check % 100 == 0) && (year_check % 400 != 0))
{
days = 28;
break;
}
else if((year_check % 400 == 0) || (year_check % 4 == 0))
{
days = 29;
break;
}
else
{
days = 28;
break;
}
}
return days;
}
int if_leap(int year_check)
{
if((year_check % 100 == 0) && (year_check % 400 != 0))
return 0;
else if((year_check % 4) == 0)
return 1;
else
return 0;
}
int calc_days(int year1, int year2, int month1, int month2, int day1, int day2, int days_in_month1, int days_in_month2)
{
int days_until = 0;
int month_days = 0;
int days_in_year = 0;
if(year1 < year2)
{
while(year1 < year2)
{
days_in_year = if_leap(year1);
if(days_in_year == 1)
days_until += 366;
else days_until += 365;
++year1;
}
if(month1 < month2)
{
while(month1 < month2)
{
month_days = check_days_in_month(month1, year1);
days_until += month_days;
++month1;
}
}
if(month1 = month2)
{
month_days = check_days_in_month(month1, year1);
days_until += (month_days - day2 - 5);
}
}
else if(year1 = year2)
{
while(month1 < month2)
{
month_days = check_days_in_month(month1, year1);
days_until += month_days;
++month1;
}
if(month1 = month2)
{
days_until += (day2 - day1);
}
}
return days_until;
}
int main(void)
{
char first_date[12];
char second_date[12];
int day1, month1, year1, day2, month2, year2;
int days_in_month1, days_in_month2, chronology;
int days_between, slashcheck;
/* Get first date string */
printf("Enter your first date: ");
scanf("%s", first_date);
slashcheck = check_and_assign_input(first_date, &month1, &day1, &year1);
if(slashcheck == 0)
{
printf("Invalid input in first date.");
exit(1);
}
/*Call check_days_in_month function to test input day data against norm*/
days_in_month1 = check_days_in_month(month1, year1);
printf("Enter your second date: ");
scanf("%s", second_date);
slashcheck = check_and_assign_input(second_date, &month2, &day2, &year2);
if(slashcheck == 0)
{
printf("\nInvalid input in second date.");
exit(1);
}
/*Call check_days_in_month function to test input day data against norm*/
days_in_month2 = check_days_in_month(month2, year2);
if((day1 > days_in_month1) || (day2 > days_in_month2))
{
printf("\nInvalid input day of first or second date.");
exit(1);
}
chronology = check_chron(year1, year2, month1, month2, day1, day2);
if(chronology == 0)
{
printf("\nInvalid input in chronology == 0.");
exit(1);
}
days_between = calc_days(year1, year2, month1, month2, day1, day2, days_in_month1, days_in_month2);
printf("\nThere are %d days between your two dates.", days_between);
return 0;
}I run into a problem whenever the dates input into the program are not in the same year, though, with the program usually being about 6 or more days over what the actual day count is.
I figure it has something to do with the algorithm of my calc_days function, but I can't seem to find the error.
If you can help in any way I would greatly appreciate it.
For ease of access, here's the calc_days function:
Code:
int calc_days(int year1, int year2, int month1, int month2, int day1, int day2, int days_in_month1, int days_in_month2){
int days_until = 0;
int month_days = 0;
int days_in_year = 0;
if(year1 < year2)
{
while(year1 < year2)
{
days_in_year = if_leap(year1);
if(days_in_year == 1)
days_until += 366;
else days_until += 365;
++year1;
}
if(month1 < month2)
{
while(month1 < month2)
{
month_days = check_days_in_month(month1, year1);
days_until += month_days;
++month1;
}
}
if(month1 = month2)
{
month_days = check_days_in_month(month1, year1);
days_until += (month_days - day2 - 5);
}
}
else if(year1 = year2)
{
while(month1 < month2)
{
month_days = check_days_in_month(month1, year1);
days_until += month_days;
++month1;
}
if(month1 = month2)
{
days_until += (day2 - day1);
}
}
return days_until;
}
