示例#1
0
int whenDate(Date d1, Date d2)
{
    if (validMonth(d1) == -1 || validDay(d1) == -1 || validYear(d1) == -1)
        return -2; //returns -2 if one of the dates are invalid

    if (validMonth(d2) == -1 || validDay(d2) == -1 || validYear(d2) == -1)
        return -2;

    if (d1.year > d2.year)
        return 1;
    else if (d1.year < d2.year)
        return -1;
    else if (d1.year == d2.year)
    {
        if (d1.month > d2.month)
            return 1;
        else if (d1.month < d2.month)
            return -1;
        else if (d1.month == d2.month)
        {
            if (d1.day > d2.day)
                return 1;
            else if (d1.day < d2.day)
                return -1;
            else if (d1.day == d2.day)
                return 0;
        }
    }
}
示例#2
0
int validTimeDate(int day, char* month, int year) {
	int m;
	
	if((m = validMonth(month)) != 0) {
		if(day >= 1 && day <= daysMonth(m, year))
			return 1;
	}

	return 0;
}
示例#3
0
int main (int argc, char *argv[])
{
	int date;
	char input[10];
	char mon[10];
	int monidx;
	int year;
	int days;
	int isleap;
	int total;

	// obtain a year
	printf("Enter the year(empty line to quit): ");

	while(fgets(input, 10, stdin) != NULL && input[0] != '\n') {

		if (!isdigit(input[0])) {
			printf("%s is not a valid year. Enter the year again(empty line to quit): ", input);
			continue;
		}
		year = atoi(input);

		// check if that year is a leap year
		isleap = leapYear(year);
		

		// obtain a valid month
		printf ("Enter the month (number, name or abbreviation): ");
		
		int f;
		while(fgets(mon, 10, stdin) == NULL || input[0] == '\n' || !(validMonth(monidx = standard(mon)))) {
			printf("Not a valid input, enter the month (number, name or abbreviation): ");
			// printf("%d\n",f);
		}
		// printf("%d\n",f);

		// transfer the input to month idx
		// monidx = standard(mon);
		
		// obtain a valid date
		// days = obtainDays(monidx);
		printf("Enter the date (1-%d): ",obtainDays(monidx));
		while(scanf("%d", &date) <= 0) {
			if (isleap && monidx == 2) {
				printf("Not a valid date, please input a valid date(1-29): ");
			}
			printf("Not a valid date, please input a valid date(1-%d): ", obtainDays(monidx));
		}

		// calculate the days
		total = calTotal(monidx, date, isleap);

		// print the results
		if (total) {
			int lastDigit = date % 10;
			if (lastDigit == 1) {
				printf("There are %d day(s) through %s %dst from begining of %d.\n", total, months[monidx].name, date, year);
			}
			else if(lastDigit == 2) {
				printf("There are %d day(s) through %s %dnd from begining of %d.\n", total, months[monidx].name, date, year);
			}
			else if (lastDigit == 3) {
				printf("There are %d day(s) through %s %drd from begining of %d.\n", total, months[monidx].name, date, year);
			}
			else
				printf("There are %d day(s) through %s %dth from begining of %d.\n", total, months[monidx].name, date, year);
		}
		else
			printf("Oops, I ate too much today.\n");

		while(getchar() != '\n')
			continue;
		// Do it again
		printf("Enter the year(empty line to quit): ");
	}
	return 0;
}