电子万年历
来源:互联网 发布:鸦片战争观后感知乎 编辑:程序博客网 时间:2024/04/27 20:20
- #include <iostream>
- #include <time.h>
- using namespace std;
- #define check_runyear(year) (((year%4==0)&&(year%100!=0))||(year%400==0))
- #define get_space(day, week) (7-(6-week+day)%7)
- #define get_upweek(year, month, week) ((week-get_lastday(year, month)%7+7)%7)
- #define get_downweek(year, month, week) ((week+get_lastday(year, (month))%7)%7)
- int get_lastday(int year, int month);
- struct tm* get_currday();
- void print_date(int year, int month, int day, int curweek);
- int get_lastday(int year, int month)
- {
- int all_month[]={31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
-
- if (check_runyear(year)&&(month==2))
- return all_month[month-1]+1;
- else
- return all_month[month-1];
- }
- struct tm* get_currday()
- {
- time_t tmp_time;
- struct tm* curdate;
- tmp_time = time(NULL);
- curdate = localtime(&tmp_time);
- return curdate;
- }
- void print_date(int year, int month, int day, int curweek)
- {
- int space = 0;
- int allday = 0;
- int i = 1;
- space = get_space(day, curweek);
- allday = get_lastday(year, month);
- printf("%12d年%d月%d日/n", year, month, day);
- printf("%16s%d/n/n", "星期", curweek);
- printf(" 日 一 二 三 四 五 六/n");
- printf(" --------------------------/n");
- for (i = 1; i<=space+allday; i++)
- {
- if (i>=1 && i<=space)
- printf(" ");
- else
- printf("%4d", i-space);
- if (i%7 == 0)
- printf("/n");
- }
- printf("/n");
- return ;
- }
- void test()
- {
- int p_year, p_month, p_day, p_week;
- struct tm *tmp;
- char tmpchar;
- tmp = get_currday();
- p_year = tmp->tm_year+1900;
- p_month = tmp->tm_mon+1;
- p_day = tmp->tm_mday;
- p_week = tmp->tm_wday;
-
- print_date(p_year, p_month, p_day, p_week);
- while (1)
- {
- tmpchar = getchar();
- switch (tmpchar)
- {
- case 'l':
- print_date(p_year, p_month, p_day, p_week);
- break;
- case 'j':
- p_month--;
- if (p_month == 0)
- {
- p_year--;
- p_month = 12;
- }
- p_week = get_upweek(p_year, p_month, p_week);
- print_date(p_year, p_month, p_day, p_week);
- break;
- case 'k':
- p_month++;
- if (p_month == 13)
- {
- p_year++;
- p_month = 1;
- }
- print_date(p_year, p_month, p_day, p_week);
- break;
- case 'q':return ;
- }
- }
- return ;
- }
- int main(int argc, char *argv[])
- {
- test();
- return 0;
- }