[此贴子已经被作者于2004-11-24 08:03:06编辑过]
不知道有没有方法可以从函数库简单运算得出,不过这种方法是行的
//两个年月日之间相隔的天数
#include <stdio.h> #include <conio.h>
int leap(int year) { if( (year%4==0&&year%100!=0)||(year%400==0) ) return 1; else return 0; }
int leftdays1(int year,int month,int day) { int i; int days=0; int d[13]={0,31,28,31,30,31,30,31,31,30,31,30,31}; for(i=month+1;i<=12;i++) { if(i==2&&leap(year)==1) days+=29; else days+=d[i]; } days+=(d[month]-day); return days; }
int leftdays2(int year,int month,int day) { int i; int days=0; int d[13]={0,31,28,31,30,31,30,31,31,30,31,30,31}; for(i=1;i<month;i++) { if(month>=3&&i==2&&leap(year)==1) days+=29; else days+=d[i]; } days+=day; return days; }
int main() { int b_year,b_month,b_day; int o_year,o_month,o_day; int totaldays; int i,a,b,c=0,m=0; int d[13]={0,31,28,31,30,31,30,31,31,30,31,30,31}; clrscr(); printf("please input the start date with space\n"); scanf("%d %d %d",&b_year,&b_month,&b_day); printf("please input the end date\n"); scanf("%d %d %d",&o_year,&o_month,&o_day); a=leftdays1(b_year,b_month,b_day); b=leftdays2(o_year,o_month,o_day);
for(i=1;i<(o_year-b_year);i++) c+=365+leap(b_year+i);
if(o_year>b_year) totaldays=a+b+c; else if(o_year==b_year&&o_month>b_month) { for(i=1;i<(o_month-b_month);i++) { if(i==2) m+=d[i]+leap(b_year); else m+=d[i]; } totaldays=m+d[b_month]-b_day+o_day; } else totaldays=o_day-b_day; printf("There are %d days left\n",totaldays); getch(); return 0; }