我正在尝试编写一个c程序,用于确定用户提供日期的一年中的日期编号(1到366)。 我如何调整这个程序闰年,如(2012年12月31日)是第366天,因为它是闰年。到目前为止这是我的代码。我想我在切换之前需要一个if staement但是我不知道如何实现请帮助
#include <stdio.h>
int isleap(int year);
void displaydate(int month ,int date ,int year,int daynumber);
int main (void) {
int month;
int day;
int year;
int leapyear;
int daynumber;
printf(" enter a month");
scanf("%d",&month);
printf(" enter a day");
scanf("%d",&day);
printf(" enter a year");
scanf("%d",&year);
leapyear= isleap( year);
displaydate( month,day,year,daynumber);
daynumber = leapyear && month > 2 ? day+1 : day;
switch(month) {
case 1 : daynumber = day;
break;
case 2 : daynumber = 31 + day;
break;
case 3 : daynumber = 59 + day + 1;
break;
case 4 : daynumber = 89 + day;
break;
case 5 : daynumber = 120 + day;
break;
case 6 : daynumber = 150 + day;
break;
case 7 : daynumber = 181 + day;
break;
case 8 : daynumber = 212 + day;
break;
case 9 : daynumber = 232 + day;
break;
case 10 : daynumber = 263 + day;
break;
case 11 : daynumber = 293 + day;
break;
case 12 : daynumber = 324 + day;
break;
default:
break;
}
return 0;
}
int isleap(int year)
{
if (((year % 4 == 0) && (year % 100!= 0)) || (year%400 == 0)) {
printf("%d 1", year);
return 1;
}
else {
printf("%d 0", year);
return 0;
}
}
void displaydate(int month ,int date ,int year,int daynumber)
{
printf("\n your date is %d and the day number is %d",month,date,year,daynumber);
}
答案 0 :(得分:0)
您可以使用三元运算符,只需在切换之前添加此代码:
day = leapyear && month > 2 ? day+1 : day;
答案 1 :(得分:0)
int isleap(int year)
{
if (((year % 4 == 0) && (year % 100!= 0)) || (year%400 == 0))
printf("%d 1", year);
else
printf("%d 0", year);
}
上面的函数应该返回是否是闰年,所以更改代码以正确返回如下。
int isleap(int year)
{
if (((year % 4 == 0) && (year % 100!= 0)) || (year%400 == 0)){
printf("%d 1", year);
return 1;
}else{
printf("%d 0", year);
return 0; }
}
现在在上面添加以下语句切换案例,如果它是闰年且月份大于2,则增加一天。
day = leapyear && (month>2) ? day+1 : day;
现在你将得到正确的结果。
希望这会有所帮助。
答案 2 :(得分:0)
除了提出的其他问题之外,您在switch
声明中添加的天数也是错误的。它应该是:
switch (month) {
case 1 : daynumber = day; break;
case 2 : daynumber = 31 + day + leapyear; break;
case 3 : daynumber = 59 + day + leapyear; break;
case 4 : daynumber = 90 + day + leapyear; break;
case 5 : daynumber = 120 + day + leapyear; break;
case 6 : daynumber = 151 + day + leapyear; break;
case 7 : daynumber = 181 + day + leapyear; break;
case 8 : daynumber = 212 + day + leapyear; break;
case 9 : daynumber = 243 + day + leapyear; break;
case 10 : daynumber = 273 + day + leapyear; break;
case 11 : daynumber = 304 + day + leapyear; break;
case 12 : daynumber = 334 + day + leapyear; break;
default: break;
}
您在计算之前也无法显示daynumber
,您需要按如下方式对计算进行排序:
...
leapyear = isleap (year);
switch (month) {
case 1 : daynumber = day; break;
case 2 : daynumber = 31 + day + leapyear; break;
case 3 : daynumber = 59 + day + leapyear; break;
case 4 : daynumber = 90 + day + leapyear; break;
case 5 : daynumber = 120 + day + leapyear; break;
case 6 : daynumber = 151 + day + leapyear; break;
case 7 : daynumber = 181 + day + leapyear; break;
case 8 : daynumber = 212 + day + leapyear; break;
case 9 : daynumber = 243 + day + leapyear; break;
case 10 : daynumber = 273 + day + leapyear; break;
case 11 : daynumber = 304 + day + leapyear; break;
case 12 : daynumber = 334 + day + leapyear; break;
default: break;
}
displaydate (month, day, year, daynumber);
...
printf
中的displaydate
语句无法为您传递的每个参数提供转换说明符。看来你打算:
void displaydate (int month, int date, int year, int daynumber)
{
printf ("\n your date is %d/%d/%d and the day number is %d\n",
month, date, year, daynumber);
}
注意: "%d/%d/%d"
mm/dd/yyyy
如果你将所有这些部分放在一起,你会得到:
#include <stdio.h>
int isleap (int year);
void displaydate (int month, int date , int year, int daynumber);
int main (void) {
int month = 0,
day = 0,
year = 0,
leapyear = 0,
daynumber = 0;
printf (" enter month: ");
scanf ("%d", &month);
printf (" enter day: ");
scanf ("%d", &day);
printf (" enter year: ");
scanf ("%d", &year);
leapyear = isleap (year);
switch (month) {
case 1 : daynumber = day; break;
case 2 : daynumber = 31 + day + leapyear; break;
case 3 : daynumber = 59 + day + leapyear; break;
case 4 : daynumber = 90 + day + leapyear; break;
case 5 : daynumber = 120 + day + leapyear; break;
case 6 : daynumber = 151 + day + leapyear; break;
case 7 : daynumber = 181 + day + leapyear; break;
case 8 : daynumber = 212 + day + leapyear; break;
case 9 : daynumber = 243 + day + leapyear; break;
case 10 : daynumber = 273 + day + leapyear; break;
case 11 : daynumber = 304 + day + leapyear; break;
case 12 : daynumber = 334 + day + leapyear; break;
default: break;
}
displaydate (month, day, year, daynumber);
return 0;
}
int isleap (int year)
{
if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0))
return 1;
return 0;
}
void displaydate (int month, int date, int year, int daynumber)
{
printf ("\n your date is %d/%d/%d and the day number is %d\n",
month, date, year, daynumber);
}
此外,请注意,您必须验证所有用户输入(上面省略)。至少应该检查每个scanf
的回报,类似于:
if (scanf ("%d", &month) != 1) {
fprintf (stderr, "error: invalid month entered.\n");
return 1;
}
仔细看看,如果您有任何问题,请告诉我。