gpt4 book ai didi

c++ - 如何乘法和除法一天中的时间,例如10 :00 and 5:00 with overloaded functions?

转载 作者:太空宇宙 更新时间:2023-11-04 11:52:42 26 4
gpt4 key购买 nike

好的,这是我编辑过的代码,但现在我每次编译时都会遇到段错误(核心已转储)。我哪里出错了?我不确定我是否正确地“回溯”了它,但这是我从尝试中得到的结果:程序接收到信号 SIGSEGV,段错误。MyTime::MyTime 中的 0x000109f0 (this=, h=, m=) 在 MyTime.cc:1010 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;
}

最佳答案

首先梳理一下单位,比如:时间乘以时间除以时间没有意义。时间加上或减去时间得到时间。时间乘以或除以一个数字得出时间。

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

在那之后,剩下的就到位了。

关于c++ - 如何乘法和除法一天中的时间,例如10 :00 and 5:00 with overloaded functions?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17438791/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com