从键盘输入你的出生年月,编程输出你出生的月份有多少天,是什么季节,假设规定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;
int c = 30;
int d = 31;
int e = 28;
int f = 29;
printf("Input year,month:");
scanf("%d, %d",&b,&a);
if(b>0)
{
if(a>=1&&a<=12)
{
if(b%4==0&&b%100!=0||b%400==0)
{
if(a==1||a==3||a==5||a==7||a==8||a==10||a==12)
{
printf("%d days\n",d);
}
else if(a==2)
{
printf("%d days\n",f);
}
else
{
printf("%d days\n",c);
}
if(a>=3&&a<=5)
{
printf("The season is spring\n");
}
else if(a>=6&&a<=8)
{
printf("The season is summer\n");
}
else if(a>=9&&a<=11)
{
printf("The season is autumn\n");
}
else
{
printf("The season is winter\n");
}
}
else
{
if(a==1||a==3||a==5||a==7||a==8||a==10||a==12)
{
printf("%d days\n",d);
}
else if(a==2)
{
printf("%d days\n",e);
}
else
{
printf("%d days\n",c);
}
if(a>=3&&a<=5)
{
printf("The season is spring\n");
}
else if(a>=6&&a<=8)
{
printf("The season is summer\n");
}
else if(a>=9&&a<=11)
{
printf("The season is autumn\n");
}
else
{
printf("The season is winter\n");
}
}
}
else
{
printf("Input error!\n");
}
}
else
{
printf("Input error!\n");
}
return 0;
}
方法二:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int year, month, n;
printf("Input year,month:");
n = scanf("%d, %d", &year, &month); //1
if (n != 2 || year < 1)//1
{
printf("Input error!\n");
exit(0);//1
}
switch (month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
printf("31 days\n");
break;//1
case 2://1
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) //1
printf("29 days\n"); //1
else
printf("28 days\n");
break; //1
case 4:
case 6:
case 9:
case 11:
printf("30 days\n");
break;
default:
printf("Input error!\n");
exit(0);
}
if (month >= 3 && month <= 5)//1
printf("The season is spring\n");
else if (month >= 6 && month <= 8)//1
printf("The season is summer\n");
else if (month >= 9 && month <= 11)//1
printf("The season is autumn\n");
else
printf("The season is winter\n");
return 0;
}