在C中编写程序,提示用户输入两个日期,然后指示日历上的日期

时间:2014-12-16 01:49:06

标签: c date

我的代码如下:

#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;

}

我试图保持简单,因为我是初学者,而且只是在King's C Programming Modern Approach Textbook的第5章。当我输入以下内容时:

输入第一个日期(mm / dd / yy):03/31/93 输入第二个日期(mm / dd / yy):04/31/93

我得到的回应是:

4/31/93早于3/31/93 AND 93年3月31日早于4/31/93

非常感谢任何帮助。感谢。

2 个答案:

答案 0 :(得分:4)

一旦您发现第一个日期的年份小于第二个年份的年份,您应打印答案并返回以防止后续检查和结果输出,即:

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

同样适用于月份和日期字段。

答案 1 :(得分:2)

在您的第一个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;
}

Ideone demo.