从键盘输入你的出生年月,编程输出你出生的月份有多少天,是什么季节,假设规定3~5月为春季,6~8月为夏季,9~11月为秋季,1、2和12月为冬季。

从键盘输入你的出生年月,编程输出你出生的月份有多少天,是什么季节,假设规定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;
} 

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

那不勒斯的萤火丶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值