天天看點

c語言 簡單的天數電腦

#include<stdio.h>
#include<stdbool.h>
#include<stdlib.h>
int count_days(int month,bool leap)
{
    switch(month)
    {
        case 1:case 3:case 5:case 7:case 8:case 10:case 12:month=31;break;
        case 2:
            if(leap)
                month=28;
            else
                month=29;
            break;
        default:month=30;
    }
    return month;
}
bool is_leap(int year)
{
    if((year%4==0&&year%100!=0)||year%400==0)
        return true;
    else
        return false;
}
int sum(int month,int day,bool leap){
    int Days=0;
    for(int i=1;i<month;i++)
        Days+=count_days(i, leap);
    Days+=day;
    return Days;
}
int main(){
    int year;
    int month;
    int day;
    printf("一次輸入年月日,分别以空格隔開\n");
    scanf("%d%d%d",&year,&month,&day);
    bool leap=is_leap(year);
    int Mon_day=count_days(month, leap);
    if(day>Mon_day){
        printf("error\n");
        exit(0);
    }
    int count=sum(month, day, leap);
    printf("今年已經過了%d天\n",count);
}