七天网络如何打印试卷

七天网络如何打印试卷

首页技巧更新时间:2025-03-26 10:40:37
基本展示要求:
  1. 显示当前月份的日历,头部是年份和月份,然后是星期几的标题(日 一 二 三 四 五 六)。
  2. 能够录入查询上月、下月
  3. 能够录入任意年度的月份并能展示日历信息

实现思路
  1. 计算该月第一天是星期几,然后确定在日历中的起始位置。
  2. 循环打印日期,每七天换行。
  3. 处理用户输入,n:下个月,p:上个月,y:指定年份,q退出。
  4. 更新当前年月,循环直到退出。

代码实现

以下是使用C语言编写的可视化万年历的详细代码,支持上下月切换和日期查询功能:

/* #include <stdio.h> #include <stdlib.h> #include <time.h> */ #include <stdio.h> #include <stdlib.h> #include <time.h> // 判断闰年函数 int is_leap_year(int year) { return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0); } // 获取月份天数 int get_month_days(int year, int month) { const int days[] = {31,28,31,30,31,30,31,31,30,31,30,31}; if (month == 2 && is_leap_year(year)) return 29; return days[month-1]; } // 计算某月第一天是星期几(0=周日,1=周一...6=周六) int get_first_day(int year, int month) { struct tm t = {0}; t.tm_year = year - 1900; t.tm_mon = month - 1; t.tm_mday = 1; mktime(&t); return t.tm_wday; } // 清屏函数(跨平台) void clear_screen() { #ifdef _WIN32 system("cls"); #else system("clear"); #endif } // 显示日历函数 void display_calendar(int year, int month) { printf("\n d年d月\n", year, month); printf("日 一 二 三 四 五 六\n"); int first_day = get_first_day(year, month); int days = get_month_days(year, month); int current_pos = 0; // 打印首行缩进 for (int i = 0; i < first_day; i) { printf(" "); current_pos ; } // 打印日期 for (int day = 1; day <= days; day) { printf("- ", day); current_pos ; if (current_pos % 7 == 0) { printf("\n"); } } // 最后一周补齐换行 if (current_pos % 7 != 0) printf("\n"); } int main() { time_t now = time(NULL); struct tm *local = localtime(&now); int year = local->tm_year 1900; int month = local->tm_mon 1; while (1) { clear_screen(); display_calendar(year, month); printf("\n操作指南:n-下月 p-上月 y-指定年月 q-退出\n"); printf("请输入命令:"); char cmd[10]; scanf("%s", cmd); switch (cmd[0]) { case 'n': // 下个月 if ( month > 12) { month = 1; year ; } break; case 'p': // 上个月 if (--month < 1) { month = 12; year--; } break; case 'y': // 指定年月 printf("\n请输入年份(如2023):"); scanf("%d", &year); printf("请输入月份(1-12):"); scanf("%d", &month); month = month < 1 ? 1 : (month > 12 ? 12 : month); break; case 'q': // 退出程序 clear_screen(); printf("感谢使用万年历!\n"); return 0; default: printf("无效命令,请重新输入!"); getchar(); // 等待用户确认 break; } } }代码说明:

  1. 日期计算
  1. 界面特性
  1. 操作功能
  1. 扩展性

使用说明:
  1. 编译运行程序后会显示当前月份的日历
  2. 根据提示输入操作指令:

输出效果

2025年03月 日 一 二 三 四 五 六 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 操作指南:n-下月 p-上月 y-指定年月 q-退出 请输入命令:扩展建议:

  1. 添加颜色显示(可使用ANSI转义码)
  2. 增加节日/节气显示
  3. 添加备忘录功能
  4. 支持周视图显示
  5. 添加日期选择功能

该程序在Windows/Linux/macOS终端环境下均可运行,实现了基本的日历查看功能,代码结构清晰易于扩展。

,
大家还看了
也许喜欢
更多栏目

© 1998-2024 shitiku.com.cn,All Rights Reserved.