【问题标题】:Output two time values in ascending order按升序输出两个时间值
【发布时间】:2013-12-10 19:58:03
【问题描述】:

在提示用户输入两个时间值后,我在按升序输出一个类的两个对象(t1 和 t2)时遇到了一些问题。我知道 bool 和 if 构造的结构方式存在一些错误。任何帮助将不胜感激!

bool lessthan(Time t2) //For two Time objects t1 and t2, t1.lessthan(t2) returns true if t1 is less than, or comes before t2.
{
    if (hours < t2.hours)
    {
        return true;
    }

    if (minutes < t2.minutes)
    {
        return true;
    }

    if (seconds < t2.seconds)
    {
        return true;
    }

    return false;
}


bool greaterthan(Time t2)  //For two Time objects t1 and t2, t1.greaterthan(t2) returns true if t1 is greater than, or comes after t2.
{
    if (hours > t2.hours)
    {
        return true;
    }

    if (minutes > t2.minutes)
    {
        return true;
    }

    if (seconds > t2.seconds)
    {
        return true;
    }

    return false;
}


bool equalto(Time t2) //For two Time objects t1 and t2, t1.equalto(t2) returns true if t1 is equal to, or is the same time as t2.
{
    if (hours == t2.hours)
    {
        return true;
    }

    if (minutes == t2.minutes)
    {
        return true;
    }

    if (seconds == t2.seconds)
    {
        return true;
    }

    return false;
}

在主要功能中,我有以下代码:

 cout << "\nTime values entered in ascending order: "<<endl;
        if (t1.lessthan(t2))
            t1.write();
        cout << endl;
            t2.write();
        cout << endl;

【问题讨论】:

    标签: c++ class time


    【解决方案1】:

    我认为同级应该是

    bool equalto(Time t2) //For two Time objects t1 and t2, t1.equalto(t2) returns true if t1 is equal to, or is the same time as t2.
    {
        if (hours != t2.hours)
        {
            return false;
        }
    
        if (minutes != t2.minutes)
        {
            return false;
        }
    
        if (seconds != t2.seconds)
        {
            return false;
        }
    
        return true;
    }
    

    在您的代码中,如果小时数相同,与分钟和秒数无关,就足够了。在我的版本中,我颠倒了逻辑顺序。

    【讨论】:

    • 非常感谢您的意见!但是它仍然无法正常运行。此外,如果我输入两个以相同数字开头的值(例如 08:35:45 后跟 05:32:56),它只会打印出后者。
    • 您是否在问题中显示了完整的输出代码?因为t1.write() 只会在 t1 更小时被调用,t2.write() 在任何情况下都会被调用。
    【解决方案2】:

    对于您的比较函数,请尝试:

    bool equalto(Time t2) //For two Time objects t1 and t2, t1.equalto(t2) returns true if t1 is equal to, or is the same time as t2.
    {
        if (hours == t2.hours && minutes == t2.minutes && seconds == t2.seconds)
        {
            return true;
        }
    
        return false;
    }
    
    bool lessthan(Time t2) //For two Time objects t1 and t2, t1.lessthan(t2) returns true if t1 is less than, or comes before t2.
    {
        if (hours > t2.hours)
        {
            return false;
        }
    
        if (minutes > t2.minutes)
        {
            return false;
        }
    
        if (seconds > t2.seconds)
        {
            return false;
        }
    
        return true;
    }
    

    然后就其他两个函数实现大于()

    greaterthan(Time t2)
    {
        return (!(equalto(t2) || lessthan(t2));
    }
    

    此外,如果您想按升序输出时间,则需要执行以下操作:

    cout << "\nTime values entered in ascending order: "<<endl;
    if (t1.lessthan(t2))
    {
        t1.write();
        cout << endl;
        t2.write();
        cout << endl;
    }
    else
    {
        t2.write();
        cout << endl;
        t1.write();
        cout << endl;
    }
    

    对于您当前的代码,将始终执行最后 3 行,因为它们与该 if 语句无关。你需要括号。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-02-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多