转别人的求解对称日的程序

// CalcPalindrome.cpp 
/********************************************************************
	created:	2011/11/02
	file base:	CalcPalindrome
	file ext:	cpp
	author:		小C
	
	purpose:	通过4位数反转求得8位对称数,
				再进行日期的正确性验证,
				最后打印存在的对称数.
*********************************************************************/
#include <stdio.h>

#define MIN_YEAR 1000
#define MAX_YEAR 9999

#define LEAPYEAR(x) ( (x%4==0&&x%100!=0||x%400==0)?1:0 )

typedef enum {
	USERFALSE = 0,
	USERTRUE = 1
} USERBOOL;

// 计算[nMinYear,nMaxYear]闭区间内的对称日
void CalcPalindromeDates(int nMinYear, int nMaxYear);

// 判断日期是否存在
USERBOOL CheckDate(char szDate[]);

// 打印日期
void PrintDate(int nMinYear, int nMaxYear, int nNum, char szDate[]);

int main(void)
{
	int nMinYear = 0;
	int nMaxYear = 0;
	
	printf("四位数对称日计算器,退出程序直接点右上角关闭按钮\n");

	while(1)
	{	
		fflush(stdin);
		printf("请输入年份区间(1000-9999),如:2001-2022\n");
		if (2 == scanf("%d-%d", &nMinYear, &nMaxYear))
		{		
			if (nMinYear < MIN_YEAR || nMaxYear < MIN_YEAR)
			{
				printf("亲~输入的年份需要在四位数以内哦~\n");
			}
			else if (nMaxYear > MAX_YEAR || nMinYear > MAX_YEAR)
			{
				printf("亲~输入的年份需要在四位数以内哦~\n");
			}
			else if (nMinYear > nMaxYear)
			{
				printf("亲~你的最小值和最大值范围错了哦~\n");
			}
			else
			{
				CalcPalindromeDates(nMinYear, nMaxYear);
				printf("\n");
				continue;
			}
		}
		else
		{
			printf("亲~你的格式错了哦~\n");
		}
	}	

	getchar();
	getchar();

	return 0;
}

void CalcPalindromeDates(int nMinYear, int nMaxYear)
{
	int i = 0;
	int nYear = 0;
	int nNum = 0;
	char szDate[9];
	char chTemp;
	for (nYear = nMinYear; nYear <= nMaxYear; nYear++)
	{
		for (i = 0; i < 4; i++)
		{
			switch(i)
			{
			case 0: chTemp = (nYear/1000+'0'); break;
			case 1: chTemp = ((nYear%1000)/100+'0'); break;
			case 2: chTemp = ((nYear%100)/10+'0');break;
			case 3: chTemp = (nYear%10+'0'); break;
			}
			szDate[i] = szDate[7-i] = chTemp;
		}
		szDate[8] = '\0';

		if (USERTRUE == CheckDate(szDate))
		{
			nNum++;
			PrintDate(nMinYear, nMaxYear, nNum, szDate);
		}
	}

	return ;
}

USERBOOL CheckDate(char szDate[])
{
	int nYear = 0;
	int nMonth = 0;
	int nDay = 0;
	int i = 0;	

	// 此处偷懒
	nYear += (szDate[0]-'0')*1000;
	nYear += (szDate[1]-'0')*100;
	nYear += (szDate[2]-'0')*10;
	nYear += (szDate[3]-'0');

	nMonth += (szDate[4]-'0')*10;
	nMonth += (szDate[5]-'0');

	nDay += (szDate[6]-'0')*10;
	nDay += (szDate[7]-'0');

	if (nMonth > 12 || nMonth < 1)
	{
		return USERFALSE;
	}
	// 1,3,5,7,8,10,12月
	if (nDay < 1 || nDay > 31) 
	{
		return USERFALSE;
	}
	// 2月
	else if (nMonth == 2 && nDay > (28+LEAPYEAR(nYear)))
	{
		return USERFALSE;
	}
	// 4,6,9,11月
	switch (nMonth) 
	{
	case 4:	if (nDay > 30)	return USERFALSE; break;
	case 6:	if (nDay > 30)	return USERFALSE; break;
	case 9: if (nDay > 30)	return USERFALSE; break;
	case 11:if (nDay > 30)	return USERFALSE; break;
	default: break;
	}

	return USERTRUE;
}

void PrintDate(int nMinYear, int nMaxYear, int nNum, char szDate[])
{
	int i = 0;
	for (i = 0; szDate[i] != '\0'; i++)
	{
		putchar(szDate[i]);
		switch (i)
		{
		case 3:
			printf("年");
			break;
		case 5:
			printf("月");
			break;
		case 7:
			printf("日");
			break;
		default:
			break;
		}
	}
	printf("是%d-%d的第%d个对称日\n", nMinYear, nMaxYear, nNum);
}

看别人的程序就是写的整齐,写的健壮。欣赏一下,保存到blog。

PS:两个getchar()是要做什么?

;