C语言编程(练习4:分支和跳转 )

题目:输入年月日,计算该天是该年的第多少天?(请使用 case)

分析:月份天数分为三种

31天的有:1、3、5、7、8、10、12

30天的有:4、6、9、11

28/29天的是2月,需要进行闰年与否的判断

/**<  输入年月日,计算该天是该年的第多少天?(请使用 case) */
/**< 需要判断是否为闰年:可以被4整除,但不能被100整除;能被400整除 */
#include <stdio.h>
#include <stdlib.h>

int isLeapYear(int);
int main()
{
    int year;
    int mouth;
    int day;
    int num = 0;
    printf("输入年份1900-2200\n");
    scanf("%d",&year);
    printf("输入月份1-12\n");
    scanf("%d",&mouth);
    printf("输入日期1-31\n");
    scanf("%d",&day);
    switch(mouth)
    {
        case 12: num += 30;
        case 11: num += 31;
        case 10: num += 30;
        case 9: num += 31;
        case 8: num += 31;
        case 7: num += 30;
        case 6: num += 31;
        case 5: num += 30;
        case 4: num += 31;
        case 3: num += 28;
        case 2: num += 31;
    }
    if(isLeapYear(year) && mouth>2)
        num = num + 1;
    num = num + day;
    printf("%d年%d月%d日是这年的第%d天",year,mouth,day,num);
    return 0;
}

int isLeapYear(int year)
{
    if(((year%4==0)&&(year%100!=0))||(year%400==0)) //判断为闰年
    {
        return 1;
    }
    else return 0;

}

运行结果:



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值