#include <stdio.h>
int main()
{
/* 定义需要计算的日期 */
int year = 2008;
int month = 8;
int day = 8;
switch(month-1)
{
case 12:day+=31;
case 11:day+=30;
case 10:day+=31;
case 9:day+=30;
case 8:day+=31;
case 7:day+=31;
case 6:day+=30;
case 5:day+=31;
case 4:day+=30;
case 3:day+=31;
case 2:if((year%4==0&&year%100)||year%400==0) day+=29; else day+=28;
case 1:day+=31;
}
printf("是本年的第%d天",day);
return 0;
}
#include <stdio.h>
int main()
{
int days = 0;
int year = 2008;
int month = 8;
int day = 8;
switch(month)
{
case 12:
days += 30;
case 11:
days += 31;
case 10:
days += 30;
case 9:
days += 31;
case 8:
days += 31;
case 7:
days += 30;
case 6:
days += 31;
case 5:
days += 30;
case 4:
days += 31;
case 3:
days += 28;
case 2:
days += 31;
case 1:
days += 0;
break;
default:
printf("一年只有12个月哦!");
}
if(year%100==0&&year%400==0||year%4==0&&year%100!=0)
{
if(month>2)
{
days= days + day + 1;
}
else
{
days= days + day;
}
}
else
{
days= days + day;
}
printf("%d年%d月%d日是该年的第%d天。",year,month,day,days);
return 0;
}
看我的代码 我和你思路一样
#include <stdio.h>int main() { int days = 0; int year = 2008; int month = 8; int day = 8; switch(month) { case 12: days += 30; case 11: days += 31; case 10: days += 30; case 9: days += 31; case 8: days += 31; case 7: days += 30; case 6: days += 31; case 5: days += 30; case 4: days += 31; case 3: days += 28; case 2: days += 31; case 1: days += 0; break; default: printf("一年只有12个月哦!"); } if(year%100==0&&year%400==0||year%4==0&&year%100!=0) { if(month>2) { days= days + day + 1; } else { days= days + day; } } else { days= days + day; } printf("%d年%d月%d日是该年的第%d天。",year,month,day,days); return 0;}
学习了,还可以这样,初学者受益匪浅。
是不是case 12:day+=31;可以去掉了
printf("2008年8月8日是该年的第%d天",day);