好的,这是我编辑的代码,但现在每次编译时都会出现Segmentation Fault(核心转储)。我哪里出错了?我不确定我是否正确地“回溯”了它,但这是我从尝试中得到的结果:编程接收信号SIGSEGV,分段错误。 MyTime :: MyTime中的0x000109f0(this =, h =,m =)在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;
}
答案 0 :(得分:1)
首先,整理单位,例如:时间乘以或除以时间是没有意义的。 添加或减去时间的时间给出了时间。 时间乘以或除以数字给出时间。
接下来,在内部将时间存储为分钟。转换为人类消费的小时和分钟。
之后其余部分将落实到位。
答案 1 :(得分:0)
对于乘法和除法,如果时间完全正确,则MyTime :: simplify不执行任何操作。如果0 <=分钟&lt;分钟/ 60将总是为零。 60,因为分钟和60都是整数,所以它会向下舍入。类似地,只要相同的条件成立,分钟%60将等于分钟。除此之外,我同意这样的评论:一次又一次地划分是......奇怪的,你应该避免的事情。
对于第二个问题,请切换
(t1.hours * 60 + t1.minutes) < (hours * 60 + minutes)
到
(hours * 60 + minutes) < (t1.hours * 60 + t1.minutes)
以便运营商&lt;对
this < t1
实际上在操作员的左侧对此进行评估,因为您已将其置于过载状态。