#include <stdio.h>//返回指定年月日是對應年度的第幾天int getDays(int month,int day);int isRunnian(int year);int main(int argc,char* argv[]){int year=0;int month=0;int day=0;printf("請輸入年月日格式示例20140101\n");scanf("%4d%2d%2d",&year,&month,&day);//簡化後的條件,是閏年且月份大於二,則返回天數加一,否則直接返回函式值int days = getDays(month,day);if (days==0){printf("您輸入的日期格式無效。\n");return 0;}if (isRunnian(year) && month >2){printf("輸入日期是對應年份的第%d天",days+1);}else{printf("輸入日期是對應年份的第%d天",days);}return 0;}//迴圈實現int getDays(int month,int day){int NUMBER[12]={31,28,31,30,31,30,31,31,30,31,30,31};int result=day;if (month >12){ //避免出現數組越界情況return 0;}for (int i=0;i< month-1;i++){//陣列下標是從零開始result+= NUMBER[i];}return result;}int isRunnian(int year){//閏年的條件//一:年度是400的整數倍,則是閏年//二:年度能被4整除且不能被100整除return year%4 ==0 && year%100 !=0 || year%400 ==0;}
#include <stdio.h>//返回指定年月日是對應年度的第幾天int getDays(int month,int day);int isRunnian(int year);int main(int argc,char* argv[]){int year=0;int month=0;int day=0;printf("請輸入年月日格式示例20140101\n");scanf("%4d%2d%2d",&year,&month,&day);//簡化後的條件,是閏年且月份大於二,則返回天數加一,否則直接返回函式值int days = getDays(month,day);if (days==0){printf("您輸入的日期格式無效。\n");return 0;}if (isRunnian(year) && month >2){printf("輸入日期是對應年份的第%d天",days+1);}else{printf("輸入日期是對應年份的第%d天",days);}return 0;}//迴圈實現int getDays(int month,int day){int NUMBER[12]={31,28,31,30,31,30,31,31,30,31,30,31};int result=day;if (month >12){ //避免出現數組越界情況return 0;}for (int i=0;i< month-1;i++){//陣列下標是從零開始result+= NUMBER[i];}return result;}int isRunnian(int year){//閏年的條件//一:年度是400的整數倍,則是閏年//二:年度能被4整除且不能被100整除return year%4 ==0 && year%100 !=0 || year%400 ==0;}