定义一个结构体变量(包括年、月、日)。计算该日在本年中是第几天?注意闰年问题。 输入格式要求:"请输入日期(年,月,日)\n" 输出格式要求:"%d月%d日是%d年的第%d天\n"
#include<stdio.h>
struct date
{
int year;
int month;
int day;
};
int calculate(date a);
int main()
{
int x;
date a;
printf("请输入日期(年,月,日)\n");
scanf("%d, %d, %d", &a.year, &a.month, &a.day);
x=calculate(a)+a.day;
printf("%d月%d日是%d年的第%d天\n", a.month, a.day, a.year, x);
return 0;
}
int calculate(date a)
{
int x;
if((a.year%4==0&&a.year%100!=0)||a.year%400==0)
{
switch(a.month)
{
case 1: x=31;
case 2: x=60;
case 3: x=91;
case 4: x=121;
case 5: x=152;
case 6: x=182;
case 7: x=213;
case 8: x=244;
case 9: x=274;
case 10: x=305;
case 11: x=335;
case 12: x=366;
}
}
else
{
switch(a.month)
{
case 1: x=31;
case 2: x=59;
case 3: x=90;
case 4: x=120;
case 5: x=151;
case 6: x=181;
case 7: x=212;
case 8: x=243;
case 9: x=273;
case 10: x=304;
case 11: x=334;
case 12: x=365;
}
}
return x;
}
我这个程序哪里错了?
用了switch语句却没有break,所以x永远是365或366