现在的位置: 首页 > 综合 > 正文

万年历 C语言

2018年05月04日 ⁄ 综合 ⁄ 共 2485字 ⁄ 字号 评论关闭
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <ctype.h>

/*
实现万年历功能 
核心公式:蔡勒公式 
*/

/*
判断是否为闰年
是返回 1
否返回0 
*/ 
int isLeapYear(int year) {
	if( ( year % 4 == 0 && year % 100 != 0 ) || ( year % 400 == 0) ) 
		return 1;
	else
		return 0; 
}

/*
根据年月计算当月的天数
返回天数 
*/
int calculateMonthDays(int month, int year) {
	int monthDays;

	if(month == 2) {
		if(isLeapYear(year)) 
			monthDays = 29;
		else 
			monthDays = 28; 
	} else {
		if(month <= 7) {
			if(month % 2 == 0) 
				monthDays = 30;
			else 
				monthDays = 31;
		} else {
			if(month % 2 == 0)
				monthDays = 31;
			else 
				monthDays = 30; 		
		}
	}
	return monthDays;
}

int main(int argc, char *argv[])
{
	int year, month, monthDays, w, y, c, i, m, tempY, tempC;
	char select, isContinue;
	do {
		printf("**********Calendar************\n");
		printf("1.Printf a Month:\n");
		printf("2.Printf a Year:\n");
		printf("3.exit!\n");
		printf("******************************\n");
		select = getch();
		if(select < '1' || select > '3') {
			system("cls");
			continue;	
		}
		switch(select) {
			case '1' :
				do {
					printf("Please input the Year and Month:\n");
					printf("Year >= 1583, 1 <= Month <=12\n");
					scanf("%d%d", &year, &month);
					if(year < 1583 || month < 1 || month > 12) {
						printf("Year or Month out of range!\n");
						printf("Press any key to exit!\n");
						getch();
						system("cls");
						break;
					}
					c = year/100;
					y = (year % 10) + 10*((year/10)%10);
					monthDays = calculateMonthDays(month, year);
					if(month == 1 || month == 2) {
						if(y != 0) {
							month += 12;
							y -= 1;
						} else {
							month += 12;
							y = 99;
							c -= 1;
						}
					}
					w = y + y/4 + c/4 - 2*c + (26*(month+1)/10);	//蔡勒公式  由年月日计算星期几 
					if(w < 0) 
						w = w%7 + 7;
					w = w%7;
					printf(" SUN MON TUE WED THU FRI SAT\n");
					for(i = 0; i < w; i++) 
						printf("    ");
					for(i = 1; i <= monthDays; i++) {
						printf("%4d", i);	
						w++;
						if(w % 7 == 0) {
							printf("\n");
							w = 0;	
						}
					}
					printf("\n");
					printf("is Continue?(Y/N)");
					isContinue = getch();
					isContinue = tolower(isContinue);
					printf("\n\n");
					if(isContinue != 'y') {
						system("cls");
						break;	
					}
				} while(isContinue == 'y');
			break;
			
			case '2' :
				do {
					printf("Please input the Year:\n");
					printf("Year >= 1583\n");
					scanf("%d", &year);
					if(year < 1583) {
						printf("Year out of range!\n");
						printf("Press any key to exit!\n");
						getch();
						system("cls");
						break;
					}
					c = year/100;
					y = (year % 10) + 10*((year/10)%10);
					tempC = c;
					tempY = y;
					for(m = 1; m <= 12; m++, c=tempC, y=tempY) {
						month = m;
						monthDays = calculateMonthDays(month, year);
						if(month == 1 || month == 2) {
							if(y != 0) {
								month += 12;
								y -= 1;
							} else {
								month += 12;
								y = 99;
								c -= 1;
							}
						}
						w = y + y/4 + c/4 - 2*c + (26*(month+1)/10);	//蔡勒公式  由年月日计算星期几 
						if(w < 0) 
							w = w%7 + 7;
						w = w%7;
						printf(" %d Month\n", m);
						printf(" SUN MON TUE WED THU FRI SAT\n");
						for(i = 0; i < w; i++) 
							printf("    ");
						for(i = 1; i <= monthDays; i++) {
							printf("%4d", i);	
							w++;
							if(w % 7 == 0) {
								printf("\n");
								w = 0;	
							}
						}
						printf("\n\n");
					}
					printf("is Continue?(Y/N)");
					isContinue = getch();
					isContinue = tolower(isContinue);
					printf("\n\n");
					if(isContinue != 'y') {
						system("cls");
						break;	
					}				
				} while(isContinue == 'y');
			break;
		}
	} while(select != '3');
	
	printf("Byb-Byb\n");
	printf("Press Any Key to Exit!\n");
	getch();
	return 0;
}

【上篇】
【下篇】

抱歉!评论已关闭.