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

 更新时间:2014年07月05日 11:53:42   投稿:junjie  
这篇文章主要介绍了C语言实现的一个万年历小程序,具有一定的参考价值,做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_days  30
#define May_days   31
#define June_days   30
#define July_days   31
#define August_days  31
#define September_days 30
#define October_days  31
#define November_days  30
#define December_days  31

#define first1month January_days
#define first2month January_days+February_days
#define first3month January_days+February_days+March_days
#define first4month January_days+February_days+March_days+April_days
#define first5month January_days+February_days+March_days+April_days+May_days
#define first6month January_days+February_days+March_days+April_days+May_days+June_days
#define first7month January_days+February_days+March_days+April_days+May_days+June_days \
	     +July_days
#define first8month January_days+February_days+March_days+April_days+May_days+June_days \
	     +July_days+August_days
#define first9month January_days+February_days+March_days+April_days+May_days+June_days \
	     +July_days+August_days+September_days
#define first10month January_days+February_days+March_days+April_days+May_days+June_days \
	     +July_days+August_days+September_days+October_days
#define first11month January_days+February_days+March_days+April_days+May_days+June_days \
	     +July_days+August_days+September_days+October_days+November_days

int known_weekday = Tues;
int known_year = 1901;
int konwn_month = 1;
int known_day = 1;

int day_count(int month)
{	
	switch(month)
	{
		case 1: return 0;break;
		case 2: return first1month;break;
		case 3: return first2month;break;
		case 4: return first3month;break;
		case 5: return first4month;break;
		case 6: return first5month;break;
		case 7: return first6month;break;
		case 8: return first7month;break;
		case 9: return first8month;break;
		case 10: return first9month;break;
		case 11: return first10month;break;
		case 12: return first11month;break;
	}
}


/*char * month_name(int month)
{
	switch(month)
	{
		case 1:
			return "January";
			break;
		case 2:
			return "February";
			break;
		case 3:
			return "March";
			break;
		case 4:
			return "April";
			break;
		case 5:
			return "May";
			break;
		case 6:
			return "June";
			break;
		case 7:
			return "July";
			break;
		case 8:
			return "August";
			break;
		case 9:
			return "September";
			break;
		case 10:
			return "October";
			break;
		case 11:
			return "November";
			break;
		case 12:
			return "December";
			break;
		default:
			break;
	}
}*/

char * month_name(int month)
{
	switch(month)
	{
		case 1:
			return "一月";
			break;
		case 2:
			return "二月";
			break;
		case 3:
			return "三月";
			break;
		case 4:
			return "四月";
			break;
		case 5:
			return "五月";
			break;
		case 6:
			return "六月";
			break;
		case 7:
			return "七月";
			break;
		case 8:
			return "八月";
			break;
		case 9:
			return "九月";
			break;
		case 10:
			return "十月";
			break;
		case 11:
			return "十一月";
			break;
		case 12:
			return "十二月";
			break;
		default:
			break;
	}
}

/*按格式打印某年某月名称*/
void first_line_print(int month, int year)
{
	//printf("%20s %d \n",month_name(month),year);
	printf("%8d年 %s \n",year, month_name(month));
}

/*按格式打印星期名称*/
void week_print()
{
	//printf("%-6s%-6s%-6s%-6s%-6s%-6s%-6s\n","Sun.", "Mon.", "Tues.", "Wed.", "Thur.", "Fri.", "Sat.");
	printf("%-3s%-3s%-3s%-3s%-3s%-3s%-3s\n","日", "一", "二", "三", "四", "五", "六");
}

/*计算该年该月份与已知日子之间的距离天数*/
int date_distance_count(int month, int year)
{
	int leap_year_count = 0;
	int i;
	int distance;

	if (year > known_year)
	{
		for (i=known_year; i<year; i++)
		{
			if(((i%4 == 0) && (i%100 != 0) ) || (i%400 == 0))
			{
				leap_year_count++;		
			}
		}

		if (month > 2)
		{
			if(((year%4 == 0) && (year%100 != 0) ) || (year%400 == 0))
			{
				leap_year_count++;		
			}
		}
	}
	else
	if (year == known_year)
	{
		if (month > 2)
		{
			leap_year_count = 1;
		}
	}
	

	distance = (year - known_year)*365 + leap_year_count + day_count(month);

	return distance;	
}

/*确定该月份第一天是星期几*/
int makesure_firstday_weekday(int month, int year)
{
	int date_distance = 0;
	int weekday;

	date_distance = date_distance_count(month, year);
	weekday = (known_weekday + date_distance)%7;

	return weekday;	
}

/*依次打印出该月份的日子*/
void print_in_turn(int month, int firstday, int year)
{
	int i = 1;
	int weekday;

	switch(firstday)
	{
		case Sun:
			break;
		case Mon:
			printf("%-3s","");
			break;
		case Tues:
			printf("%-6s","");
			break;
		case Wed:
			printf("%-9s","");
			break;
		case Thur:
			printf("%-12s","");
			break;
		case Fri:
			printf("%-15s","");
			break;
		case Sat:
			printf("%-18s","");
			break;
	}

	switch(month)
	{
		case 1:
		case 3:
		case 5:
		case 7:
		case 8:
		case 10:
		case 12:
		{
			for(i=0; i<31; i++)
			{
				weekday = (firstday + i)%7;
				printf("%-3d",i+1);
				
				if(weekday == Sat)
				{
					printf("\n");
				}					
			}
			break;
		}
		case 2:
		{
			if(((year%4 == 0) && (year%100 != 0) ) || (year%400 == 0)) /*闰年*/
			{
				for(i=0; i<29; i++)
				{
					weekday = (firstday + i)%7;
					printf("%-3d",i+1);
					
					if(weekday == Sat)
					{
						printf("\n");
					}					
				}
				break;
			}
			else /*平年*/
			{
				for(i=0; i<28; i++)
				{
					weekday = (firstday + i)%7;
					printf("%-3d",i+1);
					
					if(weekday == Sat)
					{
						printf("\n");
					}					
				}
				break;
			}
		
		}
		case 4:
		case 6:
		case 9:
		case 11:
		{
			for(i=0; i<30; i++)
			{
				weekday = (firstday + i)%7;/*计算该天是星期几*/
				printf("%-3d",i+1);
				
				if(weekday == Sat)
				{
					printf("\n");/*如果是星期六,则换行*/
				}					
			}
			break;
		}			
	}
}

void date_print(int month, int year)
{
	int firstday;

	firstday = makesure_firstday_weekday(month, year);
	print_in_turn(month, firstday, year);
	printf("\n");
}

void main_month(int month, int year)
{
	first_line_print(month, year);
	week_print();
	date_print(month, year);
	printf("\n\n");
}

void main_calendar(int year)
{
	int i;
	for(i=1; i<=12; i++)
	{
		main_month(i, year);
	}
}

int main()
{
	int year;

	printf("请输入年份:year = ");
	scanf("%d",&year);
	printf("\n");

	while(year < 1902)
	{
		printf("请输入大于1901的年份\n");

		printf("请输入年份:year = ");
		scanf("%d",&year);
		printf("\n");
	}
	
	main_calendar(year);
	
	scanf(" ");
	return 0;
}

运行效果图:

相关文章

  • 解决在Mac下直接解压C++静态库出现的问题

    解决在Mac下直接解压C++静态库出现的问题

    最近在研究C++的各种编译构建过程,学习了一下cmake,gyp/ninja这些自动化构建工具后,想着自己试下用纯命令行跑一遍编译流程。在试图把C++静态库编译为动态库的过程中遇到了棘手的问题,找了好久后发现是跟Mac平台相关的,这里记录一下,望对遇到类似问题的童鞋有帮助。
    2016-12-12
  • C语言 程序的编译系统解析

    C语言 程序的编译系统解析

    编译程序的基本功能是把源程序(高级语言)翻译成目标程序。但是,作为一个具有实际应用价值的编译系统,除了基本功能之外,还应具备语法检查、调试措施、修改手段、覆盖处理、目标程序优化、不同语言合用以及人-机联系等重要功能
    2022-02-02
  • 软件构建工具makefile基础讲解

    软件构建工具makefile基础讲解

    这篇文章介绍了软件构建工具makefile,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-12-12
  • 详解C语言内核字符串转换方法

    详解C语言内核字符串转换方法

    在内核开发模式下,初始化字符串也需要调用专用的初始化函数,如下分别初始化ANSI和UNCODE字符串,本文我们就来看看代码是如何实现的
    2022-09-09
  • 解析一个有关sizeof用法的题目--sizeof(i++)

    解析一个有关sizeof用法的题目--sizeof(i++)

    本篇文章是对一个关于sizeof用法的题目进行了详细的分析介绍,需要的朋友参考下
    2013-06-06
  • C语言实现简单计算器功能(1)

    C语言实现简单计算器功能(1)

    这篇文章主要为大家详细介绍了C语言实现简单计算器功能的第一部分,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-02-02
  • C++基础学习之输入输出流详解

    C++基础学习之输入输出流详解

    C++是一种广泛应用的编程语言,其输入和输出是程序所必须的基本操作之一。本文将介绍C++中的输入和输出操作,包括输入输出流、文件输入输出等,希望对读者有所帮助
    2023-04-04
  • 利用Matlab编写简易版连连看小游戏

    利用Matlab编写简易版连连看小游戏

    连连看作为经典的小游戏,一定是很多人的回忆吧。本文将用Matlab实现这一经典的游戏,文中示例代码具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-03-03
  • C++实现截图截屏的示例代码

    C++实现截图截屏的示例代码

    本文主要介绍了C++实现截图截屏的示例代码,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-12-12
  • C++开发:为什么多线程读写shared_ptr要加锁的详细介绍

    C++开发:为什么多线程读写shared_ptr要加锁的详细介绍

    本篇文章介绍了,在C++中为什么多线程读写shared_ptr要加锁的详细说明。需要的朋友参考下
    2013-04-04

最新评论