首页 > 解决方案 > 在C语言中以日期为dd/mm/yy并以“合法”形式显示日期

问题描述

我的程序工作但没有完成它的任务。例如,当我输入 2020 年 12 月 11 日作为输入时,它会给出以下结果:“日期为 (null) 的第 16 天,200。”

如何解决我的代码的月份和年份部分?

#include <stdio.h>

int main()
{
    int day, mon, year;
    char *months[] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};

    printf("Enter date(DD/MM/YY): ");
    scanf("%d%d%d", &day, &mon, &year);

    if(day%10 == 1 && day != 11)
        printf("Dated this %dst day of %s, 20%d.", day, months[mon], year);

    else if(day%10 == 2 && day != 12)
        printf("Dated this %dnd day of %s, 20%d.", day, months[mon], year);

    else if(day%10 == 3 && day != 13)
        printf("Dated this %drd day of %s, 20%d.", day, months[mon], year);
    else
        printf("Dated this %dth day of %s, 20%d.", day, months[mon], year);

    return 0;
}

当我输入 16/11/20 作为输入时的程序结果

标签: c

解决方案


推荐阅读