关于时间的一些小功能的实现,如两个日期之间的差值,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------------------
");printf("----------------------------------------
");printf("1.Calculate difference date and date
");printf("2.Calculate someday is day of week
");printf("3.Calculate from after some day"s day
");printf("4.Quit
");printf("please input the choice :(1~4)
");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
");}printf("do you want to continue the menu:(Y/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]
");fflush(stdin);scanf("%s", date1);date1[9]= " ";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;intsum;char ch;do{d1=print();d2=print();printf ("year = %dmonth = %dday = %d
", d1.year, d1.month, d1.day);printf ("year = %dmonth = %dday = %d
", 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
",sum);}printf("do you want to continue?Y/N
");fflush(stdin);scanf(" %c",&ch);system("clear");}while(ch=="y"||ch=="Y");}void week(){date d1;intcentury = 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--;}elsemonth = 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
", weekday);switch(weekday%7){case 0:printf("Sunday
");break;case 1:printf("Monday
");break;case 2:printf("Tuesday
");break;case 3:printf("Wednesday
");break;case 4:printf("Thursday
");break;case 5:printf("Friday
");break;case 6:printf("Saturday
");break;default:printf ("Error
");break;}printf("do you want to continue?Y/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);elsed1.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;elsed1.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]:
");fflush(stdin);scanf("%d",&sum);if (sum > 0)d1 = now_to_later(d1, sum);elsed1 = now_to_fronter(d1, sum);if(judge(d1)){printf("the date is flowing:
");printf("theyear is : %d
",d1.year);printf("the month is : %d
",d1.month);printf("the day is : %d
",d1.day);}else{printf ("errot to calculate
");}printf("do you want to continue?Y/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;}