|
![]() | 作者: sniper167 [sniper167]
![]() |
登录 |
计算从X1年X1月X1日到X2年X2月X2日的程序,给点意见 谢谢!!! int time (int year,int month,int date) {int m,n,i,leap,day; int big[7]={1,3,5,7,8,10,12}; int small[5]={2,4,6,9,11}; m=0; n=0; for (i=0;i<=6;i++) if (month>big[i]) m++; for (i=0;i<=4;i++) if (month>small[i]) n++; leap=embolism (year); if (leap) if (n<=1) day=m*31+n*29+date; else day=m*31+(n-1)*30+29+date; else if (n<=1) day=m*31+n*28+date; else day=m*31+(n-1)*30+28+date; return (day); } int embolism (int year) {int leap; if ((year%4==0&&year%100!=0)||(year%400==0)) leap=1; else leap=0; return (leap); } main () {int yearbegin,monthbegin,datebegin,yearend,monthend,dateend,i,days,days1,days2,days3,leap; days=days1=days2=days3=0; printf ("Please input the begin year,month,date:"); scanf ("%d.%d.%d",&yearbegin,&monthbegin,&datebegin); printf ("Please input the end year,month,date:"); scanf ("%d.%d.%d",&yearend,&monthend,&dateend); if (yearbegin!=yearend) {if (embolism (yearbegin)) days1=(366-time (yearbegin,monthbegin,datebegin)); else days1=(365-time (yearbegin,monthbegin,datebegin)); if (embolism (yearend)) days3=time (yearend,monthend,dateend); else days3=time (yearend,monthend,dateend); for (i=(yearbegin+1);i<yearend;i++) { if (embolism (i)) days2+=366; else days2+=365; } days=days1+days2+days3; } else days=time (yearend,monthend,dateend)-time (yearbegin,monthbegin,datebegin); printf ("From %d.%d.%d to %d.%d.%d have %d days.\n",yearbegin,monthbegin,datebegin,yearend,monthend,dateend,days); } |
地主 发表时间: 04-03-29 22:55 |
![]() | 回复: sniper167 [sniper167] ![]() |
登录 |
兄弟你。。。 咋把上午那帖删了,我上午写申请,还没来得及看呀 |
B1层 发表时间: 04-03-30 22:10 |
![]() | 回复: sniper167 [sniper167] ![]() |
登录 |
兄弟,你咋又删了? 不过这次我有copy了!!! ![]() ![]() ![]() |
B2层 发表时间: 04-03-30 22:37 |
![]() | 回复: vloadpe [vloadpe] ![]() |
登录 |
改一哈, #include "stdio.h" #include "conio.h" main() { long year0,year1; int month0,month1,date0,date1,embo=0,i, md0[12]={365,334,306,275,245,214,184,153,122, 92, 61, 31}, md1[12]={0, 31, 59, 90,120,151,181,212,243,273,304,334}; printf ("Please input the start year,month,date: "); scanf ("%ld.%d.%d",&year0,&month0,&date0); printf ("Please input the end year,month,date: "); scanf ("%ld.%d.%d",&year1,&month1,&date1); {for(i=year0+1;i<year1;i++) if((i%4==0&&i%100!=0)||i%400==0)++embo;} if(((year0%4==0&&year0%100!=0)||year0%400==0)&&month0<=2)embo=embo+1; if(((year1%4==0&&year1%100!=0)||year1%400==0)&&month1>2&&year0!=year1)embo=embo+1; printf ("%ld\n",(year1-year0-1)*365+md0[month0-1]-date0 +md1[month1-1]+date1+embo); getch(); } |
B3层 发表时间: 04-03-30 23:55 |
![]() | 回复: 286 [unique] ![]() |
登录 |
楼上两位兄弟真是辛苦了,作为一次练习你们的程序的确不错,但实际上要实现你们的功能,只需要一个命令就可以了,在C中有一个函数: double difftime(time_t time2,time_t time1) 计算结构time2和time1之间的时间差距(以秒为单位) 这个函数可以计算两个日期之间的秒数。如果想算天数,把这个值再除了(60 X 60 X 24)就可以了。 |
B4层 发表时间: 04-03-31 09:34 |
![]() | 回复: sniper167 [sniper167] ![]() |
登录 |
![]() ![]() ![]() 高手就是不一样,能把复杂的很快变简单。 佩服!!! |
B5层 发表时间: 04-03-31 23:05 |
|
20CN网络安全小组版权所有
Copyright © 2000-2010 20CN Security Group. All Rights Reserved.
论坛程序编写:NetDemon
粤ICP备05087286号