- // cout_date.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- #include "count_date.h"
- int _tmain(int argc, _TCHAR* argv[])
- {
- int work = 1;
- int count = 0;
- int YearHandle(); //YearHandle() function declaration
- int MonthHandle(); //MonthHandle() function declaration
- int DayHandle(); //DayHandle() function declaration
- while(work)
- {
- s.year =0;
- s.month =0;
- s.day =0;
- count =0;
- system("cls"); //clear the screen.
- system("color 7c"); //set the color of background.
- printf("\\n--------------------------Welcome To The Count System--------------------------\\n");
- putchar('\\n');
- s.year = YearHandle();
- s.month = MonthHandle();
- s.day = DayHandle();
- for(int i=1; i<s.month; ++i)
- {
- count+=yearArray[sign][i]; //count the day.
- }
- count+=s.day;
- printf("The %d/%d/%d is the %dth of this year!\\n", s.year, s.month, s.day, count); //ouput the relut.
- putchar('\\n');
- printf("Whether exit the count system?(1 is not exit,0 is exit)\\n"); //whether exit the system.
- scanf("%d", &work);
- fflush(stdin);
- while (work!=0 && work!=1) //kik must is 1 or 0.
- {
- printf(" Input is error,Please input again!\\n");
- scanf("%d", &work);
- fflush(stdin);
- }
- }
- printf("\\n--------------------Thanks For You Using The Count System !--------------------\\n");
- Sleep(2000); //deley some times.
- return 0;
- }
- /* YearHandle() function to enter the year and check*/
- int YearHandle()
- {
- bool kos = 0;
- bool Check_digit(char *kk);
- printf("Please input the year: ");
- char year[10]={0};
- gets(year);
- putchar('\\n');
- kos = Check_digit(year);
- s.year = atoi(year);
- while((s.year<=1000 || s.year>=9000) || kos) // input the year must between in 1000 to 9000.
- {
- printf("The year is error, Please input again!\\n");
- printf("Please input the year: ");
- gets(year);
- putchar('\\n');
- kos = Check_digit(year);
- s.year = atoi(year);
- }
- return s.year;
- }
- /* MonthHandle() function to enter the month and check*/
- int MonthHandle()
- {
- bool kos = 0;
- bool Check_digit(char *kk);
- printf("Please input the month: ");
- char month[10]={0};
- gets(month);
- putchar('\\n');
- kos = Check_digit(month);
- s.month = atoi(month);
- while(s.month<1 || s.month>12 || kos) //input the month must between in 1 to 12.
- {
- printf("The month is error,Please input again!\\n");
- printf("Please input the month: ");
- gets(month);
- putchar('\\n');
- kos = Check_digit(month);
- s.month = atoi(month);
- }
- return s.month;
- }
- /* DayHandle() function to enter the day and check*/
- int DayHandle()
- {
- bool kos = 0;
- bool Check_digit(char *kk);
- printf("Please input the day: ");
- char day[10]={0};
- gets(day);
- putchar('\\n');
- kos = Check_digit(day);
- s.day = atoi(day);
- sign = ((s.year%4==0) && (s.year%100!=0) || (s.year%400==0)); //judge the leap year.
- while(s.day <1 || s.day>yearArray[sign][s.month] || kos) //the day must in the month.
- {
- printf("The day is error,Please input again!\\n");
- printf("Please input the day: ");
- gets(day);
- putchar('\\n');
- kos = Check_digit(day);
- s.day = atoi(day);
- }
- return s.day;
- }
- /*Check_digit() function to check whether the input is a number*/
- bool Check_digit(char *kk)
- {
- int a = strlen(kk);
- for(int i=0; i<a; ++i)
- {
- if(((int)(*(kk+i)) < 48) || ((int)(*(kk+i)) > 57))
- {
- return 1;
- }
- }
- return 0;
- }
- //该片段来自于http://www.codesnippet.cn/detail/0508201513337.html
来源: http://www.codesnippet.cn/detail/0508201513337.html