且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

C/C++---时间类的改进(运算符重载)

更新时间:2022-08-12 19:31:44

【问题描述】

对前面实验写过的Time类进行修改,删去Add和Sub成员函数,通过重载“+”、“-”运算符直接进行时间的加减运算。

提示:
(1)可以用友元函数来实现“+”“-”运算符的重载。
(2)加法运算符可以是两个Time对象进行相加,也可以是一个表示秒数的int型数据加上一个Time对象,还可以是Time对象加上int型数据,得到的结果都是Time类型的对象。
(3)减法运算符可以是两个Time对象进行相减,也可以是Time对象减去一个表示秒数的int型数据,得到的结果都是Time类型的对象。

主函数设计如下,请勿修改:
int main(){undefined

Time t1(2,34),t2,t3;

t2.SetTime(13,23,34);

cout<<“t1+t2:”;

t3=t1+t2;//两个Time类对象相加

t3.print_24();

cout<<"\nt1+65:";

t3=t1+65;//Time类对象加上65秒

t3.print_24();

cout<<"\n65+t1:";

t3=65+t1;//65秒加上Time类对象

t3.print_24();

cout<<"\nt2-t1:";

t3=t2-t1;//两个Time类对象相减

t3.print_24();

cout<<"\nt1-70:";

t3=t1-70;//Time类对象减去70秒

t3.print_24();

return 0;

}

【样例输出】

t1+t2:15:57:34

t1+65:02:35:05

65+t1:02:35:05

t2-t1:10:49:34

t1-70:02:32:50

【代码如下】

#include<iostream>
using namespace std;
class Time
{
    int hour, minute, second;
public:
    int SecCalc() { return(hour * 60 + minute) * 60 + second; }
    Time(int h, int m, int s = 0);
    Time(int s = 0);
    void SetTime(int h = 0, int m = 0, int s = 0);
    void print_12();
    void print_24();
    Time operator+(Time &t);
    Time operator-(Time &t);
    Time operator+(int s);
    Time operator-(int s);
    friend Time operator+(int s,Time &t);
    friend Time operator-(int s, Time &t);
};
Time::Time(int h, int m, int s)
{
    hour = h;
    minute = m;
    second = s;
}
Time::Time(int s)
{
    hour = minute = second = 0;
    for (int i = 0; i < s; i++)
    {
        second++;
        if (second == 60)
        {
            second = 0;
            minute++;
            if (minute == 60)
            {
                hour++;
                minute = 0;
            }
        }
    }
}
void Time::SetTime(int h, int m, int s)
{
    hour = h;
    minute = m;
    second = s;
}
void Time::print_12()
{
    bool afternoon = false;
    if (hour > 12)
    {
        if (hour - 12 < 10)
            cout << "0";
        cout << hour - 12 << ":";
        afternoon = true;
    }
    else
    {
        if (hour < 10)
            cout << "0";
        cout << hour << ":";
    }
    if (minute < 10)
        cout << "0";
    cout << minute << ":";
    if (second < 10)
        cout << "0";
    cout << second;
    if (afternoon)
        cout << " PM" << endl;
    else
        cout << " AM" << endl;
}
void Time::print_24()
{
    if (hour < 10)
        cout << "0";
    cout << hour << ":";
    if (minute < 10)
        cout << "0";
    cout << minute << ":";
    if (second < 10)
        cout << "0";
    cout << second << endl;
}
Time Time::operator+(Time & t)
{
    Time temp = *this;
    for (int i = 0; i < t.second; i++)
    {
        temp.second += 1;
        if (temp.second == 60)
        {
            temp.minute += 1;
            if (temp.minute == 60)
            {
                temp.hour += 1;
                temp.minute = 0;
            }
            temp.second = 0;
        }
    }
    for (int i = 0; i < t.minute; i++)
    {
        temp.minute += 1;
        if (temp.minute == 60)
        {
            temp.hour += 1;
            temp.minute = 0;
        }
    }
    temp.hour += t.hour;
    return temp;
}
Time Time::operator-(Time & t)
{
    Time temp = *this;
    temp.hour -= t.hour;
    for (int i = 0; i < t.minute; i++)
    {
        temp.minute -= 1;
        if (temp.minute < 0)
        {
            temp.hour -= 1;
            temp.minute = 59;
        }
    }
    for (int i = 0; i < t.second; i++)
    {
        temp.second -= 1;
        if (temp.second < 0)
        {
            temp.minute -= 1;
            if (temp.minute < 0)
            {
                temp.hour -= 1;
                temp.minute = 59;
            }
            temp.second = 59;
        }
    }
    return temp;
}
Time Time::operator+(int s)
{
    Time temp = *this;
    for (int i = 0; i < s; i++)
    {
        temp.second += 1;
        if (temp.second == 60)
        {
            temp.minute += 1;
            if (temp.minute == 60)
            {
                temp.hour += 1;
                temp.minute = 0;
            }
            temp.second = 0;
        }
    }
    return temp;
}
Time Time::operator-(int s)
{
    Time temp = *this;
    for (int i = 0; i < s; i++)
    {
        temp.second -= 1;
        if (temp.second < 0)
        {
            temp.minute -= 1;
            if (temp.minute < 0)
            {
                temp.hour -= 1;
                temp.minute = 59;
            }
            temp.second = 59;
        }
    }
    return temp;
}
Time operator+(int s, Time & t)
{
    return t + s;
}
Time operator-(int s, Time & t)
{
    return t - s;
}
int main()
{//主函数设计如下,请勿修改.
    Time t1(2, 34), t2, t3;
    t2.SetTime(13, 23, 34);
    cout << "t1+t2:";
    t3 = t1 + t2;//两个Time类对象相加
    t3.print_24();
    cout << "\nt1+65:";
    t3 = t1 + 65;//Time类对象加上65秒#
    t3.print_24();
    cout << "\n65+t1:";
    t3 = 65 + t1;//65秒加上Time类对象#
    t3.print_24();
    cout << "\nt2-t1:";
    t3 = t2 - t1;//两个Time类对象相减
    t3.print_24();
    cout << "\nt1-70:";
    t3 = t1 - 70;//Time类对象减去70秒
    t3.print_24();
    return 0;
}