从键盘输入你的出生年月,编程输出你出生的月份有多少天,是什么季节,假设规定3~5月为春季,6~8月为夏季,9~11月为秋季,1、2和12月为冬季。要求程序能够在用户输入非法字符或者非法的年份和月份时输出错误提示信息:"Input error!\n",并结束程序的执行。 要求考虑闰年的情况,已知满足如下条件之一的就是闰年: (1)能被4整除但不能被100整除; (2)能被400整除。 **输入格式要求:"%d, %d" 提示信息:"Input year,month:" **输出天数的格式要求:"%d days\n" **输出季节的格式要求:"The season is spring\n","The season is summer\n","The season is autumn\n","The season is winter\n" 如果输入了非法字符或者年份小于1或者月份不在合法的区间内,则输出"Input error!\n",并结束程序的执行。 程序运行示例如下: Input year,month:2012,2↙ 29 days The season is winter
代码如下
#include<stdio.h>
#include<math.h>
int main()
{
int a,b,c,d,e,f;
printf("Input year,month:");
scanf("%d, %d",&a,&b);
c=a%4;
d=a%100;
e=a%400;
if((b>=1&&b<=12)&&(a>=1))
{
if(b==1||b==3||b==5||b==7||b==8||b==10||b==12)
{
f=31;
printf("%d days\n",f);
}
if(((c==0&&d!=0)||(e==0))&&(b==2))
{
f=29;
printf("%d days\n",f);
}
if(((c!=0)||(d==0&&e!=0))&&(b==2))
{
f=28;
printf("%d days\n",f);
}
else if(b==4||b==6||b==9||b==11)
{
f=30;
printf("%d days\n",f);
}
if(b>=3&&b<=5)
{
printf("The season is spring\n");
}
if(b>=6&&b<=8)
{
printf("The season is summer\n");
}
if(b>=9&&b<=11)
{
printf("The season is autumn\n");
}
if(b==1||b==2||b==12)
{
printf("The season is winter\n");
}
}
else
printf("Input error!\n");
}