user-0 提问: c++算法

回复:
user: user-0
content: 这样子还有没有什么漏掉的？ Date& Date::operator+=(const Date&other)  {  	int k=other.day;  	int cun; 	if(other.day>0){cun=other.day;}  	else cun=-other.day; 	if(k>0){ 	 while(cun>0) 	 { 	 	switch(month) 	 	{ 	 		case 1:if(day+cun<=31){day=day+cun;cun=0;break;}else{cun=cun-(31-day)-1;month++;day=1;} 			case 2:if((year%4==0&&year%100!=0)||(year%400==0)){if(day+cun<=29){day=day+cun;cun=0;break;}else{cun=cun-(29-day)-1;month++;day=1;}} 			else{if(day+cun<=28){day=day+cun;cun=0;break;}else{cun=cun-(29-day)-1;month++;day=1;}} 			case 3:if(day+cun<=31){day=day+cun;cun=0;break;}else{cun=cun-(31-day)-1;month++;day=1;} 			case 4:if(day+cun<=30){day=day+cun;cun=0;break;}else{cun=cun-(30-day)-1;month++;day=1;} 			case 5:if(day+cun<=31){day=day+cun;cun=0;break;}else{cun=cun-(31-day)-1;month++;day=1;} 		        case 6:if(day+cun<=30){day=day+cun;cun=0;break;}else{cun=cun-(30-day)-1;month++;day=1;} 		        case 7:if(day+cun<=31){day=day+cun;cun=0;break;}else{cun=cun-(31-day)-1;month++;day=1;} 		       case 8:if(day+cun<=31){day=day+cun;cun=0;break;}else{cun=cun-(31-day)-1;month++;day=1;} 		       case 9:if(day+cun<=30){day=day+cun;cun=0;break;}else{cun=cun-(30-day)-1;month++;day=1;} 		       case 10:if(day+cun<=31){day=day+cun;cun=0;break;}else{cun=cun-(31-day)-1;month++;day=1;} 		       case 11:if(day+cun<=30){day=day+cun;cun=0;break;}else{cun=cun-(30-day)-1;month++;day=1;} 		       case 12:if(day+cun<=31){day=day+cun;cun=0;break;}else{cun=cun-(31-day)-1;year++;month=1;day=1;} 		 } 	  } } else{ 		while(cun>0) 	{ 		switch(month) 		{ 			case 1:if(day-cun>=1){day=day-cun;cun=0;break;}else{cun=cun-day;year--;month=12;day=31;} 			case 2:if(day-cun>=1){day=day-cun;cun=0;break;}else{cun=cun-day;month--;day=31;} 			case 3:if((year%4==0&&year%100!=0)||(year%400==0)){ 				if(day-cun>=1){day=day-cun;cun=0;break;} 				else{cun=cun-day;month--;day=29;}  			} 			else{ 				if(day-cun>=1){day=day-cun;cun=0;break;} 				else{cun=cun-day;month--;day=28;} 			} 			case 4:if(day-cun>=1){day=day-cun;cun=0;break;}else{cun=cun-day;month--;day=31;} 			case 5:if(day-cun>=1){day=day-cun;cun=0;break;}else{cun=cun-day;month--;day=30;} 			case 6:if(day-cun>=1){day=day-cun;cun=0;break;}else{cun=cun-day;month--;day=31;} 			case 7:if(day-cun>=1){day=day-cun;cun=0;break;}else{cun=cun-day;month--;day=30;} 			case 8:if(day-cun>=1){day=day-cun;cun=0;break;}else{cun=cun-day;month--;day=31;} 			case 9:if(day-cun>=1){day=day-cun;cun=0;break;}else{cun=cun-day;month--;day=31;} 			case 10:if(day-cun>=1){day=day-cun;cun=0;break;}else{cun=cun-day;month--;day=30;} 			case 11:if(day-cun>=1){day=day-cun;cun=0;break;}else{cun=cun-day;month--;day=31;} 		       case 12:if(day-cun>=1){day=day-cun;cun=0;break;}else{cun=cun-day;month--;day=30;} 		} 	} } 	  return *this;   }

user: user-1
content: 楼主多测试几个例子呗，看看有问题没

user: user-2
content: Date operator+(Date a,int b) {     Date c;c.day=a.day;c.year=a.year;c.month=a.month;     int cun;//要操作的日期总数     if(b>0) {         cun=b;     }//判断是加日期还是减日期，b>0,加日期 ，b<0,减日期     else cun=-b;//强制使得cun是正数     if(b>=0) {//若b>0,加b天         while(cun>0) {//当操作天叔大于0时继续操作             switch(c.month) {//对月份进行判断             //一月份如果天数加上cun还是小于31天，天数直接加上cun并且使存=0；跳出循环；如果加上cun大于了31天，月份+1，天数=1；cun=cun-(31-c.day)-1             //二月份             //如果为闰年，如果天数加上cun还是小于29，天数直接加上cun，cun=0，跳出循环；否则月份+1，天数变为1号，cun=cun-(29-c.day)-1             //如果为平年，如果天数加上cun还是小于28，天数直接加上cun，cun=0，跳出循环；否则月份+1，天数变为1号，cun=cun-(28-c.day)-1             //三到十一月份 如果天数加上cun还是小于31天，天数直接加上cun并且使存=0；跳出循环；如果加上cun大于了31天，月份+1，天数=1；cun=cun-(31-c.day)-1             //十二月份 如果天数加上cun还是小于31天，天数直接加上cun并且使存=0；跳出循环；如果加上cun大于了31天，年份+1，月份+1，天数=1；cun=cun-(31-c.day)-1             case  1:if(c.day+cun<=31) {c.day=c.day+cun;cun=0;break;} else {c.month++;c.day=1;cun=cun-(31-c.day)-1;}             case  2:if((c.year%4==0&&c.year%100!=0)||(c.year%400==0)) {                     if(c.day+cun<=29) {c.day=c.day+cun;cun=0;break;} else {c.month++;c.day=1;cun=cun-(29-c.day)-1;}                     } else {                     if(c.day+cun<=28) {c.day=c.day+cun;cun=0;break;} else {c.month++;c.day=1;cun=cun-(29-c.day)-1;}                     }             case  3:if(c.day+cun<=31) {c.day=c.day+cun;cun=0;break;} else {c.month++;c.day=1;cun=cun-(31-c.day)-1;}             case  4:if(c.day+cun<=30) {c.day=c.day+cun;cun=0;break;} else {c.month++;c.day=1;cun=cun-(30-c.day)-1;}             case  5:if(c.day+cun<=31) {c.day=c.day+cun;cun=0;break;} else {c.month++;c.day=1;cun=cun-(31-c.day)-1;}             case  6:if(c.day+cun<=30) {c.day=c.day+cun;cun=0;break;} else {c.month++;c.day=1;cun=cun-(30-c.day)-1;}             case  7:if(c.day+cun<=31) {c.day=c.day+cun;cun=0;break;} else {c.month++;c.day=1;cun=cun-(31-c.day)-1;}             case  8:if(c.day+cun<=31) {c.day=c.day+cun;cun=0;break;} else {c.month++;c.day=1;cun=cun-(31-c.day)-1;}             case  9:if(c.day+cun<=30) {c.day=c.day+cun;cun=0;break;} else {c.month++;c.day=1;cun=cun-(30-c.day)-1;}             case 10:if(c.day+cun<=31) {c.day=c.day+cun;cun=0;break;} else {c.month++;c.day=1;cun=cun-(31-c.day)-1;}             case 11:if(c.day+cun<=30) {c.day=c.day+cun;cun=0;break;} else {c.month++;c.day=1;cun=cun-(30-c.day)-1;}             case 12:if(c.day+cun<=31) {c.day=c.day+cun;cun=0;break;} else {c.year++;c.month=1;c.day=1;cun=cun-(31-c.day)-1;}             }         }     } else {//若b<0,要减b天数         while(cun>0) {             switch(c.month) {             //一月份如果天数减去cun还是大于1号，天数直接减去cun并且使存=0；跳出循环；如果减去cun小于了1号，年份-1，月份=12，天数=31；cun=cun-c.day;             //如果天数减去cun还是大于1号，天数直接减去cun并且使存=0；跳出循环；如果减去cun小于了1号，月份-1，天数=28/29/30/31；cun=cun-c.day;             case  1:if(c.day-cun>=1) {c.day=c.day-cun;cun=0;break;} else {c.year--;c.month=12;c.day=31;cun=cun-c.day;}             case  2:if(c.day-cun>=1) {c.day=c.day-cun;cun=0;break;} else {c.month--;c.day=31;cun=cun-c.day;}             case  3:if((c.year%4==0&&c.year%100!=0)||(c.year%400==0)) {//闰年                     if(c.day-cun>=1) {c.day=c.day-cun;cun=0;break;} else {c.month--;c.day=29;cun=cun-c.day;}                     } else {//平年                     if(c.day-cun>=1) {c.day=c.day-cun;cun=0;break;} else {c.month--;c.day=28;cun=cun-c.day;}                     }             case  4:if(c.day-cun>=1) {c.day=c.day-cun;cun=0;break;} else {c.month--;c.day=31;cun=cun-c.day;}             case  5:if(c.day-cun>=1) {c.day=c.day-cun;cun=0;break;} else {c.month--;c.day=30;cun=cun-c.day;}             case  6:if(c.day-cun>=1) {c.day=c.day-cun;cun=0;break;} else {c.month--;c.day=31;cun=cun-c.day;}             case  7:if(c.day-cun>=1) {c.day=c.day-cun;cun=0;break;} else {c.month--;c.day=30;cun=cun-c.day;}             case  8:if(c.day-cun>=1) {c.day=c.day-cun;cun=0;break;} else {c.month--;c.day=31;cun=cun-c.day;}             case  9:if(c.day-cun>=1) {c.day=c.day-cun;cun=0;break;} else {c.month--;c.day=31;cun=cun-c.day;}             case 10:if(c.day-cun>=1) {c.day=c.day-cun;cun=0;break;} else {c.month--;c.day=30;cun=cun-c.day;}             case 11:if(c.day-cun>=1) {c.day=c.day-cun;cun=0;break;} else {c.month--;c.day=31;cun=cun-c.day;}             case 12:if(c.day-cun>=1) {c.day=c.day-cun;cun=0;break;} else {c.month--;c.day=30;cun=cun-c.day;}             }         }     }     return c; //返回Date c }   http://bbs.csdn.net/topics/380157851

user: user-1
content: 楼主你这个排版。。。 稍微看了下，发现了一个问题： case语句的else分支里{c.month++;c.day=1;cun=cun-(30-c.day)-1;}这样写是不对的 应该改为{cun=cun-(30-c.day)-1;c.month++;c.day=1;} 不然的话c.day都被你置成1了，你才用当月的最大天数减它，显然得到的结果是不对的

user: user-3
content: 我了个去，加号不是双目运算符么，括号里参数只有一个，即类对象本身加上一个参数，计算结果返回！ 方法：先写两个函数，将日期与一个整数互转，即选定以前的某天为基数，一般是以1970-1-1为第1天。 Date operator+(int b)实际上就可以先将日期转为自基数的天数后，加上参数上的天数，再转回日期就行了。 看看VC里的SystemTimeToVariantTime和VariantTimeToSystemTime。 你所写的方法，实话说，懒得看！