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

首页 > 软件编程 > C 语言 更新日期: 2015-12-08
这篇文章主要介绍了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;
}

运行效果图:

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


> 本站内容系网友提交或本网编辑转载,其目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责。如涉及作品内容、版权和其它问题,请及时与本网联系,我们将在第一时间删除内容!

相关文章
  • 一个计算身份证号码校验位的Python小程序
    闲着无事,便想写个实用点的Python小程序,如何计算机身份证号码的校验位,这样的文章在网上一抓一大把,这里仅简单介绍下吧S = Sum(Ai * Wi), i=0,.......16 (现在的身份证号码都是18位长,其中最后一位是校验位,15位的身份证号码好像不用了) Ai对应身份证号码,Wi则为用于加权计算的值,它一串固定的数值,应该是根据某种规则得出的 ...
  • C#一个简单的定时小程序实现代码
    这篇文章主要介绍了C#实现一个简单的定时小程序代码,实现过程很简单,需要的朋友可以参考下之前一直觉得定时程序好神秘,后来,当我自己真正写了一个小的定时程序时,发现其实没有想象中的那么难.下面,我分享一下我自己的操作过程,希望能对大家有帮助. 1)在我们的项目中添加引用文件:TaskSchedulerEngine.dll(dll定义了一个ITask接口,定义了 ...
  • Python实现的一个找零钱的小程序代码分享
    这篇文章主要介绍了Python实现的一个找零钱的小程序代码分享,本文中可以区分出应该找零多少元.多少角,需要的朋友可以参考下Python写的一个按面值找零钱的程序,按照我们正常的思维逻辑从大面值到小面值的找零方法,人民币面值有100元,50元,20元,10元,5元,1元,5角,1角,而程序也相应的设置了这些面值.只需要调用函数时传入您想要找零的金额,程序会自 ...
  • python写的一个squid访问日志分析的小程序
    python写的一个squid访问日志分析的小程序
    这篇文章主要介绍了python写的一个分析squid访问日志的小程序,本文实现的目标是统计access.log中的ip数目,需要的朋友可以参考下这两周组里面几位想学习python,于是我们就创建了一个这样的环境和氛围来给大家学习. 昨天在群里,贴了一个需求,就是统计squid访问日志中ip 访问数和url的访问数并排序,不少同学都大体实现了相应的功能,我把我 ...
  • C语言编写简单的定时关机程序
    本文给大家分享的是一则C语言编写的简单的定时关机程序,可以设置0-600秒倒计时,有需要的小伙伴可以参考下.写一个定时关机的小程序,可以立即关闭计算机,也可以一段时间后关闭计算机. 这里主要考察system()命令. 代码实现: #include<stdio.h> #include<string.h> #include<stdl ...
猜你喜欢