user-0 提问: “计算两个日期差值”代码优化提升

回复:
user: user-1
content: 说一些代码上的问题： 1、date2num函数名字看不出是在干什么？“日期转数字”是什么意思？改为date_distance()或是days_of_two_date()之类的，更明确 2、date2num(DATE Date)，应该用const DATE* 修饰Data，注意函数的实参拷贝和指针参数对性能的影响 3、int a[2][13]，为什么是13列？为什么第一列的数字都是0？ 4、代码没有合理的空格，看着很拥挤 5、一行最好只定义一个变量，而且定义的时候就初始化

user: user-2
content: 仅供参考：#include <afxdisp.h> #include <stdio.h> #include <stdlib.h> int main(int argc, TCHAR* argv[]) {     COleDateTime t;     COleDateTimeSpan ts;     CString s,fmt;     int nYear;     int nMonth;     int nDay;     int nHour;     int nMin;     int nSec;     int lDays;     int nHours;     int nMins;     int nSecs;     int i,N;     // initialize MFC and print and error on failure     if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0)) {         printf("Fatal Error: MFC initialization failed\n");         return 1;     }     if (argc<13) {         printf("Usage:%s sYYYY sMM sDD shh smm sss pDD phh pmm pss n {SQL|YYYY|YY}\n",argv[0]);         return 2;     }     if (stricmp(argv[12],"SQL")==0) fmt="%Y-%m-%d %H:%M:%S";     else if (stricmp(argv[12],"YYYY")==0) fmt="%Y%m%d %H%M%S";     else if (stricmp(argv[12],"YY")==0) fmt="%y%m%d %H%M%S";     else {         printf("Usage:%s sYYYY sMM sDD shh smm sss pDD phh pmm pss n {SQL|YYYY|YY}\n",argv[0]);         return 3;     }     nYear =atoi(argv[ 1]);     nMonth=atoi(argv[ 2]);     nDay  =atoi(argv[ 3]);     nHour =atoi(argv[ 4]);     nMin  =atoi(argv[ 5]);     nSec  =atoi(argv[ 6]);     lDays =atoi(argv[ 7]);     nHours=atoi(argv[ 8]);     nMins =atoi(argv[ 9]);     nSecs =atoi(argv[10]);     N     =atoi(argv[11]);     if (N<=0) {         printf("Usage:%s sYYYY sMM sDD shh smm sss pDD phh pmm pss n {SQL|YYYY|YY}\n",argv[0]);         return 4;     }     t=COleDateTime( nYear, nMonth, nDay, nHour, nMin, nSec);     ts=COleDateTimeSpan( lDays, nHours, nMins, nSecs );     for (i=1;i<=N;i++) {         s=t.Format(fmt);         printf("%08d %s\n",i,s);         t=t+ts;     }     return 0; }

user: user-2
content: The COleDateTime class handles dates from 1 January 100 – 31 December 9999.  COleDateTimeSpan::GetDays long GetDays( ) const;  Return Value  The day portion of this date/time-span value.  Remarks  Call this member function to retrieve the day portion of this date/time-span value.  The return values from this function range between approximately  – 3,615,000 and 3,615,000.  For other functions that query the value of a COleDateTimeSpan object, see the following member functions:   GetHours   GetMinutes   GetSeconds   GetTotalDays   GetTotalHours   GetTotalMinutes   GetTotalSeconds  COleDateTimeSpan Overview |  Class Members |  Hierarchy Chart  See Also   COleDateTimeSpan::SetDateTimeSpan    COleDateTime::operator +, - COleDateTime operator +( const COleDateTimeSpan& dateSpan ) const;  COleDateTime operator -( const COleDateTimeSpan& dateSpan ) const;  COleDateTimeSpan operator -( const COleDateTime& date ) const;  Remarks  COleDateTime objects represent absolute times. COleDateTimeSpan objects represent relative times. The first two operators allow you to add and subtract a COleDateTimeSpan value from a COleDateTime value. The third operator allows you to subtract one COleDateTime value from another to yield a COleDateTimeSpan value.  If either of the operands is null, the status of the resulting COleDateTime value is null.  If the resulting COleDateTime value falls outside the bounds of acceptable values, the status of that COleDateTime value is invalid.  If either of the operands is invalid and the other is not null, the status of the resulting COleDateTime value is invalid.  For more information on the valid, invalid, and null status values, see the m_status member variable.  For more information about the bounds for COleDateTime values, see the articleDate and Time: Automation Support in Visual C++ Programmer’s Guide.  COleDateTime Overview |  Class Members |  Hierarchy Chart  See Also   COleDateTime::operator +=, -=, COleDateTime::GetStatus, COleDateTimeSpan

user: user-0
content: date.txt中存放样例如下： 2016 12 31 2016 08 07

user: user-0
content: #include<stdio.h>	// sscanf( )、fopen( )、fgets( ) #include<stdlib.h>	// exit( )  //日期结构体 typedef struct { 	char year[5]; 	char month[3]; 	char day[3]; }DATE;  //判断是否是闰年 int IsleapYear(int y) { 	if((y%400==0) || (y%100!=0) && (y%4==0)) 		return 1; 	else 		return 0; }  //将日期转化成数字（即从指定的一天算起到所求日期的天数） long date2num(DATE Date) { 	int Iyear,Imonth,Iday; 	long dateNum; 	int i,h=0,count=0; 	int a[2][13]={{0,31,28,31,30,31,30,31,31,30,31,30,31},{0,31,29,31,30,31,30,31,31,30,31,30,31}}; 	 	Iyear=atoi(Date.year); 	Imonth=atoi(Date.month); 	Iday=atoi(Date.day); 	 	//闰年增加的天数 	for(i=0;i<Iyear;i++) 		if (IsleapYear(i)) 			count=count+1; 	 	//最后一年中各个月份的天数 	for(i=0;i<Imonth;i++) 		h=h+a[IsleapYear(Iyear)][i]; 	 	dateNum=(Iyear-1)*365+count+h+Iday; 	return dateNum; }  int main( ) { 	FILE	*Fp; 	char	Str[12]; 	DATE	DateStart, DateEnd; 	long	NumStart,NumEnd; 	int		Interval; 	if((Fp=fopen("date.txt", "r+"))==0) 	{ 		printf("打开date.txt文件失败\n"); 		exit(1); 	} 	 	fgets(Str, sizeof(Str), Fp); 	if((sscanf(Str, "%s%s%s", DateStart.year, DateStart.month, DateStart.day))!=3) 	{ 		printf("信息丢失\n"); 		exit(1); 	} 	 	fgets(Str, sizeof(Str), Fp); 	if((sscanf(Str, "%s%s%s", DateEnd.year, DateEnd.month, DateEnd.day))!=3) 	{ 		printf("信息丢失\n"); 		exit(1); 	} 	 	/* printf("计算%s年%s月%s日起到%s年%s月%s日止间隔的天数\n", DateStart.year, DateStart.month, DateStart.day, DateEnd.year, DateEnd.month, DateEnd.day); 	printf("间隔%d天\n", Interval); */ 	 	NumStart=date2num(DateStart); 	NumEnd=date2num(DateEnd); 	Interval=NumEnd-NumStart; 	fprintf(Fp,"\n%s年%s月%s日起到%s年%s月%s日止间隔的天数%d天。", DateStart.year, DateStart.month, DateStart.day, DateEnd.year, DateEnd.month, DateEnd.day, Interval);  	 	fclose(Fp); }