天到年,月,天的转换未在c中显示正确的结果

问题描述

#include<stdio.h>//Its not showing the correct result//

int main()
{
    int y,m,d;
    scanf("%d",&d);
    y=0;
    m=0;
    y=d/365;
    d=d%365;
    m=d/30;
    d=d%30;
    printf("%d ano(s)\n",&y);
    printf("%d mes(es)\n",&m);
    printf("%d dia(s)\n",&d);

    return 0;
}

解决方法

转换未显示正确的结果

m=d/30;是有意义的,如果所有月份都为30天。 除非使用其他日历,例如this,否则他们不会这样做。

代码需要考虑每月和leap年的不同天数。


节省时间。启用所有编译器警告。打印值,而不是地址。

// printf("%d ano(s)\n",&y);
// printf("%d mes(es)\n",&m);
// printf("%d dia(s)\n",&d);
printf("%d ano(s)\n",y);
printf("%d mes(es)\n",m);
printf("%d dia(s)\n",d);