栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C语言实现万年历源码

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C语言实现万年历源码

本文实例为大家分享了C语言实现万年历的具体代码,供大家参考,具体内容如下

主函数所在源码

#include 
#include 
#include 

int GetWeek(int year,int month,int day);//求今天是周几。周四就返回 4 。周日 返回 0;非法返回 -1; 
int GetDaysInMonth(int year,int month);
int CreateMonthData(int MonthDay[6][7],int year,int month);
void PrintMonth(int MonthDay[6][7]);

int main()
{
 int MDate[6][7] = {{0}};
 
 int y = 0;
 int m = 0;
 int ret = 0;
 
 printf("Plear input year month:n");
 scanf("%d%d",&y,&m);

 if(m <= 0 || m > 12)
 {
 printf("Your month is invalidn");
 return 1;
 }
 
 ret = CreateMonthData(MDate,y,m);
 if(ret == 0)
 {
 PrintMonth(MDate);
 }
 return 0;
} 

int CreateMonthData(int MonthDay[6][7],int year,int month)
{
 int week = GetWeek(year,month,1);//返回第几周。 
 int day = 1;
 int i = 0;
 int j = 0;
 int daysInMonth = GetDaysInMonth(year,month);//当月天数。 

 if(week < 0)
 {
 printf("GetWeek Failedn");
 return -1;
 }
 
  
 for(j = 0;j < 7;j++)
 {
 if(j < week)
 {
 MonthDay[0][j] = 0;
 }
 else
 {
 MonthDay[0][j] = day;
 day++;
 }
 }

 
 for(i = 1;i < 6;i++)
 {
 for(j = 0;j < 7;j++)
 {
 if(day > daysInMonth)
 {
 MonthDay[i][j] = 0;
 }
 else
 {
 MonthDay[i][j] = day;
 day++;
 }
 }
 }

 return 0;
} 



void PrintMonth(int MonthDay[6][7])
{
 int i=0;
 int j=0;
 printf(" 日 一 二 三 四 五 六n");
 for(i=0;i<6;i++)
 {
 for(j=0;j<7;j++)
 {
 printf("%2d ",MonthDay[i][j]);
 }
 printf("n");
 } 
}

第二个文件源码

#include 
#include 
#include 

//润年 
int LeapDays[12] = {31,29,31,30,31,30,31,31,30,31,30,31};
//非闰年 
int CommonDays[12] = {31,28,31,30,31,30,31,31,30,31,30,31};

//闰年返回 1 ,否则返回 0; 
int IsLeapYear(int year)
{
 if((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
 {
 return 1;
 }
 else
 {
 return 0;
 }
}

// 输入非法 返回 0;否则返回 1; 
int IsValidDate(int year,int month,int day)
{
 int ret = 1;
 if(month < 1 || month > 12 || day < 1 || year <= 0)
 {
  return 0;
 }

 if(IsLeapYear(year))
 {
  if(day > LeapDays[month - 1])
  {
   ret = 0;
  }
 }
 else
 {
  if(day > CommonDays[month - 1])
  {
   ret = 0;
  }
 }

 return ret;
}


int GetDaysInYear(int year,int month,int day)
{
 int sum=0;//总天数 
 int isrun=IsLeapYear(year);//闰年返回 1 ;否则返回 0;
 int i=0;
 int j=0;
 if(isrun)
 {
 for(i=0;i

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/62239.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号