Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 运算符重载逻辑问题_C++_Oop_Operator Overloading_Overloading - Fatal编程技术网

C++ 运算符重载逻辑问题

C++ 运算符重载逻辑问题,c++,oop,operator-overloading,overloading,C++,Oop,Operator Overloading,Overloading,我们在课堂上的作业之一是创建一个程序,使用对象显示小时、分钟和秒。有了这些数字,我们现在必须重载各种操作符,使秒/分钟增加1,并使用++和-。我在使用--运算符时遇到了一些问题,因为它没有按预期工作,如果我输入0分钟,它会减少返回值的分钟数,比如128分钟。当我开始在这方面,我真的很感谢一些帮助 然后第二部分使用其他操作符(>=hour1; cin>>分钟1; cin>>second1; cout>minute2; cin>>第二; 库特 我和接线员有点问题 实际上,您还有很多麻烦!已经oper

我们在课堂上的作业之一是创建一个程序,使用对象显示小时、分钟和秒。有了这些数字,我们现在必须重载各种操作符,使秒/分钟增加1,并使用++和-。我在使用--运算符时遇到了一些问题,因为它没有按预期工作,如果我输入0分钟,它会减少返回值的分钟数,比如128分钟。当我开始在这方面,我真的很感谢一些帮助

然后第二部分使用其他操作符(><>=hour1; cin>>分钟1; cin>>second1; cout>minute2; cin>>第二; 库特 我和接线员有点问题

实际上,您还有很多麻烦!已经
operator++
无法按预期工作。请尝试:

Time t(0, 59, 59);
++t;
一旦你用溢出增加秒数,下一个可能溢出的是分钟数,所以你需要先检查这些

++seconds;
if(seconds == 60)
{
    seconds = 0;
    ++minutes;
    // only, if minutes were incremented, they can overflow, so check only here needed
   if(minutes == 60)
   {
       minutes = 0;
       // open: how do you want to handle hours overflowing?
       // variant 1: just go on, counting 23, 24, 25, ...
       ++hours;
       // variant 2: restart at 0:
       hours = (hours + 1) % 24;
       // variant 3 (my favourite): rember in a flag that we overflowed
       // (and have a getter for so that user can check):
       isWrapAround = hours == 23; // new member variable of type bool
       hours = (hours + 1) % 24;
   }
}
类似地,您需要处理
操作符--
,只需将
++
的每次出现都替换为
--
,并将溢出检测调整为下溢检测。注意后者:您的原始代码没有进行正确的下溢检测:

--seconds;
if(seconds == 0)
当我们实际还有1秒时,这将减少分钟数,但00:00:00是一个有效时间!因此您需要在减少之前检查0(
if(seconds-->=0)
,或者在减少之后检查负值(
--seconds;if(seconds==-1)
if(seconds<0)
)。使用此修复程序,
+=59
也将不再正确,您需要
+=60
或者干脆
=59

通常,前置递增运算符和-递减运算符返回对当前对象的引用。这将允许例如
++(++时间)

后增量和-减量运算符非常奇怪…请重新验证是否真的需要在-分钟/分钟内完成此任务(如果是这样,我只能对您的老师摇头…)。这对任何人来说都是一个巨大的惊喜,因为运算符的行为与通常的行为完全不同!后一种是:

Time operator++(int)
{
    Time tmp(*this);
    ++*this;
    return tmp;
}
如果你真的,真的应该增加分钟数(注意讽刺的是:post-X-crement操作符实际上表现得像pre-X-crement,至少你最初的方法是这样的):只需保持秒数不变。然后,在post操作符中,你只需要在相应的pre变体中最外层If的主体。然后可以重新编写它们(避免代码重复)如下所示:

最后:比较:不幸的是,我们还没有可用的C++20,否则我们可以简单地实现spaceship操作符(
)…没关系,我们仍然可以使用普通函数代替,并在操作符中使用此函数:

int Time::compare(Time const& other) const
{
    // most relevant are hours, if these differ, values of minutes or
    // seconds don't matter any more...
    int result = hours - other.hours;
    if(result == 0)
    {
        // so hours are the same...
        // minutes then are relevant next
        result = minutes - other.minutes;
        if(result == 0)
            result = seconds - other.seconds;
    }
    return result;
}
那么,所有要实施的运营商将如下所示:

bool Time::operator@(Time const& other) const
{
    return compare(other) @ 0;
}
其中,
@
代表所需的所有操作员(
=
!=
=


奖励:记住没有单独标志的溢出:

您不再需要其他变量,但它需要更复杂的逻辑

首先,离开-1(
操作符--
)将指示发生了回卷。相应地,回卷的getter将
返回hours==-1;

在我们以前直接使用
hours
的任何其他地方,我们现在将使用getter for,如下所示:

int Time::getHours() const
{
    return hours + (hours == -1);
}
计算增量的值稍微复杂一些:

hours = (getHours() + 1) % 24 - (hours == 23);
我和接线员有点问题

实际上,您还有很多麻烦!已经
operator++
无法按预期工作。请尝试:

Time t(0, 59, 59);
++t;
一旦你用溢出增加秒数,下一个可能溢出的是分钟数,所以你需要先检查这些

++seconds;
if(seconds == 60)
{
    seconds = 0;
    ++minutes;
    // only, if minutes were incremented, they can overflow, so check only here needed
   if(minutes == 60)
   {
       minutes = 0;
       // open: how do you want to handle hours overflowing?
       // variant 1: just go on, counting 23, 24, 25, ...
       ++hours;
       // variant 2: restart at 0:
       hours = (hours + 1) % 24;
       // variant 3 (my favourite): rember in a flag that we overflowed
       // (and have a getter for so that user can check):
       isWrapAround = hours == 23; // new member variable of type bool
       hours = (hours + 1) % 24;
   }
}
类似地,您需要处理
操作符--
,只需将
++
的每次出现都替换为
--
,并将溢出检测调整为下溢检测。注意后者:您的原始代码没有进行正确的下溢检测:

--seconds;
if(seconds == 0)
当我们实际还有1秒时,这将减少分钟数,但00:00:00是一个有效时间!因此您需要在减少之前检查0(
if(seconds-->=0)
,或者在减少之后检查负值(
--seconds;if(seconds==-1)
if(seconds<0)
)。使用此修复程序,
+=59
也将不再正确,您需要
+=60
或者干脆
=59

通常,前置递增运算符和-递减运算符返回对当前对象的引用。这将允许例如
++(++时间)

后增量和-减量运算符非常奇怪…请重新验证是否真的需要在-分钟/分钟内完成此任务(如果是这样,我只能对您的老师摇头…)。这对任何人来说都是一个巨大的惊喜,因为运算符的行为与通常的行为完全不同!后一种是:

Time operator++(int)
{
    Time tmp(*this);
    ++*this;
    return tmp;
}
如果你真的,真的应该增加分钟数(注意讽刺的是:post-X-crement操作符实际上表现得像pre-X-crement,至少你最初的方法是这样的):只需保持秒数不变。然后,在post操作符中,你只需要在相应的pre变体中最外层If的主体。然后可以重新编写它们(避免代码重复)如下所示:

最后:比较:不幸的是,我们还没有可用的C++20,否则我们可以简单地实现spaceship操作符(
)…没关系,我们仍然可以使用普通函数代替,并在操作符中使用此函数:

int Time::compare(Time const& other) const
{
    // most relevant are hours, if these differ, values of minutes or
    // seconds don't matter any more...
    int result = hours - other.hours;
    if(result == 0)
    {
        // so hours are the same...
        // minutes then are relevant next
        result = minutes - other.minutes;
        if(result == 0)
            result = seconds - other.seconds;
    }
    return result;
}
那么,所有要实施的运营商将如下所示:

bool Time::operator@(Time const& other) const
{
    return compare(other) @ 0;
}
其中,
@
代表所需的所有操作员(
=
!=
=


奖励:记住没有单独标志的溢出:

你不需要再来一次