- #include <stdio.h>
- #include <stdlib.h>
- #include <windows.h>
- //判断每个月的第一天是星期几,
- int judgeFirstDate(int year, int m, int q)
- {
- if(m == 1 || m == 2)
- {
- m += 12;//如果月份为1,2,则表示为去年的13,14月,年份也改为去年
- --year;
- }
- int j = year / 100;//j表示世纪
- int k = year % 100;//k表示本世纪的第几年
- int h = (q + 26*(m+1)/10 + k + k/4 + j/4 + 5*j) % 7;//h=0为周六,h=1为周日,依次类推
- return h;
- }
- //处理周几,把h=0为周六改为h=0为周日,把h=1为周日改为h=1为周一,依次类推
- int judgeWeek(int week)
- {
- if(!week)
- return 6;
- else
- return --week;
- }
- //把输入的字符串转换为数字年份
- void input(char* cTime, int* year, int* month, int* day)
- {
- int len = strlen(cTime);
- if(cTime[0] == '+')
- {
- ++(*month);
- return;
- }
- else if(cTime[0] == '-')
- {
- --(*month);
- return ;
- }
- *year = *month = *day = 0;
- *year = atoi(cTime);
- if(len >= 6)
- *month = atoi(cTime+5);
- if(len >= 8)
- *day = atoi(cTime+7);
- }
- //判断是不是闰年
- int isLeapYear(int year)
- {
- if((year%400 == 0) || (year%4 == 0 && year%100 != 0))
- return 1;
- return 0;
- }
- //输出月份最上边的哪个导航
- void outputWeek()
- {
- printf("Sunday Monday Tuesday Wednesday Thursday Friday Saturday\n");
- }
- //判断某年某个月份有几天
- int monthDay(int year, int month)
- {
- int judge = isLeapYear(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:
- return judge == 1 ? 29 : 28;
- break;
- }
- return 0;
- }
- //输出某年的某个月
- void outputOneMonth(int year, int month, int day)
- {
- int i;
- int dayNum = monthDay(year,month);
- int week = judgeFirstDate(year,month,1);
- week = judgeWeek(week);
- outputWeek();
- for(i = 0; i < week; ++i)
- printf(" ");
- for(i = 1; i <= dayNum; ++i)
- {
- if(day == i) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),
- FOREGROUND_GREEN | FOREGROUND_INTENSITY);
- printf("%-10d",i);
- if(day == i) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),
- FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
- if((week+1)%7 == 0)
- printf("\n");
- ++week;
- }
- if(week %7 != 0)
- printf("\n");
- }
- //输出一年的所有月份
- void outputAllMonth(int year)
- {
- int i;
- for(i = 1; i <= 12; ++i)
- {
- outputOneMonth(year,i,0);
- printf("\n");
- }
- }
- int main()
- {
- int year,month,day;
- char cTime[11];
- while(fgets(cTime,11,stdin))
- {
- //读入数据
- input(cTime,&year,&month,&day);
- printf("%d",year);
- if(month == 0)
- {
- printf("\n");
- //输出一年的所有月份
- outputAllMonth(year);
- }
- else
- {
- printf(".%d",month);
- if(day != 0)
- printf(".%d",day);
- printf("\n");
- //输出某个月份
- outputOneMonth(year,month,day);
- }
- printf("+:last month\n-:next month\n");
- }
- return 0;
- }
简单万年历
最新推荐文章于 2024-06-14 23:39:45 发布