A代码完善--系统日期

A代码完善--系统日期

时间: 1ms        内存:128M

描述:

注:本题只需要提交填写部分的代码,请按照C++方式提交。

已知某操作系统的默认日期时间格式为 MMDDhhmmYYYY.ss ,其中YYYY表示年,MM表示月,DD表示日,hh表示小时,mm表示分种,ss表示秒。
按要求设计日期时间类DateTime,显示指定格式的日期。

#include <iostream>
#include <stdio.h>
#include <iomanip>
using namespace std;
class Date
{
public:
    Date(int year,int month,int day):year(year),month(month),day(day) {}
    Date(const Date &d)
    {
        this->year=d.year;
        this->month=d.month;
        this->day=d.day;
    }
protected:
    int year;
    int month;
    int day;
};

class Time
{
public:
    Time(int hour,int minute,int seconds):hour(hour),minute(minute),seconds(seconds) {}
    Time(const Time &t)
    {
        this->hour=t.hour;
        this->minute=t.minute;
        this->seconds=t.seconds;
    }
protected:
    int hour;
    int minute;
    int seconds;
};
/*************************
  在此处补充和修改你的代码
class DateTime :protected Date,protected Time
{
public:
    DateTime(Date &d, Time &t )
    void show()
};
**************************/
int main()
{
    int year,month,day;
    cin>>year>>month>>day;
    Date d1(year,month,day);
    int hour,minute,seconds;
    cin>>hour>>minute>>seconds;
    Time t1(hour,minute,seconds);
    DateTime dt(d1,t1);
    dt.show();
    return 0;
}

输入:

年 月 日 小时 分钟 秒

输出:

按格式MMDDhhmmYYYY.ss显示日期

示例输入:

2014 7 10 10 30 15

示例输出:

071010302014.15

提示:

参考答案(内存最优[1268]):


#include <iostream>
#include <stdio.h>
#include <iomanip>
using namespace std;
class Date
{
public:
    Date(int year,int month,int day):year(year),month(month),day(day) {}
    Date(const Date &d)
    {
        this->year=d.year;
        this->month=d.month;
        this->day=d.day;
    }
protected:
    int year;
    int month;
    int day;
};

class Time
{
public:
    Time(int hour,int minute,int seconds):hour(hour),minute(minute),seconds(seconds) {}
    Time(const Time &t)
    {
        this->hour=t.hour;
        this->minute=t.minute;
        this->seconds=t.seconds;
    }
protected:
    int hour;
    int minute;
    int seconds;
};
class DateTime :protected Date,protected Time
{
public:
    DateTime(Date &d, Time &t ):Date(d),Time(t){}
    void show()
	{
		printf("%02d%02d%02d%02d%d.%02d\n",month,day,hour,minute,year,seconds);
	}
};
int main()
{
    int year,month,day;
    cin>>year>>month>>day;
    Date d1(year,month,day);
    int hour,minute,seconds;
    cin>>hour>>minute>>seconds;
    Time t1(hour,minute,seconds);
    DateTime dt(d1,t1);
    dt.show();
    return 0;
}

参考答案(时间最优[0]):


#include <iostream>
#include <stdio.h>
#include <iomanip>
using namespace std;
class Date
{
public:
    Date(int year,int month,int day):year(year),month(month),day(day) {}
    Date(const Date &d)
    {
        this->year=d.year;
        this->month=d.month;
        this->day=d.day;
    }
protected:
    int year;
    int month;
    int day;
};

class Time
{
public:
    Time(int hour,int minute,int seconds):hour(hour),minute(minute),seconds(seconds) {}
    Time(const Time &t)
    {
        this->hour=t.hour;
        this->minute=t.minute;
        this->seconds=t.seconds;
    }
protected:
    int hour;
    int minute;
    int seconds;
};
class DateTime :protected Date,protected Time
{
public:
    DateTime(Date &d, Time &t ):Date(d),Time(t){}
    void show()
    {
     if(month<10)
        cout<<'0'<<month;
        else cout<<month;
        if(day<10)
         cout<<'0'<<day;
        else cout<<day;
        if(hour<10)
        cout<<'0'<<hour;
        else cout<<hour;
        if(minute<10)
        cout<<'0'<<minute;
        else cout<<minute;
        cout<<year<<'.';
        if(seconds<10)
        cout<<'0'<<seconds<<endl;
        else cout<<seconds;
    }
};
int main()
{
    int year,month,day;
    cin>>year>>month>>day;
    Date d1(year,month,day);
    int hour,minute,seconds;
    cin>>hour>>minute>>seconds;
    Time t1(hour,minute,seconds);
    DateTime dt(d1,t1);
    dt.show();
    return 0;
}

题目和答案均来自于互联网,仅供参考,如有问题请联系管理员修改或删除。

点赞

发表评论

电子邮件地址不会被公开。必填项已用 * 标注