题目描述:
输入日期和时间,输出该日期和时间的下一秒。考虑闰年情况
输入:2016/03/18 01:01:01
输出:2016/03/18 01:01:02
下面为参考程序:
// 计算下一秒.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include<cstdlib>
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <iterator>
#include<iostream>
using namespace std;
int GetMonthDay(int nYear, int nMonth)
{
int nDays = -1;
if (nMonth >= 1 && nMonth <= 12)
{
switch (nMonth)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
nDays = 31;
break;
case 4:
case 6:
case 9:
case 11:
nDays = 30;
break;
case 2:
if (((nYear % 400 == 0) || (nYear % 4 == 0 && nYear % 100 != 0)))
{
nDays = 29;
}
else
{
nDays = 28;
}
}
}
return nDays;
}
void NextSecond(int *nYear, int *nMonth, int *nDate, int *nHour, int *nMinute, int *nSecond)
{
int nDays = 0;
(*nSecond)++; //加上一秒
if (*nSecond >= 60)
{
*nSecond = 0;
(*nMinute)++;
if (*nMinute >= 60)
{
*nMinute = 0;
(*nHour)++;
if (*nHour >= 24)
{
*nHour = 0;
(*nDate)++;
nDays = GetMonthDay(*nYear, *nMonth);
if ((*nDate > nDays) && (nDays > 0))
{
*nDate = 1;
(*nMonth)++;
if (*nMonth > 12)
{
*nMonth = 1;
(*nYear)++;
}
}
}
}
}
}
int _tmain(int argc, _TCHAR* argv[])
{
int n, y, r, s, f, m;
string str;
while (getline(cin, str))
{
n = atoi(str.substr(0, 4).c_str());
y = atoi(str.substr(5, 2).c_str());
r = atoi(str.substr(8, 2).c_str());
s = atoi(str.substr(11, 2).c_str());
f = atoi(str.substr(14, 2).c_str());
m = atoi(str.substr(17, 2).c_str());
cout << n << "/" << (y < 10 ? "0" : "") << y << "/" << (r < 10 ? "0" : "") << r << " "
<< (s < 10 ? "0" : "") << s << ":" << (f < 10 ? "0" : "") << f << ":"
<< (m < 10 ? "0" : "") << m << endl;
NextSecond(&n, &y, &r, &s, &f, &m);
cout << n << "/" << (y < 10 ? "0" : "") << y << "/" << (r < 10 ? "0" : "") << r << " "
<< (s < 10 ? "0" : "") << s << ":" << (f < 10 ? "0" : "") << f << ":"
<< (m < 10 ? "0" : "") << m << endl;
}
system("pause");
return 0;
}