c語言小專案----日曆
阿新 • • 發佈:2018-12-16
#include<stdio.h> #include<stdlib.h> #include<string.h> int LeapDays[12]={31,29,31,30,31,30,31,31,30,31,30,31};//閏年月份 int CommonDay[12]={31,28,31,30,31,30,31,31,30,31,30,31};//非閏年月份 int Createmonthdata(int MonthDay[6][7],int year,int month);//規定格式賦值 void printMoth(int MonthDay[6][7]);//規範輸出格式 int IsleapYear(int year);//判斷閏年 int IsValidData(int year,int month,int day);//判斷輸入是否符合規定格式 int GetDayInYear(int year,int month,int day);//求某一天是那一年的第幾天 int GetDaysInMonth(int year,int month);//計算需求月有多少天 int GETweek(int year,int month,int day);//求某一天是在第幾周 int main(int argc, const char *argv[]) { int Mdate[6][7]={{0}}; int y=0; int m=0; int ret=0; while(1) { printf("請輸入年和月: \n"); scanf("%d%d",&y,&m); printf(" %10d 年 %d 月 \n",y,m); if(m<0||m>12) { printf("月輸入有錯誤,請重新輸入\n"); return -1; } printf("---------------------------\n"); ret=Createmonthdata(Mdate,y,m); if(ret==0) { printMoth(Mdate); } printf("---------------------------\n"); } return 0; } int Createmonthdata(int MonthDay[6][7],int year,int month) { int week=GETweek(year,month,1);//返回第幾周 int day=1; int i=0; int j=0; int daysInMonth=GetDaysInMonth(year,month);//當月天數 if(week<0) { printf("GETweek failed\n"); return -1; } /*給第0行賦值*/ for(j=0;j<7;j++) { if(j<week) { MonthDay[0][j]=0; } else { MonthDay[0][j]=day; day++; } } /*給第1~5行賦值*/ for(i=1;i<6;i++) { for(j=0;j<7;j++) { if(day>daysInMonth) { MonthDay[i][j]=0; } else { MonthDay[i][j]=day; day++; } } } return 0; } /*讓二維陣列如下顯示*/ /* * 日 |一 |二 |三 |四 |五 |六 * | 1 | 2 | 3 | 4 | 5 | 6 | 7 * | 8 | 9 |10 |11 |12 |13 |14 * |15 |16 |17 |18 |19 |20 |21 * |22 |23 |24 |25 |26 |27 |28 * |29 |30 |31 | 0 | 0 | 0 | 0 * | 0 | 0 | 0 | 0 | 0 | 0 | 0 */ void printMoth(int MonthDay[6][7]) { int i=0; int j=0; printf(" 日 |一 |二 |三 |四 |五 |六 \n"); printf("---------------------------\n"); for(i=0;i<6;i++) { for(j=0;j<7;j++) { printf("|%2d ",MonthDay[i][j]); } printf("\n"); } } int IsleapYear(int year) { if((year %4 == 0&&year % 100!=0)||(year %400==0))//判斷閏年,閏年返回1,否則返回0 { return 1; } else { return 0; } } int IsValidData(int year,int month,int day)//判斷輸入是否符合規定格式,符合返回0,否則返回1 { int ret=1; if(month<1||month>12||day<1||year<=0) { return 0; } if(IsleapYear(year)) { if(day>LeapDays[month-1]) { ret=0; } } else { if(day>CommonDay[month-1]) { ret=0; } } return ret; } int GetDayInYear(int year,int month,int day)//求某一天是那一年的第幾天 { int sum=0;//總天數 int isrun=IsleapYear(year);//閏年返回1,否則返回0 int i=0; int j=0; if(isrun) { for(i=0;i<month-1;i++) { sum=sum+LeapDays[i]; } } else { for(i=0;i<month-1;i++) { sum=sum+CommonDay[i]; } } sum=sum+day; return sum; } int GetDaysInMonth(int year,int month)//計算需求月有多少天 { int isrun=IsleapYear(year);//閏年返回1,否則返回0 if(isrun) { return LeapDays[month-1]; } else { return CommonDay[month-1]; } } int GETweek(int year,int month,int day)//求某天是第幾周,是第幾周就返回幾,週日返回0,出錯返回-1, { int sum=0; if(0==IsValidData(year,month,day)) { printf("Input data is invald\n"); return -1; } sum = year -1; sum = sum + sum / 4 - sum / 100 + sum / 400; sum = sum + GetDayInYear(year,month,day); return sum % 7; }