gpt4 book ai didi

c - 如何将日转换为年月周然后天

转载 作者:行者123 更新时间:2023-11-30 21:05:55 29 4
gpt4 key购买 nike

这里我计算的月份只有 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() ;
}

最佳答案

您可以添加函数来计算#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 年份。您应该添加自己的逻辑来包含此功能。

关于c - 如何将日转换为年月周然后天,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52092223/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com