为什么我会从我的职能部门收到一份会议声明

Why am I getting a meesed up cout statement from my function?

本文关键字:一份 声明 会议 我的 职能部门 为什么      更新时间:2023-10-16

嗨,我正在开发一个程序,该程序应该显示两个日期中的后一个,它需要接受两个Date结构并返回后一个。后者为2014年4月31日,而后者为2014年04月30日。这是我的代码,我不明白为什么我会得到一个奇怪的长数字。。。谢谢你的帮助。

#include <iostream>
#include <iomanip>
using namespace std;
struct Date
{
    int day;
    int month;
    int year;
};
int main()
{
    void laterDate(Date, Date);
    Date one;
    Date two;
Date later;
cout << "Enter a Date starting with the day, then month, then year:n";
cin >> one.day;
cin >> one.month;
cin >> one.year;
cout << "nnEnter another date in the same fashion as above:n";
cin >> two.day;
cin >> two.month;
    cin >> two.year;
cout << "nnThank you, I will now tell you which date is later then the other!" <<             endl;
laterDate(one, two);
system("pause");
return 0;
}

void laterDate(Date o,Date t)
{
Date later;

if (o.year >= t.year)
    if (o.month >= t.month)
        if (o.day > t.day)
        {
            later.day= o.day;
            later.month = o.month;
            later.year = o.year;
        }
        else
        {
            later.day = t.day;
            later.month = t.month;
            later.year = t.year;
        }
        cout << later.day << "/" << later.month << "/" << later.year << endl;
}

输出输入一个日期,从天开始,然后是月,然后是年:04302014年

以与上面相同的方式输入另一个日期:04312014年

谢谢你,我现在告诉你哪一天比另一天晚!-858993460/-858993460按任意键继续。

结束输出

您应该打破从显示结果的代码中比较日期的逻辑。比较可以完全排序的对象(如日期(的标准习惯用法是重载operator<

// the intent of this function is to return true if lhs precedes rhs,
// and return false otherwise (rhs precedes lhs, or they are equal)
bool operator<(Date const& lhs, Date const& rhs)
{
    // first test the year.
    // if the years differ, there is no need to test the month or the day
    if (lhs.year < rhs.year) return true;
    if (lhs.year > rhs.year) return false;
    // years are equal, test the month
    if (lhs.month < rhs.month) return true;
    if (lhs.month > rhs.month) return false;
    // months are equal, test the day
    return lhs.day < rhs.day;
}

然后你的函数可以很容易地写成这样:

void laterDate(Date const& lhs, Date const& rhs)
{
    Date const& later = (lhs < rhs) ? rhs : lhs;
    cout << later.day << "/" << later.month << "/" << later.year << endl;
}
if (o.year > t.year || 
   (o.year >= t.year && o.month > t.month) || 
    (o.year >= t.year && o.month >= t.month &&
           o.day > t.day))
        {
            later.day= o.day;
            later.month = o.month;
            later.year = o.year;
        }
        else
        {
            later.day = t.day;
            later.month = t.month;
            later.year = t.year;
        }

通过这种方式,else将始终被调用,在您的实现中,它只在两个第一个条件为true时被调用,导致显示随机位

我想我的结构会有所不同。首先,我将输出Date作为Date类的友元函数,然后,我将重载>运算符来比较Date类实例。

struct Date
{
    int day;
    int month;
    int year;
    friend std::ostream &operator<<(std::ostream& out, const Date &d) {
        return out << d.day << "/" << d.month << "/" << d.year;
    }
    bool operator>(const Date &t) const;
};

>运算符的实现如下所示:

bool Date::operator>(const Date &t) const
{
    if (year > t.year) return true;
    if (year < t.year) return false;
    if (month > t.month) return true;
    if (month < t.month) return false;
    return day > t.day;
}

然后你的main程序是这样的:

int main()
{
    Date one;
    Date two;
    std::cout << "Enter a Date starting with the day, then month, then year:n";
    std::cin >> one.day >> one.month >> one.year;
    std::cout << "nnEnter another date in the same fashion as above:n";
    std::cin >> two.day >> two.month >> two.year;
    std::cout << "nnThank you: " << (one > two ? one : two) 
              << " is the later daten";
}

同样要注意的是,可以将输入操作符部分链接起来,如图所示。事实上,我倾向于声明一个输入运算符operator>>,并将Date结构提升为一个完整的类。