输入年,月,日,计算是本年的第几天。两种算法,用if和函数,只用if没问题,加上函数就会出问题,求指导
#include <stdio.h>#include <stdlib.h>
void leap();
void number();
int main()
{
//输入年月日,求是这一年的第几天;
int year, month,day,n;
int sumday;
printf("请输入年月日:");
scanf("%d%d%d",&year,&month,&day);
if(year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
{
switch(month)
{
case 1:
sumday = day;
printf("今天是今年的第%d天",sumday);
break;
case 2:
sumday = day + 31;
printf("今天是今年的第%d天",sumday);
break;
case 3:
sumday = day + 60;
printf("今天是今年的第%d天",sumday);
break;
case 4:
sumday = day + 91;
printf("今天是今年的第%d天",sumday);
break;
case 5:
sumday = day + 121;
printf("今天是今年的第%d天",sumday);
break;
case 6:
sumday = day + 152;
printf("今天是今年的第%d天",sumday);
break;
case 7:
sumday = day + 182;
printf("今天是今年的第%d天",sumday);
break;
case 8:
sumday = day + 213;
printf("今天是今年的第%d天",sumday);
break;
case 9:
sumday = day + 244;
printf("今天是今年的第%d天",sumday);
break;
case 10:
sumday = day + 274;
printf("今天是今年的第%d天",sumday);
break;
case 11:
sumday = day + 305;
printf("今天是今年的第%d天",sumday);
break;
case 12:
sumday = day + 335;
printf("今天是今年的第%d天",sumday);
break;
}
}
else
{
switch(month)
{
case 1:
sumday = day;
printf("今天是今年的第%d天",sumday);
break;
case 2:
sumday = day + 31;
printf("今天是今年的第%d天",sumday);
break;
case 3:
sumday = day + 59;
printf("今天是今年的第%d天",sumday);
break;
case 4:
sumday = day + 90;
printf("今天是今年的第%d天",sumday);
break;
case 5:
sumday = day + 120;
printf("今天是今年的第%d天",sumday);
break;
case 6:
sumday = day + 151;
printf("今天是今年的第%d天",sumday);
break;
case 7:
sumday = day + 181;
printf("今天是今年的第%d天",sumday);
break;
case 8:
sumday = day + 212;
printf("今天是今年的第%d天",sumday);
break;
case 9:
sumday = day + 243;
printf("今天是今年的第%d天",sumday);
break;
case 10:
sumday = day + 273;
printf("今天是今年的第%d天",sumday);
break;
case 11:
sumday = day + 304;
printf("今天是今年的第%d天",sumday);
break;
case 12:
sumday = day + 334;
printf("今天是今年的第%d天",sumday);
break;
}
}
//利用函数计算
n = number(year, month, day);
printf("今天是今年的第%d天",sumday);
return 0;
}
void leap(int a)
{
if(a % 4 == 0 && a % 100 != 0 || a % 400 == 0)
return 1;
else
return 0;
}
void number(int year,int month,int day)
{
int sum = 0, i;
int a[12] = {31,28,31,30,31,30,31,31,30,31,30,31};
int b[12] = {31,29,31,30,31,30,31,31,30,31,30,31};
if(leap(year) == 1)
for(i = 0;i < month-1; i++)
{
sum += b[i];
}
else
for(i = 0;i < month-1; i++)
{
sum += a[i];
}
sum = sum +day;
return sum;
}
||=== 构建: Debug 在 example14 day 中 (编译器: GNU GCC Compiler) ===|
E:\codeblocks\c class 282\example14 day\main.c||In function 'main':|
E:\codeblocks\c class 282\example14 day\main.c|13|warning: suggest parentheses around '&&' within '||' [-Wparentheses]|
E:\codeblocks\c class 282\example14 day\main.c|122|error: void value not ignored as it ought to be|
E:\codeblocks\c class 282\example14 day\main.c|9|warning: variable 'n' set but not used [-Wunused-but-set-variable]|
E:\codeblocks\c class 282\example14 day\main.c||In function 'leap':|
E:\codeblocks\c class 282\example14 day\main.c|128|warning: suggest parentheses around '&&' within '||' [-Wparentheses]|
E:\codeblocks\c class 282\example14 day\main.c|129|warning: 'return' with a value, in function returning void|
E:\codeblocks\c class 282\example14 day\main.c|131|warning: 'return' with a value, in function returning void|
E:\codeblocks\c class 282\example14 day\main.c||In function 'number':|
E:\codeblocks\c class 282\example14 day\main.c|138|error: void value not ignored as it ought to be|
E:\codeblocks\c class 282\example14 day\main.c|149|warning: 'return' with a value, in function returning void|
||=== 构建 失败: 2 error(s), 6 warning(s) (0 分, 0 秒) ===|