年份加一

编写一个日期类,要求按 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;//update start point
			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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值