比如说当前时间 2010-8-12 19:31:01 我追加20天他应该是2010-9-1 19:31:01 依次 这样 我追加30天就是一个月 求代码 我是菜鸟 直接贴代码就好了 追回的呈现格式 也要 2010-8-12 19:31:01 这样的 直接上代码哈 !!!意思应该很明白了
c++如何在当前时间追加天数 并得出追加后的当前时间 ?
答案:2 悬赏:40 手机版
解决时间 2021-04-30 03:45
- 提问者网友:放下
- 2021-04-29 11:35
最佳答案
- 五星知识达人网友:迷人又混蛋
- 2021-04-29 11:40
#include <iostream>
using namespace std;
class DAY
{
private:
int y;
int m;
int d;
int days[12];
public:
DAY()
{
cout<<"请输入当前日期"<<endl;
cout <<"year"<<endl;
cin >> y;
cout << "mouth"<<endl;
cin >>m;
cout << "day"<<endl;
cin >> d;
int i;
for( i = 1 ; i <= 12 ; i++)
{
switch(i)
{
case 2:
days[i-1] = isLeapYear() ? 29:28; //设置2月天数
break;
case 4:
case 6:
case 9:
case 11:
days[i-1] = 30;
break;
default :
days[i-1] = 31;
}
}
}
void show()
{
cout <<"year:"<<y<<endl;
cout <<"mouth:"<<m<<endl;
cout <<"day:"<<d<<endl;
}
int judge_day(int mouth)
{
return days[mouth-1];
}
void calculate(int increase)
{
if(d + increase <= days[m-1])
d += increase;
else
{
increase -= days[m-1] - d;
m++;
if(m>12)
{
//if m is greater than 12 , and increase is less than days[m-1]
//,then the following while statement will not be executed,
//so must reset m and year++
m = 1;
y++;
}
while(increase -days[m-1] >0)
{
increase -= days[m-1];
m++;
if(m > 12)
{
y++;
m = 1;
days[1] = isLeapYear() ? 29 : 28;
}
}
d = increase;
}
}
bool isLeapYear()
{
return ( y%4 == 0 && y%100 != 0 ) || (y%400 == 0);
}
};
int main()
{
DAY test;
int increase;
test.show();
cout <<"请输入天数,以查询该天之后的日期"<<endl;
cin >> increase;
test.calculate(increase);
cout <<"\n"<<increase<<"天以后是:"<<endl;
test.show();
return 0;
}
// 测试:
using namespace std;
class DAY
{
private:
int y;
int m;
int d;
int days[12];
public:
DAY()
{
cout<<"请输入当前日期"<<endl;
cout <<"year"<<endl;
cin >> y;
cout << "mouth"<<endl;
cin >>m;
cout << "day"<<endl;
cin >> d;
int i;
for( i = 1 ; i <= 12 ; i++)
{
switch(i)
{
case 2:
days[i-1] = isLeapYear() ? 29:28; //设置2月天数
break;
case 4:
case 6:
case 9:
case 11:
days[i-1] = 30;
break;
default :
days[i-1] = 31;
}
}
}
void show()
{
cout <<"year:"<<y<<endl;
cout <<"mouth:"<<m<<endl;
cout <<"day:"<<d<<endl;
}
int judge_day(int mouth)
{
return days[mouth-1];
}
void calculate(int increase)
{
if(d + increase <= days[m-1])
d += increase;
else
{
increase -= days[m-1] - d;
m++;
if(m>12)
{
//if m is greater than 12 , and increase is less than days[m-1]
//,then the following while statement will not be executed,
//so must reset m and year++
m = 1;
y++;
}
while(increase -days[m-1] >0)
{
increase -= days[m-1];
m++;
if(m > 12)
{
y++;
m = 1;
days[1] = isLeapYear() ? 29 : 28;
}
}
d = increase;
}
}
bool isLeapYear()
{
return ( y%4 == 0 && y%100 != 0 ) || (y%400 == 0);
}
};
int main()
{
DAY test;
int increase;
test.show();
cout <<"请输入天数,以查询该天之后的日期"<<endl;
cin >> increase;
test.calculate(increase);
cout <<"\n"<<increase<<"天以后是:"<<endl;
test.show();
return 0;
}
// 测试:
全部回答
- 1楼网友:逐風
- 2021-04-29 13:09
我不能帮你但是我想要分嘿嘿
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯