编写一个日期类,要求按 xxxx-xx-xx 的格式输出日期,实现加一天的操作,不考虑闰年问题,所有月份设为 30 天。
input:
2008-09-07
output:
2008-09-08
input:
2008-12-30
output:
2009-01-01
input:
2008-09-30
output:
2008-10-01
#include <iostream>
#include<vector>
#include <string>
#include <algorithm>
using namespace std;
class Solution {
private:
int m_year, m_month, m_day;
vector<int> threeNumber;
public:
Solution() {}
Solution(string str) {
int times = 0,i=0,from=0;
while (times < 3) {
while(str[i] != '-'&&i<str.length()) i++;
threeNumber.push_back(getInt(str,from, i - 1));
from = ++i;
times++;
}
assignMember();
check();
}
~Solution() {}
int getInt(const string &str, int from, int to) {
int finalInt = 0;
for (int i = from;i<=to; i++) {
finalInt = finalInt * 10 + (str[i] - '0');
}
return finalInt;
}
void assignMember() {
m_year = threeNumber[0];
m_month = threeNumber[1];
m_day = threeNumber[2];
}
void check() {
if (m_year > 9999 || m_month > 12 || m_day > 30) printf_s("data error! ");
else if (m_year < 1 || m_month < 1 || m_day < 1) printf_s("data error! ");
else ;
}
void addOneDay() {
if (m_day < 30) {
m_day++;
}
else if (m_day == 30) {
if (m_month == 12) {
m_year++;
m_month = 1;
m_day = 1;
}
else {
m_month ++;
m_day = 1;
}
}
else {
}
}
void showDate() {
cout << m_year << "-";
if (m_month < 10) {
cout << '0' << m_month << "-";
}
else {
cout << m_month << "-";
}
if (m_day < 10) {
cout << '0' << m_day<<endl;
}
else {
cout << m_day<<endl;
}
}
};
int main()
{
string str = "";
int times = 5;
while (times--) {
cin >> str;
Solution slu1(str);
slu1.addOneDay();
slu1.showDate();
}
system("pause");
return 0;
}