zoukankan      html  css  js  c++  java
  • 4-2电子时钟中的运算符重载

    4-2 电子时钟中的运算符重载

    Time Limit: 1000MS Memory limit: 65536K

    题目描写叙述

    通过本题目的练习能够运算符重载的方法;
    设计一个时间类Time,私有数据成员有hour(时)、minute(分)、second(秒);
    公有成员函数有:setHour(int)设置数据成员hour的值。非法的输入默觉得12;setMinue(int)设置数据成员minute的值。非法输入默觉得0;setSecond(int)设置数据成员second的值,非法输入默觉得0;setTime(int,int。int)设置时、分、秒三个数据成员的值;三个成员函数int getHour(); int getMinute(); int getSecond();分别用于获取时间对象的属性值。
    定义两个构造函数Time(); 和 Time(int,int,int);
    定义一个成员函数void displayTime(); 用于显示时间,注意格式为 hh:mm:ss。位数不够用0填充。
    定义一个时钟添加1的成员函数 void tick(); 把second的值加1。并注意是否到60;
    定义一个成员或友元函数 bool operator= =(….); 推断两个时间对象的值是否相等;
    定义一个成员或友元函数bool operator>(…..); 推断第一个时间对象的值是否大于第二个时间对象的值。
     
     
    在主函数main()中指定開始时间和结束时间,并调用对应成员函数,显示从開始时间到结束时间之间全部时间对象的值,其格式见演示样例输出。

    输入

    输入6个整数。之间用一个空格间隔。分别表示開始时间的时、分、秒和结束时间的时、分、秒的值

    输出

    从開始时间到结束时间之间全部时间对象的值。每一个值占一行,格式为hh:mm:ss

    演示样例输入

    01 01 01 01 01 10

    演示样例输出

    01:01:01
    01:01:02
    01:01:03
    01:01:04
    01:01:05
    01:01:06
    01:01:07
    01:01:08
    01:01:09
    01:01:10

    提示

    输入
    11 10 12 10 12 56
    输出
    The begin time is not earlier than the end time!

    来源

     黄晶晶

    演示样例程序


    #include <iostream>
    using namespace std;
    class Time
    {
    private:
        int hour;
        int minute;
        int second;
    public:
        Time(int h,int m,int s)
        {
            hour=h;
            minute=m;
            second=s;
        }
        Time operator ++ ();
        void display();
        int cmp(Time &t);
    };
    
    
    Time Time::operator ++ ( )
    
    {
    
        if(++second>= 60)
        {
            second-= 60;
    
            if(++minute>= 60)
            {
                minute-=60;
                ++hour;
            }
        }
    }
    void Time::display()
    {
        if(hour<10)
            cout<<"0"<<hour<<":";
        else
            cout<<hour<<":";
        if(minute<10)
            cout<<"0"<<minute<<":";
        else
            cout<<minute<<":";
        if(second<10)
            cout<<"0"<<second;
        else
            cout<<second;
        cout<<endl;
    }
    int Time::cmp(Time & t)
    {
        if(hour>t.hour)
            return 0;
        else if(hour== t.hour&& minute > t.minute)
            return 0;
        else if(hour == t.hour && minute == t.minute && second > t.second)
            return 0;
        else
            return 1;
    }
    
    
    int main()
    {
        int h1,h2,m1,m2,s1,s2;
        cin>>h1>>m1>>s1;
        cin>>h2>>m2>>s2;
        Time t1(h1,m1,s1);
        Time t2(h2,m2,s2);
        if(!t1.cmp(t2) )
        {
        cout<<"The begin time is not earlier than the end time!"<<endl;
        }
        else
        {
            while(t1.cmp(t2))
            {
                t1.display();
                ++t1;
            }
        }
        return 0;
    }
    

    
  • 相关阅读:
    python基础()
    《野草在歌唱》读后感 读书笔记
    《饥饿的盛世》读后感 读书笔记
    《T.S.艾略特传:不完美的一生》读后感
    《宋徽宗》读后感 读书笔记
    《孔子传》读后感 读书笔记
    《武曌》读后感 读书笔记
    《百岁人生》读后感 读书笔记
    《曾国藩的正面与侧面》读后感 读书笔记
    《洞见》读后感 读书笔记
  • 原文地址:https://www.cnblogs.com/mthoutai/p/6792295.html
Copyright © 2011-2022 走看看