计算输入的日期与1949年10月1日距离多少天
从键盘输入一个日期,格式为yyyy-M-d要求计算该日期与1949年10月1日距离多少天
例如:
用户输入了:1949-10-2
程序输出:1
用户输入了:1949-11-1
程序输出:31
#include <stdio.h> struct date { int year; int month; int day; }; int main (void) { int getDaysOfDate (struct date d); struct date firstDate,secondDate; int firstDays,secondDays; printf ("Enter first date [yyyy mm dd]:"); scanf ("%i %i %i",&firstDate.year,&firstDate.month,&firstDate.day); printf ("Enter second date [yyyy mm dd]:"); scanf ("%i %i %i",&secondDate.year,&secondDate.month,&secondDate.day); firstDays=getDaysOfDate(firstDate); secondDays=getDaysOfDate(secondDate); printf ("Two date between the number days is: %i\n",secondDays-firstDays ); return 0; } int getDaysOfDate (struct date d) { int days; if(d.month<=2) { days=1461*(d.year-1)/4+153*(d.month+13)/5+d.day; }else{ days=1461*d.year/4+153*(d.month+1)/5+d.day; } if(d.year>=1700&&d.month>=3&&d.day>=1 && d.year<=1800&&d.month<=2&&d.day <=28) { days=days+2; } if(d.year>=1800&&d.month>=3&&d.day>=1 && d.year<=1900&&d.month<=2&&d.day <=28) { days=days+1; } return days; }
#include<stdio.h> #include<stdlib.h> #include<conio.h> int day_month[12]={31,28,31,30,31,30,31,31,30,31,30,31}; int IsLeapYear(int year) { return ((year%400==0) || (year%4==0 && year%100 !=0)); } long int ChangToLongInt(int year,int month,int day) { return (year*10000+month*100+day); } int MainMenu() { char seltstr[100]; system("CLS"); printf("\n菜单选项,按键选择!\n\n"); printf("1:给定日期段,计算相隔天数\n\n"); printf("2:给定初始日期与相隔天数,计算结束日期\n\n"); printf("按其他键退出程序!\n\n"); printf("请选择:"); scanf("%s",&seltstr); if(seltstr[1]=='\0' && (seltstr[0]=='1' || seltstr[0]=='2')) return seltstr[0]-'0'; else return 0; } long int fun(int *year1,int *month1,int *day1) { (*day1)++; if((*month1)==2) { if(IsLeapYear(*year1)) { if(*day1-1==day_month[*month1-1]) { *day1=1; (*month1)++; } } else { if(*day1-1==day_month[*month1-1]+1) { *day1=1; (*month1)++; } } } else { if(*day1-1==day_month[*month1-1]) { *day1=1; if(*month1<12)(*month1)++; else {*month1=1;(*year1)++;} } } return ChangToLongInt(*year1,*month1,*day1); } int main() { long int Date1,Date2,cont,num; int tmp; int year1,year2,month1,month2,day1,day2; star_main: tmp=MainMenu(); cont=1; if(tmp==1) { system("CLS"); printf("\n请按年、月、日顺序输入开始日期,以空格分开,回车符结束:"); scanf("%d%d%d",&year1,&month1,&day1); printf("\n请按年、月、日顺序输入结尾日期,以空格分开,回车符结束:"); scanf("%d%d%d",&year2,&month2,&day2); Date1=ChangToLongInt(year1,month1,day1); Date2=ChangToLongInt(year2,month2,day2); for(;Date1<Date2;) { cont++; Date1=fun(&year1,&month1,&day1); } printf("\n总共相隔 %ld 天\n",cont); printf("\n按任意键返回主菜单!\n\n"); getch(); goto star_main; } else if(tmp==2) { system("CLS"); printf("\n请按年、月、日顺序输入开始日期,以空格分开,回车符结束:"); scanf("%d%d%d",&year1,&month1,&day1); printf("\n请输入相隔的天数:"); scanf("%ld",&num); cont=1; Date1=ChangToLongInt(year1,month1,day1); while(cont<num) { cont++; Date1=fun(&year1,&month1,&day1); } printf("\n结束日期是 %ld 年 %ld 月 %ld日\n\n",Date1/10000,Date1%10000/100,Date1%100); printf("\n按任意键返回主菜单!\n\n"); getch(); goto star_main; } else return 0; return 0; }