這個仍是挺實用的。。。。函數
頭文件:spa
#ifndef MAIN_H #define MAIN_H #include "stdio.h" #include "math.h" #include "stdlib.h" //獲取某一個月的最大天數 int monthday(int,int ); //判斷閏年 ,是返回1,不是返回0 int isleapyear(int ); #endif
主函數:code
#include "main.h" int main() { int year,month,days,weekday; int i,d; while(1) { printf("please input the year:\n"); scanf("%d",&year); days = year-1+(year-1)/400+(year-1)/4-(year-1)/100;//計算某年第一天是星期幾 for(month=1;month<=12;month++) { printf("\t\t****%d年--%d月****\n",year,month); printf("sun\tmon\ttues\twed\tthur\tfir\tsat\t\n");//表頭 i = 1; d = 1; weekday = (days + 1)%7; //求星期幾 while(i<=weekday) //輸出前面的空格 { printf("\t"); i++; } while(d<=monthday(month,year)) //輸出日期 { weekday = (days + 1)%7; if(weekday==6) //最後一個是星期六,輸出以後要換行 printf("%d\n",d); else //不是星期六的輸出後不換行 printf("%d\t",d); if(d==monthday(month,year)) printf("\n"); d++; days++; } } } }
功能函數實現:blog
#include "main.h" int monthday(int month,int year) { switch(month) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: return 31;break; case 4: case 6: case 9: case 11: return 30;break; case 2: if(isleapyear(year)) { return 29;//閏年29天 break; } else { return 28; break; } } } int isleapyear(int year) { if((year%4==0)&&(year%100!=0)||(year%400==0)) return 1; else return 0; }
效果圖:input