【TOJ 5257】C++实验:重载转型运算符

描述

编写日期类,将日期格式化输出。

主函数里的代码已经给出,请补充完整,提交时请勿包含已经给出的代码。

int main()
{
	int y, m, d;
	while(cin>>y>>m>>d)
	{
		CDate date(y, m, d);
		cout<<(string)date<<endl;
	}
}

输入

输入数据有多组,每组一行,包括年、月、日三个正整数。

输入保证日期正确。

输出

每组输出格式化后的日期,见样例。

样例输入

2000 2 29
2016 12 1

样例输出

2000-2-29
2016-12-1

#include<iostream>
#include<sstream>
using namespace std;
class CDate{
public:
    int y,m,d;
    CDate(int y,int m,int d):y(y),m(m),d(d){}
    operator string()
    {
        string s;
        stringstream ss;
        ss<<y<<"-"<<m<<"-"<<d;
        ss>>s;
        return s;
    }
};
int main()
{
    int y, m, d;
    while(cin>>y>>m>>d)
    {
        CDate date(y, m, d);
        cout<<(string)date<<endl;
    }
}

猜你喜欢

转载自www.cnblogs.com/kannyi/p/9051761.html