Write a C program that prompts the user to enter two dates, and then indicate which date precedes the calendar

My code is as follows:

#include <stdio.h>
int main(void)
{
    int m1, m2, d1, d2, y1, y2;

    printf("enter first date (mm/dd/yy): ");
    scanf("%d/%d/%d", &m1, &d1, &y1);

    printf("enter second date (mm/dd/yy): ");
    scanf("%d/%d/%d", &m2, &d2, &y2);

    if (y1<y2)
        printf("%d/%d/%.2d is earlier than %d/%d/%.2d\n",m1, d1, y1, m2, d2, y2);
    else
        printf("%d/%d/%.2d is earlier than %d/%d/%.2d\n",m2, d2, y2, m1, d1, y1);

    if (y1==y2)
    {if (m1<m2)
            printf("%d/%d/%.2d is earlier than %d/%d/%.2d\n",m1, d1, y1, m2, d2, y2);
    else if (m2<m1)
        printf("%d/%d/%.2d is earlier than %d/%d/%.2d\n",m2, d2, y2, m1, d1, y1);}

    if (y1==y2 && m1==m2)
    {if (d1<d2)
        printf("%d/%d/%.2d is earlier than %d/%d/%.2d\n",m1, d1, y1, m2, d2, y2);
    else
        printf("%d/%d/%.2d is earlier than %d/%d/%.2d\n",m2, d2, y2, m1, d1, y1);}

    return 0;

}

I try to make it simple, as I am new and I am only in the fifth chapter in King C's modern English programming textbook. When I introduce the following:

enter the first date (mm / dd / yy): 03/31/93 enter the second date (mm / dd / yy): 04/31/93

The answer I get is:

4/31/93 - earlier 3/31/93 and 3/31/93 earlier than 4/31/93

Any help is greatly appreciated. Thank.

+4
source share
2 answers

, , , , ..:

if (y1<y2) {
    printf("%d/%d/%.2d is earlier than %d/%d/%.2d\n",m1, d1, y1, m2, d2, y2);
    return 0;
}

.

+4

if else , y1 >, y2. , (, ) y1 > y2 , else () .

else else if ( ):

if (y1 < y2) {
    printf("%d/%d/%.2d is earlier than %d/%d/%.2d\n",m1, d1, y1, m2, d2, y2);
}
else if (y2 < y1) {
    printf("%d/%d/%.2d is earlier than %d/%d/%.2d\n",m2, d2, y2, m1, d1, y1);
}

, , , , , : if (d1 < d2) false, else. , else if:

if (y1 == y2 && m1 == m2) {
    if (d1 < d2) {
        printf("%d/%d/%.2d is earlier than %d/%d/%.2d\n",m1, d1, y1, m2, d2, y2);
    }
    else {
        if (d2 < d1) {
            printf("%d/%d/%.2d is earlier than %d/%d/%.2d\n",m2, d2, y2, m1, d1, y1);
        }
        else {
            printf("Dates are equal\n");
        }
    }
}

, , -, printf, .

, ( ):

#include <stdio.h>

#define FIRST_DATE_IS_BIGGER (1)
#define SECOND_DATE_IS_BIGGER (-1)
#define DATES_ARE_EQUAL 0

int main(void)
{
    int m1, m2, d1, d2, y1, y2;
    int result = DATES_ARE_EQUAL;

    printf("Enter the first date (mm/dd/yy): ");
    scanf("%d/%d/%d", &m1, &d1, &y1);

    printf("Enter the second date (mm/dd/yy): ");
    scanf("%d/%d/%d", &m2, &d2, &y2);

    if (y1 < y2) {
        result = SECOND_DATE_IS_BIGGER;
    }
    else if (y2 < y1) {
        result = FIRST_DATE_IS_BIGGER;
    }
    else {
        if (m1 < m2) {
            result = SECOND_DATE_IS_BIGGER;
        }
        else if (m2 < m1) {
            result = FIRST_DATE_IS_BIGGER;
        }
        else {
            if (d1 < d2) {
                result = SECOND_DATE_IS_BIGGER;
            }
            else if (d2 < d1) {
                result = FIRST_DATE_IS_BIGGER;
            }
        }
    }

    switch (result) {
        case FIRST_DATE_IS_BIGGER:
            printf("%d/%d/%.2d is earlier than %d/%d/%.2d\n",m2, d2, y2, m1, d1, y1);
            break;
        case SECOND_DATE_IS_BIGGER:
            printf("%d/%d/%.2d is earlier than %d/%d/%.2d\n",m1, d1, y1, m2, d2, y2);
            break;
        case DATES_ARE_EQUAL:
            printf("Dates are equal.\n");
            break;
    }
    return 0;
}

.

+2

All Articles