万年历程序要求:

1.当选择1的时候,输入,打印输入的这一年12月的日历。
2.当选择2的时候,输入年-月,打印输入这一年这一月的日历。
实现效果:
选择1时

*********************Please chose*************************
*************1.Print a year's calendar *******************
*************2.Print a month's calendar*******************
**********************************************************
your chose:>1
input(year)>2022
2022-1Sun     Mon     Tue     Wed     Thu     Fri     Sat12       3       4       5       6       7       89       10      11      12      13      14      1516      17      18      19      20      21      2223      24      25      26      27      28      2930      31
2022-2Sun     Mon     Tue     Wed     Thu     Fri     Sat1       2       3       4       56       7       8       9       10      11      1213      14      15      16      17      18      1920      21      22      23      24      25      2627      28
2022-3Sun     Mon     Tue     Wed     Thu     Fri     Sat1       2       3       4       56       7       8       9       10      11      1213      14      15      16      17      18      1920      21      22      23      24      25      2627      28      29      30      31
2022-4Sun     Mon     Tue     Wed     Thu     Fri     Sat1       23       4       5       6       7       8       910      11      12      13      14      15      1617      18      19      20      21      22      2324      25      26      27      28      29      30
2022-5Sun     Mon     Tue     Wed     Thu     Fri     Sat1       2       3       4       5       6       78       9       10      11      12      13      1415      16      17      18      19      20      2122      23      24      25      26      27      2829      30      31
2022-6Sun     Mon     Tue     Wed     Thu     Fri     Sat1       2       3       45       6       7       8       9       10      1112      13      14      15      16      17      1819      20      21      22      23      24      2526      27      28      29      30
2022-7Sun     Mon     Tue     Wed     Thu     Fri     Sat1       23       4       5       6       7       8       910      11      12      13      14      15      1617      18      19      20      21      22      2324      25      26      27      28      29      3031
2022-8Sun     Mon     Tue     Wed     Thu     Fri     Sat1       2       3       4       5       67       8       9       10      11      12      1314      15      16      17      18      19      2021      22      23      24      25      26      2728      29      30      31
2022-9Sun     Mon     Tue     Wed     Thu     Fri     Sat1       2       34       5       6       7       8       9       1011      12      13      14      15      16      1718      19      20      21      22      23      2425      26      27      28      29      30
2022-10Sun     Mon     Tue     Wed     Thu     Fri     Sat12       3       4       5       6       7       89       10      11      12      13      14      1516      17      18      19      20      21      2223      24      25      26      27      28      2930      31
2022-11Sun     Mon     Tue     Wed     Thu     Fri     Sat1       2       3       4       56       7       8       9       10      11      1213      14      15      16      17      18      1920      21      22      23      24      25      2627      28      29      30
2022-12Sun     Mon     Tue     Wed     Thu     Fri     Sat1       2       34       5       6       7       8       9       1011      12      13      14      15      16      1718      19      20      21      22      23      2425      26      27      28      29      30      31

选择2时

*********************Please chose*************************
*************1.Print a year's calendar *******************
*************2.Print a month's calendar*******************
**********************************************************
your chose:>2
input(year-month)>2020-2
2020-2Sun     Mon     Tue     Wed     Thu     Fri     Sat12       3       4       5       6       7       89       10      11      12      13      14      1516      17      18      19      20      21      2223      24      25      26      27      28      29

程序中所用到的函数:

1.参考公式:

C语言根据日期判断星期几(使用基姆拉尔森计算公式)
算法如下:
基姆拉尔森计算公式
W= (d+2m+3(m+1)/5+y+y/4-y/100+y/400)%7
在公式中d表示日期中的日数,m表示月份数,y表示年数。
注意:在公式中有个与其他公式不同的地方
把一月和二月看成是上一年的十三月和十四月,
例:如果是2004-1-10则换算成:2003-13-10来代入公式计算。
以公元元年为参考,公元元年1月1日为星期一
改进后代码如下:
iY=2022, iM=7, iD=17 iWeekDay=0
iY=2022, iM=7, iD=18 iWeekDay=1

iY=2022, iM=7, iD=23 iWeekDay=6

int getWeekdayByYearday(int iY, int iM, int iD)
{/*iY=2022, iM=7, iD=17  iWeekDay=0iY=2022, iM=7, iD=18  iWeekDay=1...iY=2022, iM=7, iD=23  iWeekDay=6*/int iWeekDay = -1;if (1 == iM || 2 == iM){iM += 12;iY--;}iWeekDay = (iD + 1 + 2 * iM + 3 * (iM + 1) / 5 + iY + iY / 4 - iY / 100 + iY / 400) % 7;return iWeekDay;
}

2.得到某年某月有几天,并返回天数

int getmonthday(int year,int month)
{int day=0;switch(month){//当月是1,3,5,7,8,10,12时,天数返回31天//当月是4,6,9,11时,天数返回30天//当月是2月时,判断进行平闰年判断,//如果是闰年返回29天,平年返回28天case 1:case 3:case 5:case 7:case 8:case 10:case 12:day=31;break;case 4:case 6:case 9:case 11:day=30;break;case 2:{if(year%400==0||(year%4==0&&year%100!=0)){day=29;}else{day=28;}break;}}return day;
}

3.通过传递的参数输出某年某一月的日历

int printfmonth(int year,int month)
{//week是某年某月的第一天的是星期几int i,j,week=getWeekdayByYearday(year, month, 1);printf("%d-%d\n",year,month);printf("\tSun\tMon\tTue\tWed\tThu\tFri\tSat\n");//当小于等于第一天时,输出tab键进for(i=0;i<=week;i++){printf("\t");}//当小于等于月份天数时输出日历几号for(i=1,j=week;i<=getmonthday(year,month);i++){ //如果日期等于7时进行换行并输入tab键对齐//j赋值为0if(j==7){printf("\n");printf("\t");j=0;}printf("%d",i);//输入月份日期printf("\t");j++;}printf("\n");//当打完一个月份日历后进行换行
}

4.主函数

int main(int argc,const char * argv[])
{int chose=0;int ret=0;int year,month;int i;//打印表头printf("*********************Please chose*************************\n");printf("*************1.Print a year's calendar *******************\n");printf("*************2.Print a month's calendar*******************\n");printf("**********************************************************\n");printf("your chose:>");//判断输入的选项个数是否为一个,不是的话输出输入错误。ret=scanf("%d",&chose);if(ret!=1){printf("input chose error\n");return -1;}while((getchar()!='\n'));//吃掉垃圾字符//chose one时执行目的1//chose two时执行目的2switch(chose){case one:printf("input(year)>");ret=scanf("%d",&year);if(ret!=1){printf("input error\n");return -1;}while((getchar()!='\n'));//判断输入的年份是否正确,不正确的话输出输入错误if(year<=0){printf("input error\n");return -1;}//通过循环打印目的1的一年日历要求,12个月for(i=1;i<=12;i++){printfmonth(year,i);}break;case two:printf("input(year-month)>");ret=scanf("%d-%d",&year,&month);if(ret!=2){printf("input error\n");return -1;}while((getchar()!='\n'));//判断输入的年和月是否正确,不正确输出输入错误if(year<=0||month<0||month>12){printf("input error\n");return -1;}//打印输入的年-月的日历printfmonth(year,month);}return 0;
}

源代码如下:

#include <stdio.h>#define one 1
#define two 2int getWeekdayByYearday(int iY, int iM, int iD)
{int iWeekDay = -1;if (1 == iM || 2 == iM){iM += 12;iY--;}iWeekDay = (iD + 1 + 2 * iM + 3 * (iM + 1) / 5 + iY + iY / 4 - iY / 100 + iY / 400) % 7;return iWeekDay;
}
int getmonthday(int year,int month)
{int day=0;switch(month){case 1:case 3:case 5:case 7:case 8:case 10:case 12:day=31;break;case 4:case 6:case 9:case 11:day=30;break;case 2:{if(year%400==0||(year%4==0&&year%100!=0)){day=29;}else{day=28;}break;}}return day;
}int printfmonth(int year,int month)
{int i,j,week=getWeekdayByYearday(year, month, 1);printf("%d-%d\n",year,month);printf("\tSun\tMon\tTue\tWed\tThu\tFri\tSat\n");for(i=0;i<=week;i++){printf("\t");}for(i=1,j=week;i<=getmonthday(year,month);i++){if(j==7){printf("\n");printf("\t");j=0;}printf("%d",i);printf("\t");j++;}printf("\n");
}int main(int argc,const char * argv[])
{int chose=0;int ret=0;int year,month;int i;//打印表头printf("*********************Please chose*************************\n");printf("*************1.Print a year's calendar *******************\n");printf("*************2.Print a month's calendar*******************\n");printf("**********************************************************\n");printf("your chose:>");ret=scanf("%d",&chose);if(ret!=1){printf("input chose error\n");return -1;}while((getchar()!='\n'));switch(chose){case one:printf("input(year)>");ret=scanf("%d",&year);if(ret!=1){printf("input error\n");return -1;}while((getchar()!='\n'));if(year<=0){printf("input error\n");return -1;}for(i=1;i<=12;i++){printfmonth(year,i);}break;case two:printf("input(year-month)>");ret=scanf("%d-%d",&year,&month);if(ret!=2){printf("input error\n");return -1;}while((getchar()!='\n'));if(year<=0||month<0||month>12){printf("input error\n");return -1;}printfmonth(year,month);}return 0;
}

用C语言实现万年历的代码及思路(详细教程)相关推荐

  1. Github上传代码菜鸟超详细教程

    最近需要将课设代码上传到Github上,之前只是用来fork别人的代码. 这篇文章写得是windows下的使用方法. 第一步:创建Github新账户 第二步:新建仓库 第三部:填写名称,简介(可选), ...

  2. 数据结构-顺序表-c++语言-模板类实现代码(附详细解释) _清风明月

    #include<iostream> #include<new> #define MAXSIZE 1000 #define REALLOC 10 using namespace ...

  3. Gitee【代码托管】详细教程

    什么是码云(Gitee.com) 码云(Gitee.com)是专为开发者提供的稳定.高效.安全的云端软件开发协作平台. 无论是个人.团队.或是企业,都能够用码云实现代码托管.项目管理.协作开发. 码云 ...

  4. vue中开发多语言(国际化),vue+i18n(详细教程)

    目录 第一步: 第二步: 第三步: 第四步: 最后: 前言:我们有时候会遇到一个项目需要支持多语言,而用直接翻译的插件时常会导致翻译的结果跟自己预想的有所偏差或者结果太长造成页面结构紊乱而这个时候就需 ...

  5. C语言实现万年历(附代码) 小白完成的第一个C语言程序,希望大家多多关注,点赞

    C语言实现万年历 前言:本文章向大家介绍如何使用C语言代码实现万年历使用实例,讲解编写万年历的方法,教你轻松学会写出万年历.这个小程序算是我自己写的第一个比较完整的小程序,算是对大一上学期学习的C语言 ...

  6. C语言实现万年历(附代码)

    C语言实现万年历 具备知识 1.公元1年1月1日是星期日. 2.判断平年与闰年的方法. 闰年:普通年需是4的整数倍,世纪年需是400的整数倍. 平年:除去闰年的全是平年. 3.平年2月有28天,闰年2 ...

  7. c语言万年历代码作业,用c语言编写万年历程序

    用c语言编写万年历程序 <C 程序设计>课程设计报告 2011-2012学年第二学期 设计题目:万年历的设计 指导教师: 李素若 完成时间:2012 年 6月 1日至 2011年 6月 2 ...

  8. 用C语言编写万年历6,C语言编写万年历

    <C语言编写万年历>由会员分享,可在线阅读,更多相关<C语言编写万年历(8页珍藏版)>请在人人文库网上搜索. 1.C语言编写万年历 [要求]:1 程序运行后,首先在屏幕上显示主 ...

  9. 原 C语言实现万年历程序,C语言实现万年历源码

    本文实例为大家分享了C语言实现万年历的具体代码,供大家参考,具体内容如下 主函数所在源码 #include #include #include int GetWeek(int year,int mon ...

  10. C语言实现万年历系统

    码到城攻 C语言实现万年历系统 - 码到城攻C语言实现万年历系统,提供查询时间.更新时间.查询日历.周历等功能,还可以添加备忘录和帮助信息https://www.codecomeon.com/post ...

最新文章

  1. 用男女朋友关系解释计算机常用概念
  2. 机房收费系统总结【4】-报错码
  3. IE 无法点击文本框或输入文字
  4. 九种东西吃多会...
  5. 图论--SCC缩点--Tarjan
  6. ab实验置信度_为什么您的Ab测试需要置信区间
  7. 计算机网络TCPP是一组什么,WWW的全称是什么?WWW中文名称是啥?
  8. 每周分享第 39 期
  9. 论文学习——《Affective Computing:A Review》
  10. 生活随记 - 2020国庆第三天
  11. 数学建模方法——层次分析法(AHP)
  12. 2021年立秋是什么时候?立秋的习俗有哪些?
  13. python:游戏倒计时器
  14. Autoware(Architecture Proposal)
  15. RC电路的频率响应、选频网络特性测试的分析与仿真
  16. obs,直播文字画面模糊处理
  17. Kubernetes如何被应用在华为
  18. Scratch少儿编程案例-植物大战僵尸-趣味角色版
  19. BYOD——自带设备
  20. supervisorctl start报错 project-ops entered FATAL state, too many start retries too quickly

热门文章

  1. 推荐几个很好的资源下载网站
  2. 【VBA】excel多功能数据处理插件
  3. OPENCV+VS+QT,导入生成别人的.pro文件时提示opencv文件找不到,C1083:无法打开包括文件 opencv2/opencv.hpp
  4. 增长量计算n+1原则_何俊-资料分析中的增长量计算
  5. Chirp信号及其生成
  6. 【智能优化算法-蝙蝠算法】基于混合粒子群和蝙蝠算法求解单目标优化问题附matlab代码
  7. 抖音检测注入框架分析
  8. mySQL及可视化界面navicat在window的配置
  9. matlab pn,PN序列生成代码快速参考
  10. 机器学习【系列】之第六章随机森林模型