题目描述
已知日期类Date,有属性:年、月、日,其他成员函数根据需要自行编写,注意该类没有输出的成员函数
已知时间类Time,有属性:时、分、秒,其他成员函数根据需要自行编写,注意该类没有输出的成员函数
现在编写一个全局函数把时间和日期的对象合并起来一起输出,
函数原型为:void display(const Date &d, const Time &t)
函数输出要求为:
1、时分秒输出长度固定2位,不足2位补0
2、年份输出长度固定为4位,月和日的输出长度固定2位,不足2位补0
例如2017年3月3日19时5分18秒
则输出为:2017-03-03 19:05:18
程序要求
1、把函数display作为时间类、日期类的友元
2、分别创建一个日期对象和时间对象,保存日期的输入和时间的输入
3、调用display函数实现日期和时间的合并输出
输入
第一行输入t表示有t组示例
接着一行输入三个整数,表示年月日
再接着一行输入三个整数,表示时分秒
依次输入t组示例
输出
每行输出一个日期和时间合并输出结果
输出t行
样例查看模式
正常显示查看格式
输入样例1 <-复制
输出样例1
#include<iostream>
using namespace std;
class Time;//注意这里要提前声明一下
class Date
{
public:
Date(int year, int month, int day)
{
this->year = year;
this->month = month;
this->day = day;
}
friend void Display(const Date& d, const Time& t);
private:
int year;
int month;
int day;
};
class Time
{
public:
Time(int hour, int min, int sec)
{
this->hour = hour;
this->min = min;
this->sec = sec;
}
friend void Display(const Date& d, const Time& t);
private:
int hour;
int min;
int sec;
};
void Display(const Date& d, const Time& t)
{
cout << d.year << "-";
if (d.month < 10)
{
cout << "0"<<d.month;
}
else
{
cout << d.month;
}
cout << "-";
if (d.day < 10)
{
cout << "0" << d.day;
}
else
{
cout << d.day;
}
cout << " ";
if (t.hour < 10)
{
cout << "0" << t.hour;
}
else
{
cout << t.hour;
}
cout << ":";
if (t.min < 10)
{
cout << "0" << t.min;
}
else
{
cout << t.min;
}
cout << ":";
if (t.sec < 10)
{
cout << "0" << t.sec;
}
else
{
cout << t.sec;
}
cout << endl;
}
int main()
{
int t;
cin >> t;
int year, month, day;
int hour, min, sec;
while (t--)
{
cin >> year >> month >> day >> hour >> min >> sec;
Date d(year, month, day);
Time t(hour, min, sec);
Display(d, t);
}
return 0;
}
977

被折叠的 条评论
为什么被折叠?



