關於時間的一些小功能的實現,如兩個日期之間的差值,N天之後的日期等
#include<stdio.h>
#include<stdlib.h>
#include <string.h>
typedef struct
{
int year;
int month;
int day;
}date;
int month[12]={31,28,31,30,31,30,31,31,30,31,30,31};
int leap1(int year);
int calculate(date a,date b);
date print();
void distance();
void week();
void later();
int judge(date d1);
void main()
{
int m;
char ch;
do{
printf("------------------MENU------------------\n");
printf("----------------------------------------\n");
printf(" 1.Calculate difference date and date \n");
printf(" 2.Calculate someday is day of week \n");
printf(" 3.Calculate from after some day's day \n");
printf(" 4.Quit \n");
printf("please input the choice :(1~4)\n");
scanf("%d",&m);
system("clear");
switch(m)
{
case 1:
distance();break;
case 2:
week();break;
case 3:
later();break;
case 4:
exit(0);
default:
printf("please input correct number\n");
}
printf("do you want to continue the menu:(Y/N)\n");
fflush(stdin);
scanf("%c",&ch);
system("clear");
}while(ch=='y'&&'Y');
}
int year_to_day(int year_begin, int year_end)
{
int sum = 0;
int number = year_begin;
while(number != year_end)
{
sum = sum + 365 + leap1(year_begin);
number++;
}
return sum;
}
int month_to_day(date now)
{
int i, day = 0;
for (i = 0; i < now.month - 1; ++i)
{
day = day + month[i];
}
return (now.month >= 3) ? day + now.day + leap1(now.year) : day + now.day;
}
int calculate(date begin,date end)
{
return year_to_day(begin.year, end.year) - month_to_day(begin) + month_to_day(end);
}
int leap1(int year)
{
return ((year % 400 == 0) || ((year % 4 == 0)&& (year %100 != 0))) ? 1 : 0;
}
date print()
{
char date1[10];
char year[5], month[3], day[3];
date d1;
memset(date1, 0x0, sizeof(date1));
memset(year, 0x0, sizeof(year));
memset(month, 0x0, sizeof(month));
memset(day, 0x0, sizeof(day));
printf("please input the date,format[20120203]\n");
fflush(stdin);
scanf("%s", date1);
date1[9]= '\0';
memcpy(year, date1, 4);
memcpy(month, date1+4, 2);
memcpy(day, date1+6, 2);
d1.year = atoi(year);
d1.month = atoi(month);
d1.day = atoi(day);
return d1;
}
void distance()
{
date d1,d2;
int sum;
char ch;
do{
d1=print();
d2=print();
printf ("year = %dmonth = %dday = %d\n", d1.year, d1.month, d1.day);
printf ("year = %dmonth = %dday = %d\n", d2.year, d2.month, d2.day);
if(judge(d1)&&judge(d2))
{
if(d1.year<=d2.year)
{
sum=calculate(d1,d2);
}
else
{
sum=calculate(d2,d1);
}
printf("the distance between two dates is %d\n",sum);
}
printf("do you want to continue?Y/N\n");
fflush(stdin);
scanf(" %c",&ch);
system("clear");
}while(ch=='y'||ch=='Y');
}
void week()
{
date d1;
int century = 0;
int year = 0;
int weekday = 0;
int month = 0;
char ch;
do
{
system("clear");
d1=print();
if (judge(d1))
{
if (d1.month < 3)
{
month = d1.month + 12;
d1.year--;
}
else
month = d1.month;
century = d1.year/100;
year = d1.year % 100;
weekday = year + (year/4)+(century/4)-(2*century)+(26*(month + 1)/10) + d1.day - 1;
weekday = (weekday < 0) ? weekday + 7: weekday;
printf ("weekday = %d\n", weekday);
switch(weekday%7)
{
case 0:
printf("Sunday\n");break;
case 1:
printf("Monday\n");break;
case 2:
printf("Tuesday\n");break;
case 3:
printf("Wednesday\n");break;
case 4:
printf("Thursday\n");break;
case 5:
printf("Friday\n");break;
case 6:
printf("Saturday\n");break;
default:
printf ("Error\n");
break;
}
printf("do you want to continue?Y/N\n");
fflush(stdin);
scanf(" %c",&ch);
}
}while(ch=='y'||ch=='Y');
system("clear");
}
date now_to_later(date now, int gap)
{
date d1;
int sum, sum_bak;
int temp;
d1 = now;
sum = gap;
if(judge(d1)&&(sum>0) && (sum < 3649270))
{
while(sum>365)
{
if(d1.month>=3)
{
d1.year++;
sum=sum-365-leap1(d1.year);
}
else
{
sum=sum-365-leap1(d1.year);
d1.year++;
}
}
while(sum > 0)
{
if (d1.month != 2)
{
temp = month[d1.month - 1] -d1.day + 1;
}
else
{
temp = month[d1.month - 1] +leap1(d1.year)- d1.day + 1;
}
sum_bak = sum;
sum = sum - temp;
if (sum >= 0)
{
d1.month++;
d1.day = 1;
if (d1.month > 12)
{
d1.month = 1;
d1.year++;
}
sum_bak = sum;
}
}
d1.day += sum_bak;
}
return d1;
}
date now_to_fronter(date now, int gap)
{
date d1;
int sum, sum_bak;
int temp;
d1 = now;
sum = gap;
if(judge(d1)&&(sum < 0) && (sum > -3649270))
{
while(sum < -365)
{
if(d1.month>=3)
{
sum=sum+365+leap1(d1.year);
d1.year--;
}
else
{
d1.year--;
sum=sum + 365 + leap1(d1.year);
}
}
sum_bak = sum;
while(sum < 0)
{
temp =-d1.day;
sum = sum - temp;
if (sum <= 0)
{
d1.month--;
if (d1.month < 1)
{
d1.month = 12;
d1.year--;
}
if (d1.month == 2)
d1.day = month[d1.month - 1] + leap1(d1.year);
else
d1.day = month[d1.month - 1];
sum_bak = -sum;
}
}
if (sum_bak < 0)
{
d1.day = d1.day + sum_bak;
}else
{
if (d1.month == 2)
d1.day = month[d1.month - 1] + leap1(d1.year) - sum_bak;
else
d1.day = month[d1.month - 1] - sum_bak;
}
}
return d1;
}
void later()
{
date d1;
int sum, sum_bak;
int temp;
char ch;
do
{
d1=print();
printf("please input the number of days[+to future\\-to fronter]:\n");
fflush(stdin);
scanf("%d",&sum);
if (sum > 0)
d1 = now_to_later(d1, sum);
else
d1 = now_to_fronter(d1, sum);
if(judge(d1))
{
printf("the date is flowing:\n");
printf("the year is : %d\n",d1.year);
printf("the month is : %d\n",d1.month);
printf("the day is : %d\n",d1.day);
}
else
{
printf ("errot to calculate\n");
}
printf("do you want to continue?Y/N\n");
fflush(stdin);
scanf(" %c",&ch);
system("clear");
}while(ch=='y'||ch=='Y');
}
int judge(date d1)
{
return ((d1.year > 0 && d1.year <= 9999) &&
(d1.month > 0 && d1.month <= 12) &&
(d1.day > 0 && (
((d1.month == 2) && (d1.day < month[d1.month - 1] + leap1(d1.year)))||
((d1.month != 2) && (d1.day < month[d1.month - 1]))
))) ? 1 : 0;
}