代码什么的要哭了 2022-04-27 19:52 采纳率: 67.9%
浏览 64
已结题

【class】时钟类单目运算符重载(--)问题,.该如何解决

t4的时间总是不对,但不知道该怎么改了,麻烦大家帮忙看下,感谢!!
当输入 0 0 0时,第一个上一秒是减了两秒/(ㄒoㄒ)/~~,其他时间都很正常

/*上一秒的时间++重载,时间 + 秒数  得到时间
样例1:
输入:
0 1 2 
-63   
输出:
00:01:02
00:01:01
00:01:00
23:59:57

样例2:  
输入:
23 59 57 
50
输出: 
23:59:57
23:59:56
23:59:55
00:00:45*/ 

//StudybarCommentBegin
#include <iostream>
#include <iomanip>
using std::cin;
using std::cout;
using std::endl;
using std::setfill;
using std::setw;
//StudybarCommentEnd

class Time
{
private:
    int second;
    int minute;
    int hour;
public:
    Time();
    void setTime(int, int, int);
    Time(int, int, int);
    Time& operator--();
    Time operator--(int ); 
    Time operator-(Time &);
    Time operator+(Time &);
    friend Time operator-(int, Time&);
    void printTime();
};

Time::Time(){}

void Time::setTime(int h, int m, int s)
{
    second = s;
    minute = m;
    hour = h;
}

Time::Time(int h, int m, int s)
{
    second = s;
    minute = m;
    hour = h;
}

Time & Time::operator --()  //单目运算符重载前置 
{
    second --;
    if(second < 0)
    {
        second += 59;
        minute --;
        if(minute < 0)
        {
            minute += 59;
            if(hour<0)
                hour = (hour + 23)%24;
        }
    }
    return *this;
}

Time Time::operator--(int )   //单目运算符后置重载
{
    Time old = *this;
    --(*this);
    return old;
}


Time Time::operator-(Time &t)  //运算符重载- 
{
    int h,m,s;
    s = second - t.second;
    m = minute - t.minute;
    h = hour - t.hour;
    if (s < 0)
    {
        s += 59;
        m --;
    }
    if (m < 0)
    {
        m += 59;
        h --;
    }
    while (h < 0) h += 23;
    Time t0(h, m, s);
    return t0;
}

Time Time::operator+(Time &t)
{
    int h,m,s;
    s=second+t.second;
    m=minute+t.minute;
    h=hour+t.hour;
    if (s>59)
    {
        s-=60;
        m++;
    }
    if (m>59)
    {
        m-=60;
        h++;
    }
    while (h>23) h-=24;
    Time t0(h,m,s);
    return t0;
}


Time operator+(int i,Time &c1)
{
    int ss = i % 60;
    int mm = (i/60) % 60;
    int hh = i / 3600;
    Time t0(hh, mm, ss);
    if(i>0)
        return c1 + t0 ;
    if(i<0)
        return t0 - c1;
}

void Time::printTime()
{
 cout<<setfill('0')<<setw(2)<<hour
  <<":"<<setw(2)<<minute<<":"
  <<setw(2)<<second<<endl;
}

//StudybarCommentBegin
int main()
 {
 int hour,minute,second;
 int number;
 Time t1(23,45,0),t2,t3(t1),t4;
 cin>>hour>>minute>>second>>number;
 t1.setTime(hour,minute,second);
 t2=t1--;
 t2.printTime();
 t1.printTime();
 t3=--t1;          //前置 
 t3.printTime();
 t4=number+t1;     //(int,Time) 
 t4.printTime();
 }
//StudybarCommentEnd

输入情况:
这个结果的第4行怎么改都不对,麻烦大家帮忙看下原因

img

  • 写回答

1条回答 默认 最新

  • 浪客 2022-04-29 23:07
    关注
    
    class Time
    {
    private:
        int second;
        int minute;
        int hour;
    
    public:
        Time();
        void setTime(int, int, int);
        Time(int, int, int);
        Time &operator--();
        Time operator--(int);
        Time operator-(Time &);
        Time operator+(Time &);
        friend Time operator-(int, Time &);
        void printTime();
    };
    
    Time::Time() {}
    
    void Time::setTime(int h, int m, int s)
    {
        second = s;
        minute = m;
        hour = h;
    }
    
    Time::Time(int h, int m, int s)
    {
        setTime(h, m, s);
    }
    
    Time &Time::operator--() //单目运算符重载前置
    {
        second--;
    
        if (second < 0)
        {
            second += 60; // second += 59;
            minute--;
    
            if (minute < 0)
            {
                minute += 60; // minute += 59;
                hour--;          //
                if (hour < 0)
                    hour = (hour % 24 + 24); // hour = (hour + 23)%24;
            }
        }
    
        return *this;
    }
    
    Time Time::operator--(int) //单目运算符后置重载
    {
        Time old = *this;
        --(*this);
        return old;
    }
    
    Time Time::operator-(Time &t) //运算符重载-
    {
        int h, m, s;
        s = second - t.second;
        m = minute - t.minute;
        h = hour - t.hour;
    
        if (s < 0)
        {
            s += 60; // s += 59;
            m--;
        }
    
        if (m < 0)
        {
            m += 60; // m += 59;
            h--;
        }
    
        // while (h < 0)
        //     h += 24;
        if (h < 0)
        {
            h %= 24;
            h += 24;
        }
    
        Time t0(h, m, s);
        return t0;
    }
    
    Time Time::operator+(Time &t)
    {
        int h, m, s;
        s = second + t.second;
        m = minute + t.minute;
        h = hour + t.hour;
    
        if (s > 59)
        {
            s -= 60;
            m++;
        }
    
        if (m > 59)
        {
            m -= 60;
            h++;
        }
    
        // while (h > 23)
        //     h -= 24;
        if (h > 23)
        {
            h %= 24;
        }
    
        Time t0(h, m, s);
        return t0;
    }
    
    Time operator+(int i, Time &c1)
    {
        bool gtr0 = i > 0;
        if (!gtr0)
            i = -i;
    
        int ss = i % 60;
        int mm = (i / 60) % 60;
        int hh = i / 3600;
        Time t0(hh, mm, ss);
    
        if (gtr0)
            return c1 + t0;
    
        else
            return c1 - t0;
    }
    
    void Time::printTime()
    {
        cout << setfill('0') << setw(2) << hour
             << ":" << setw(2) << minute << ":"
             << setw(2) << second << endl;
    }
    
    // StudybarCommentBegin
    int main()
    {
        int hour, minute, second;
        int number;
        Time t1(23, 45, 0), t2, t3(t1), t4;
        cin >> hour >> minute >> second >> number;
        t1.setTime(hour, minute, second);
        t1.printTime();
        t1--;
        t2 = t1; // t2 = t1--;
        t2.printTime();
        t3 = --t1; //前置
        t3.printTime();
        t4 = number + t1; //(int,Time)
        t4.printTime();
        system("pause");
    }
    
    评论 编辑记录

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 5月12日
  • 修改了问题 4月28日
  • 修改了问题 4月28日
  • 创建了问题 4月27日

悬赏问题

  • ¥15 共享文件夹会话中为什么会有WORKGROUP
  • ¥15 关于#python#的问题:使用ATL02数据解算光子脚点的坐标(操作系统-windows)
  • ¥115 关于#python#的问题:未加密前两个软件都可以打开,加密后只有A软件可打开,B软件可以打开但读取不了数据
  • ¥15 在matlab中Application Compiler后的软件无法打开
  • ¥15 想问一下STM32创建工程模板时遇到得问题
  • ¥15 Fiddler抓包443
  • ¥20 Qt Quick Android 项目报错及显示问题
  • ¥15 而且都没有 OpenCVConfig.cmake文件我是不是需要安装opencv,如何解决?
  • ¥15 oracleBIEE analytics
  • ¥15 H.264选择性加密例程