【问题标题】:How to convert day into year month week then days如何将天转换为年月周然后天
【发布时间】:2019-02-05 02:45:45
【问题描述】:

在这里我计算了只有 30 天的月份,但我想计算月份 1 月 31 日、2 月 28 日或 29、4 月 30 日这样

我如何在逻辑上做到这一点。请帮我解决这个问题

#include<stdio.h>
void main()
{
int in_days, years, months, days, extra, week;
printf("Enter number of days : ");
scanf("%d", &in_days);
If(in_day>1460){
   years = in_days/365;
   extra = in_days%365;
   months = extra/30; 
   days = extra%30;
   printf("%d days is same as %d years + %d months + %d days", in_days, years, months, days);
} 
else If(in_day<1460 & & in_day>=365){
       years = in_days/365;
       extra = in_days%365;
       months = extra/30;
       days = extra%30;
       printf("%d days is same as %d years + %d months + %d days", in_days, years, months, days);
  } 
else If(in_day<365){
       months = in_day/30;
       days = in_day%30;
       printf("%d days is same as %d months + %d days", in_days, months, days);
  } 
else If(in_day<30){
       week= in_day/7;
       days = week%7;
       printf("%d days is same as %d months + %d days", in_days, week, days);
  } 
getch() ;
}

【问题讨论】:

  • 不要试图自己计算,找一个为你计算的库。由于所有奇怪的极端情况(如闰年和闰秒,并非所有月份都相同等),日期和时间比看起来要困难得多。
  • C 区分大小写。在代码中看到所有If 意味着它不会编译。
  • 定义一个数组 int month_lengths[] 包含值 31, 28, 31, 30, ... 。然后你可以编写一个简单的循环来计算,例如,第 74 天是 74 - 31 - 28 = 3 月的第 15 天。然后你可以对闰年做一个简单的修改。
  • 一个大问题是您无法真正检查闰年,因为您所拥有的只是没有任何开始或结束的“天数”。 “天数”必须与其他日期相关,否则您不知道哪些年份是闰年。你只能说每四年是闰年。

标签: c error-handling runtime-error programming-languages


【解决方案1】:

您可以添加函数来计算#months

#include<stdio.h>

int cal_months(int *days){
    int months_arr[] = {31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365}, i, months = 1;
    if(*days < 31){
        return 0;
    }
    for(i = 1; i < 12; i++){
        if(*days < months_arr[i]){
            *days -= months_arr[i-1];
            break;
        }else{
            months ++;
        }
    }
    return months;
}

int main()
{
    int in_days, years, months, days, extra, week;
    printf("Enter number of days : ");
    scanf("%d", &in_days);
    if(in_days>1460){
        years = in_days/365;
        extra = in_days%365;
        months = cal_months(&extra);
        //days = extra%30;
        printf("%d days is same as %d years + %d months + %d days\n", in_days, years, months, extra);
    }else if(in_days<1460 && in_days>=365){
        years = in_days/365;
        extra = in_days%365;
        months = cal_months(&extra);
        //days = extra%30;
        printf("%d days is same as %d years + %d months + %d days\n", in_days, years, months, extra);
    }else if(in_days<365){
        days = in_days;
        months = cal_months(&days);
        printf("%d days is same as %d months + %d days\n", in_days, months, days);
    }else if(in_days<31){
        week= in_days/7;
        days = in_days%7;
        printf("%d days is same as %d weeks+ %d days\n", in_days, week, days);
    }
    return 0;
}

注意:此代码不考虑 LEAP 年。您应该添加自己的逻辑来包含此功能。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多