C语言实现万年历源码

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

主函数所在源码

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

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 invalid\n");
 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 Failed\n");
 return -1;
 }

 /*给第0行赋值*/
 for(j = 0;j < 7;j++)
 {
 if(j < week)
 {
 MonthDay[0][j] = 0;
 }
 else
 {
 MonthDay[0][j] = day;
 day++;
 }
 }

 /*给第1~5赋值*/
 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;
} 

/*将二维数组中本月日期按如下形式显示:以2017年1月为例*/
/*
 日 一 二 三 四 五 六
 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 0 0 0 0
 0 0 0 0 0 0 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 <stdio.h>
#include <stdlib.h>
#include <string.h>

//润年
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<month-1;i++)
 {
 sum=sum+LeapDays[i];
 }
 }
 else
 {
 for(i=0;i<month-1;i++)
 {
 sum=sum+CommonDays[i];
 }
 }
 sum=sum+day;
 return sum;
}

/*求指定月总共多少天*/
int GetDaysInMonth(int year,int month)
{
 int isrun=IsLeapYear(year);//闰年返回 1 ;否则返回 0;
 if(isrun)
 {
 return LeapDays[month-1];
 }
 else
 {
 return CommonDays[month-1];
 }
}

//求今天是周几。周四就返回 4 。周日 返回 0;非法返回 -1;
int GetWeek(int year,int month,int day)
{
 int sum = 0;
 if(0 == IsValidDate(year,month,day))
 {
 printf("Input date is invalid\n");
 return -1;
 }

 sum = year -1;
 sum = sum + sum / 4 - sum / 100 + sum / 400;
 sum = sum + GetDaysInYear(year,month,day);

 return sum % 7;
}

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

(0)

相关推荐

  • C语言实现万年历

    C语言实现的万年历显示,按下上下左右按键来更换日期和年份,供大家参考,具体内容如下 #include <stdio.h> #include <stdlib.h> #include <time.h> #include<conio.h> typedef struct today { int day; int month; int year; } today; int day_cankao[2][13]={ {0,31,28,31,30,31,30,31,31,3

  • C语言实现的一个万年历小程序

    该程序简单地输入一个年份(1901年之后的年份),随后程序输出该年份十二个月的日历. #include<stdio.h> #define Mon 1 #define Tues 2 #define Wed 3 #define Thur 4 #define Fri 5 #define Sat 6 #define Sun 0 #define January_days 31 #define February_days 28 #define March_days 31 #define April_day

  • C语言实现万年历小程序

    一.杂谈 大一学了C之后一直困惑,C到底怎么用?它不像HTML那么直观,也没有SQL那么常用,更没有Java那么功能强大,那他为何还存在,并依然火热呢? 答案很简单:编程语言是一家,C语言结构简单,但所蕴含的逻辑思维和其他语言大致相同,适合初学者. 编程不是一蹴而就,能力需要日积月累,推荐想我一样的初学者,大家自己动手玩玩简单的C程序! 二.万年历 像这样一个简单的全年日历,仔细观察不难发现由每个月的日历组成,每个月有最多6行,占6周,每周7天: 程序如下: 设定日历大小: int date[1

  • C语言实现万年历程序

    本文实例为大家分享了C语言实现万年历程序,供大家参考,具体内容如下 #include <stdio.h> int year(int y) { if ((y%4==0) && (y%100!=0) || y%400==0) return 366; else return 365; } int main() { int y; int i,j,sum=0; int begin,week; int days[12]={31,28,31,30,31,30,31,31,30,31,30,3

  • C语言实现万年历源码

    本文实例为大家分享了C语言实现万年历的具体代码,供大家参考,具体内容如下 主函数所在源码 #include <stdio.h> #include <stdlib.h> #include <string.h> int GetWeek(int year,int month,int day);//求今天是周几.周四就返回 4 .周日 返回 0:非法返回 -1: int GetDaysInMonth(int year,int month);/*求指定月总共多少天*/ int C

  • C语言学生管理系统源码分享

    本文实例为大家分享了C语言学生管理系统源码,供大家参考,具体内容如下 #include<stdio.h> #include<stdlib.h> //结构体可以存放的学生信息最大个数,不可变变量 int const MAX_LENGTH=100; //学生信息结构体数组,最多可以存放100个学生信息 struct student{ int id; //学号 char *name; //姓名 int age; //年龄 float c_score; //C语言成绩 float engl

  • 邻接表无向图的Java语言实现完整源码

    邻接表无向图的介绍 邻接表无向图是指通过邻接表表示的无向图. 上面的图G1包含了"A,B,C,D,E,F,G"共7个顶点,而且包含了"(A,C),(A,D),(A,F),(B,C),(C,D),(E,G),(F,G)"共7条边. 上图右边的矩阵是G1在内存中的邻接表示意图.每一个顶点都包含一条链表,该链表记录了"该顶点的邻接点的序号".例如,第2个顶点(顶点C)包含的链表所包含的节点的数据分别是"0,1,3":而这"

  • Go语言io pipe源码分析详情

    目录 1.结构分析 2.pipe sruct分析 3.PipeReader对外暴露的是读/关闭 4.写法 5.总结 pipe.go分析: 这个文件使用到了errors包,也是用到了sync库. 文件说明:pipe是一个适配器,用于连接Reader和Writer. 1.结构分析 对外暴露的是一个构造函数和构造的两个对象. 两个对象分别暴露了方法,同时这两个对象还有一个共同的底层对象. 实际上,这两个对象暴露的方法是直接调用底层对象的, 那么核心还是在底层对象上,只是通过两个对象和一个构造方法将底层

  • Go语言context test源码分析详情

    目录 1.测试例子分析 2.单元测试 1.测试例子分析 example_test.go,展示了With-系列的4个例子 func ExampleWithCancel() {   gen := func(ctx context.Context) <-chan int {     dst := make(chan int)     n := 1     go func() {       for {         select {         case <-ctx.Done():      

  • C语言 MD5的源码实例详解

    C语言 MD5源码 md5c.h: /* POINTER defines a generic pointer type */ typedef unsigned char * POINTER; /* UINT2 defines a two byte word */ //typedef unsigned short int UINT2; /* UINT4 defines a four byte word */ typedef unsigned long int UINT4; /* MD5 conte

  • C语言UDP传输系统源码

    本文实例为大家分享了C语言UDP传输系统的具体代码,供大家参考,具体内容如下 /*加载库文件*/ #pragma comment( lib, "ws2_32.lib" ) /*加载头文件*/ #include <winsock2.h> #include <ws2tcpip.h> #include <stdio.h> #include <stdlib.h> /*定义多播常量*/ #define MCASTADDR "224.3.5

  • 纯C语言:折半查找源码分享

    复制代码 代码如下: #include <stdio.h>       int bin_search(int key[],int low, int high,int k)      {        int mid;        if(low>high)    {       return -1;        }    else       {             mid = (low+high) / 2;             if(key[mid]==k)         

  • 纯C语言:分治快速排序源码分享

    复制代码 代码如下: #include<stdio.h>void fun(int array[],int low,int high){    int i = low;    int j = high;      int temp = array[i];              while(i < j)     {  while((array[j] >= temp) && (i < j))  {    j--;    array[i] = array[j]; 

  • 模块一 GO语言基础知识-库源码文件

    你已经使用过 Go 语言编写了小命令(或者说微型程序)吗? 当你在编写"Hello, world"的时候,一个源码文件就足够了,虽然这种小玩意儿没什么用,最多能给你一点点莫名的成就感.如果你对这一点点并不满足,别着急,跟着学,我肯定你也可以写出很厉害的程序. 我们在上一篇的文章中学到了命令源码文件的相关知识,那么除了命令源码文件,你还能用 Go 语言编写库源码文件.那么什么是库源码文件呢? 在我的定义中,库源码文件是不能被直接运行的源码文件,它仅用于存放程序实体,这些程序实体可以被其他

随机推荐