写了一个输入时间与秒数的程序,输出该时间加上秒数的对应时间,但好像有极端情况没考虑到,不知道错在哪里
#include
using namespace std;
#define DayToSecond 86400 /* 一天等于86400秒 */
#define HourToSecond 3600 /* 一小时等于3600秒 */
#define MinuteToSecond 60 /* 一分钟等于60秒 */
#define LeapYear 366 /* 闰年有366天 */
#define CommonYear 365 /* 平年有365天 */
#define LeapFeb 29 /* 闰年的2月 */
#define CommonFeb 28 /* 平年的2月 */
int mon_c[13] = {31,28,31,30,31,30,31,31,30,31,30,31,0}; //一般长度
int main(){
int n;cin>>n;
while(n--){
unsigned int y,mon,d,h,m,s;unsigned long long t;
cin>>y>>mon>>d>>h>>m>>s>>t;
unsigned int Y,MON,D,H=0,M=0,S=0;
Y=y;MON=mon;D=d;
long long temp = 0;
int temp1 = 0;
if(mon!=1){
for(int i=0;i-1;i++){
if(i==1){if(((Y % 4 == 0) && (Y % 100 != 0)) || (Y % 400 == 0)){t+=29*DayToSecond;}
else t+=28*DayToSecond;}
else t+=mon_c[i]*DayToSecond;
}}
t+=(d-1)*DayToSecond+h*3600+m*60+s;
temp = t/DayToSecond; //cout<
if(temp!=0){ /* 超过一天 */
while(temp >= CommonYear){
if(((Y% 4 == 0) && (Y % 100 != 0)) || (Y % 400 == 0)){ /* 是闰年 */
if(temp >= LeapYear)temp-=LeapYear;/* 闰年的秒钟数 */
else break;
}
else {temp-=CommonYear;} /* 平年 */
Y++;
}//tem为剩余天数(一年内)
temp1=0;
while(temp >= CommonFeb){/* 超过了一个月 */
if((((Y % 4 == 0) && (Y % 100 != 0)) || (Y % 400 == 0))&&temp1 == 1){ /* 判断当年是不是闰年且是不是2月份 */
if(temp >= LeapFeb)temp-=LeapFeb;/* 闰月的秒钟数 */
else break;
}else{
if(temp >= mon_c[temp1])temp-=mon_c[temp1];/* 平年 */
else break;
}temp1+=1;
}
MON=temp1+1;
D = temp+1; /* 得到日期 */
}
temp = t%DayToSecond; /* 得到1天秒数 */
H= temp/HourToSecond; /* 得到小时数 */
M = (temp%HourToSecond)/MinuteToSecond; /* 得到分钟数 */
S = (temp%HourToSecond)%MinuteToSecond; /* 得到秒钟数 */
string month;
//月份
if(MON==1)month="Jan";
if(MON==2)month="Feb";
if(MON==3)month="Mar";
if(MON==4)month="Apr";
if(MON==5)month="May";
if(MON==6)month="Jun";
if(MON==7)month="Jul";
if(MON==8)month="Aug";
if(MON==9)month="Sept";
if(MON==10)month="Oct";
if(MON==11)month="Nov";
if(MON==12)month="Dec";
string ch="th";
if(D==1||D==21||D==31)ch="st";
else if(D==2||D==22)ch="nd";
else if(D==3||D==23)ch=="rd";
cout<" " <" "<<setw(2)<< setfill('0')<":"<<setw(2) << setfill('0')<":"<<setw(2) << setfill('0')<" "<return 0;}
答案错误20%
先将该年的月,日,时分秒全转换为秒数相加,再以该年一月一日为基计算,我试了一些结果都能运行对,想请问有什么问题
Sept改为Sep ?
试试
跨年加1秒
跨年加365*24*3600秒
跨闰年加366*24*3600秒
加或减2147483647秒
加或减2147483648秒