- /************电子万年历******************
- *功能:
- * 可以查看当前日期的任意上一个月
- * 和下一个月的日期列表
- *
- *输入:输入j,查看上一个月。
- * 输入k,查看下一个月。
- * 输入q,退出。
- *
- *作者:晓*
- *****************************************/
- #include <iostream>
- #include <time.h>
- using namespace std;
- /*输入参数为某一年,判断是否为闰年,是返回1,否返回0*/
- #define check_runyear(year) (((year%4==0)&&(year%100!=0))||(year%400==0))
- /*计算这个月开始时需要输入的空格数*/
- #define get_space(day, week) (7-(6-week+day)%7)
- /************************************
- *功能:根据当前的日期计算上个月对应的星期
- *参数:year --- 当前年号
- * month --- 前一个月
- * week --- 当前日期的星期
- *
- *返回:前一个月与当前日期对应的星期
- ************************************/
- #define get_upweek(year, month, week) ((week-get_lastday(year, month)%7+7)%7)
- /************************************
- *功能:根据当前的日期计算上个月对应的星期
- *参数:year --- 当前年号
- * month --- 当月
- * week --- 当前日期的星期
- *
- *返回:后一个月与当前日期对应的星期
- ************************************/
- #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_week = get_downweek(p_year, p_month, p_week);
- 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;
- }
电子万年历
最新推荐文章于 2024-04-24 22:50:08 发布