1

好的,这是我编辑的代码,但现在每次编译时都会出现分段错误(核心转储)。我哪里出错了?我不确定我是否正确地“回溯”了它,但这就是我尝试得到的结果:程序收到信号 SIGSEGV,分段错误。0x000109f0 in MyTime::MyTime (this=, h=, m=) at MyTime.cc:10 10 MyTime::MyTime(int h, int m){

//MyTime.h File
#include <iostream>

class MyTime
{
  public:

   MyTime(int h = 0, int m = 0);

   void Reset(int h, int m);

   void input();

   void output() const;

   MyTime operator + (const MyTime& t1) const;

   MyTime operator - (const MyTime& t1) const;

   MyTime operator * (const int& num) const;

   MyTime operator / (const int& num) const;

   bool operator == (const MyTime& t1) const;

   bool operator < (const MyTime& t1) const;

   bool operator <= (const MyTime& t1) const;

   int get_hours() const{return hours;}
   int get_minutes() const{return minutes;}

 private:
    void simplify();
    int hours;      // hours can be > 24
    int minutes;   // 0 <= minutes <= 59
};

    std::istream& operator >>(std::istream& fin, MyTime& t);

    std::ostream& operator <<(std::ostream& fout, const MyTime& t);

//MyTime.cc File
#include "MyTime.h"
#include <cstdlib>
#include <iostream>
#include <iomanip>
using namespace std;

// Constructor

MyTime::MyTime(int h, int m){
    hours = h;
    minutes = m;
}

void MyTime::Reset(int h, int m){
    hours = h;
    minutes = m;
}

void MyTime::simplify(){
    hours += minutes/60;
    minutes = minutes%60;
}

void MyTime::input(){
    char junk;
    cin >> hours;
    cin.get(junk);
    cin >> minutes;
    simplify();
}

void MyTime::output() const{
    cout << hours << ':' << setw(2) << setfill('0') << minutes;
}

MyTime MyTime::operator +(const MyTime& t1) const{
    MyTime tmp;
    tmp.hours = t1.hours + hours;
    tmp.minutes = t1.minutes + minutes;
    tmp.simplify();
    return tmp;
}

MyTime MyTime::operator -(const MyTime& t1) const{
    MyTime tmp;
    tmp.minutes = abs((t1.hours*60+t1.minutes) -
                                    (hours*60+minutes));
    tmp.simplify();
    return tmp;
}

MyTime MyTime::operator /(const int& num) const{
    MyTime tmp;
    tmp.minutes = hours * 60 + minutes;
    tmp.minutes /= num;
    tmp.simplify();
    return tmp;
}

MyTime MyTime::operator *(const int& num) const{
    MyTime tmp;
    tmp.minutes = hours * 60 + minutes;
    tmp.minutes *= num;
    tmp.simplify();
    return tmp;
}

bool MyTime::operator == (const MyTime& t1) const{
    return t1.hours == hours && t1.minutes == minutes;
}

bool MyTime::operator < (const MyTime& t1) const{
    return (t1.hours * 60 + t1.minutes) < (hours * 60 + minutes);
}

bool MyTime::operator <=(const MyTime& t1) const{
    return (t1 == (hours * 60 + minutes)) || (t1 < (hours * 60 + minutes));
}

ostream& operator <<(ostream& fout, const MyTime& t){
    t.output();
    return fout;
}

istream& operator >>(istream& fin, MyTime& t){
    t.input();
    return fin;
}

//main.cc File
#include <iostream>
#include "MyTime.h"

int main()
{
   MyTime t1, t2;
   int scalar;

   std::cout << "Enter a time:  ";
   std::cin >> t1;

   std::cout << "Enter another time:  ";
   std::cin >> t2;

   std::cout << "Enter a scalar to manipulate those times:  ";
   std::cin >> scalar;

   if(t1 == t2)
     std::cout << t1 << " is equal to " << t2 << std::endl;

   if(t1 < t2)
     std::cout << t1 << " is less than " << t2 << std::endl;

   if(t1 <= t2)
     std::cout << t1 << " is less than or equal to " << t2 << std::endl;

     std::cout << t1 << " + " << scalar << " = " << t1 + scalar << std::endl;
     std::cout << t1 << " - " << scalar << " = " << t1 - scalar << std::endl;
     std::cout << t1 << " * " << scalar << " = " << t1 * scalar << std::endl;
     std::cout << t1 << " / " << scalar << " = " << t1 / scalar << std::endl;

     std::cout << t2 << " + " << scalar << " = " << t2 + scalar << std::endl;
     std::cout << t2 << " - " << scalar << " = " << t2 - scalar << std::endl;
     std::cout << t2 << " * " << scalar << " = " << t2 * scalar << std::endl;
     std::cout << t2 << " / " << scalar << " = " << t2 / scalar << std::endl;

     return 0;
}
4

2 回答 2

1

首先,理清单位,例如:时间乘或除以时间是没有意义的。时间加上或减去时间就是时间。时间乘以或除以一个数字就是时间。

接下来,在内部将时间存储为分钟。转换为小时和分钟供人类消费。

之后,其余的将就位。

于 2013-07-03T04:15:02.777 回答
0

对于乘法和除法,如果时间格式正确,MyTime::simplify 什么也不做。如果 0 <= 分钟 < 60,分钟/60 将始终为零,因为分钟和 60 都是整数,所以它会向下舍入。同样,只要相同的条件成立,minutes%60 将等于分钟。除此之外,我同意将一个时间除以另一个时间是......奇怪的评论,你应该避免。

对于您的第二个问题,请切换

(t1.hours * 60 + t1.minutes) < (hours * 60 + minutes)

(hours * 60 + minutes) < (t1.hours * 60 + t1.minutes)

使运算符 < for

this < t1

实际上在运算符的左侧对此进行评估,因为您已将其放入重载中。

于 2013-07-03T02:41:08.633 回答