首页 > 解决方案 > 麻烦开发简单的时钟和日期类

问题描述

我遇到了获得预期结果的问题。

我已经尝试了很多时间煎熬我的大脑,但它没有工作......我认为问题是 Date 类中的 void 函数的故障以及 Clock 类中的一些指针错误和 AdvanceTime 函数,但我自己觉得模棱两可进一步推进这一步。

我试图尽我所能描述这个问题,所以你能帮我吗?

预期的结果是这样的:

Setting the clock to 2:03:04 using normal setters
02:03:04
Advancing the clock one hour with a value parameter
02:03:04
Advancing the clock one hour with a pointer parameter
03:03:04
Advancing the clock one hour with a reference parameter
04:03:04
Setting the clock to 8:09:10 with cascading setters returning an instance
08:03:04
Setting the clock to 5:06:07 with cascading setters returning a pointer
05:06:07
Setting the clock to 11:12:13 with cascading setters returning a reference
11:12:13
Setting the clock to 23:59:59
23:59:59
Advancing the clock one second
00:00:00

但是,我得到的是:

Setting the clock to 2:03:04 using normal setters
02:03:04
Advancing the clock one hour with a value parameter
02:03:04
Advancing the clock one hour with a pointer parameter
03:03:04
Advancing the clock one hour with a reference parameter
04:03:04
Setting the clock to 8:09:10 with cascading setters returning an instance
08:03:04
Setting the clock to 5:06:07 with cascading setters returning a pointer
05:03:04
Setting the clock to 11:12:13 with cascading setters returning a reference
11:03:04
Setting the clock to 23:59:59
23:59:59
Advancing the clock one second
23:59:00

我试图在小时、分钟和秒内对剩余的东西进行一些评估。

好吧,我迷失了从 59 个边缘获得 00:00:00 的方式,而且,一些前进的时钟正在工作,但有些东西不起作用。

我实际上陷入了困惑。

时钟.h

// Clock.h
// This class describes a Clock.

class Clock {
    private:
        int hour;
        int minute;
        int second;
    public:
        // Constructor
        Clock();

        // Normal Setters
        void setHour(int newHour);
        void setMinute(int newMinute);
        void setSecond(int newSecond);

        // Cascading Setters returning an instance
        Clock setHourIns(int newHour);
        Clock setMinuteIns(int newMinute);
        Clock setSecondIns(int newSecond);

        // Cascading Setters returning a pointer
        Clock * setHourPtr(int newHour);
        Clock * setMinutePtr(int newMinute);
        Clock * setSecondPtr(int newSecond);

        // Cascading Setters returning a reference
        Clock & setHourRef(int newHour);
        Clock & setMinuteRef(int newMinute);
        Clock & setSecondRef(int newSecond);

        // Getters
        int getHour() const;
        int getMinute() const;
        int getSecond() const;

        // Advance one hour
        void advanceOneHour();
        void advanceOneMinute();
        void advanceOneSecond();

        // Printing Methods
        void printTwoDigits(int number) const;
        void printTime() const;
};

时钟.cpp

// Clock.cpp
// This class describes a Clock.

#include <iostream>
#include <iomanip>
#include "Clock.h"

using namespace std;

// Constructor for Clock
Clock::Clock() {
    hour = 0;
    minute = 0;
    second = 0;
}

// Normal Setters
void Clock::setHour(int newHour) {
    hour = newHour;
}

void Clock::setMinute(int newMinute) {
    minute = newMinute;
}

void Clock::setSecond(int newSecond) {
    second = newSecond;
}

// Cascading Setters which return an instance
Clock Clock::setHourIns(int newHour) {
    hour = newHour;
    return (*this);
}

Clock Clock::setMinuteIns(int newMinute) {
    minute = newMinute;
    return (*this);
}

Clock Clock::setSecondIns(int newSecond) {
    second = newSecond;
    return (*this);
}

// Cascading Setters which return an pointer
Clock * Clock::setHourPtr(int newHour) {
    hour = newHour;
    return this;
}

Clock * Clock::setMinutePtr(int newMinute) {
    minute = newMinute;
    return this;
}

Clock * Clock::setSecondPtr(int newSecond) {
    second = newSecond;
    return this;
}

// Cascading Setters which return a reference
Clock & Clock::setHourRef(int newHour) {
    hour = newHour;
    return (*this);
}

Clock & Clock::setMinuteRef(int newMinute) {
    minute = newMinute;
    return (*this);
}

Clock & Clock::setSecondRef(int newSecond) {
    second = newSecond;
    return (*this);
}

// Getters
int Clock::getHour() const {
    return hour;
}

int Clock::getMinute() const {
    return minute;
}

int Clock::getSecond() const {
    return second;
}

// Advance one hour
void Clock::advanceOneHour() {
    if((hour + 1) >= 24) {
        hour = 0;
    }
    hour = (hour + 1) % 24;
}

void Clock::advanceOneMinute() {
    if((minute + 1) >= 60) {
        minute = 0;
        hour++;
    }
    minute = (minute + 1) % 60;
}

void Clock::advanceOneSecond() {
    if((second + 1) >= 60) {
        second = 0;
        minute++;
    }
    second = (second + 1) % 60;
}

// Printing methods
void Clock::printTwoDigits(int number) const {
    cout << setw(2) << setfill('0') << number;
}

void Clock::printTime() const {
    printTwoDigits(hour); cout << ":";
    printTwoDigits(minute); cout << ":";
    printTwoDigits(second); cout << endl;
}

时钟主程序.cpp

// ClockMain.cpp

#include <iostream>
#include "Clock.h"

using namespace std;

// Advance the clock by one hour with a value parameter
void advanceOneHourVal(Clock myClock) {
    myClock.advanceOneHour();
}

// Advance the clock by one hour with a pointer parameter
void advanceOneHourPtr(Clock * myClock) {
    myClock->advanceOneHour();
}

// Advance the clock by one hour with a reference parameter
void advanceOneHourRef(Clock & myClock) {
    myClock.advanceOneHour();
}

int main() {
    Clock c1;

    cout << "Setting the clock to 2:03:04 using normal setters" << endl;
    c1.setHour(2); c1.setMinute(3); c1.setSecond(4); c1.printTime();

    cout << "Advancing the clock one hour with a value parameter" << endl;
    advanceOneHourVal(c1);
    c1.printTime();

    cout << "Advancing the clock one hour with a pointer parameter" << endl;
    advanceOneHourPtr(&c1);
    c1.printTime();

    cout << "Advancing the clock one hour with a reference parameter" << endl;
    advanceOneHourRef(c1);
    c1.printTime();

    cout << "Setting the clock to 8:09:10 with cascading setters returning an instance" << endl;
    c1.setHourIns(8).setMinuteIns(9).setSecondIns(10);
    c1.printTime();

    cout << "Setting the clock to 5:06:07 with cascading setters returning a pointer" << endl;
    c1.setHourIns(5).setMinuteIns(6).setSecondIns(7);
    c1.printTime();

    cout << "Setting the clock to 11:12:13 with cascading setters returning a reference" << endl;
    c1.setHourIns(11).setMinuteIns(12).setSecondIns(13);
    c1.printTime();

    cout << "Setting the clock to 23:59:59" << endl;
    c1.setHourRef(23).setMinuteRef(59).setSecondRef(59);
    c1.printTime();
    c1.advanceOneSecond();
    cout << "Advancing the clock one second" << endl;
    c1.printTime();
}

同样,我在 Date 类中也遇到了类似的错误。该错误表明在前进中返回无效函数的错误。

日期.cpp

// Date.cpp
// This class describes a Date.

#include <iostream>
#include <iomanip>
#include "Date.h"

using namespace std;

// Constructor for Clock
Date::Date() {
    year = 0;
    month = 0;
    day = 0;
}

// Normal Setters
void Date::setYear(int newYear) {
    year = newYear;
}

void Date::setMonth(int newMonth) {
    month = newMonth;
}

void Date::setDay(int newDay) {
    day = newDay;
}

// Cascading Setters which return an instance
Date Date::setYearIns(int newYear) {
    year = newYear;
    return (*this);
}

Date Date::setMonthIns(int newMonth) {
    month = newMonth;
    return (*this);
}

Date Date::setDayIns(int newDay) {
    day = newDay;
    return (*this);
}

// Cascading Setters which return an pointer
Date * Date::setYearPtr(int newYear) {
    year = newYear;
    return this;
}

Date * Date::setMonthPtr(int newMonth) {
    month = newMonth;
    return this;
}

Date * Date::setDayPtr(int newDay) {
    day = newDay;
    return this;
}

// Cascading Setters which return a reference
Date & Date::setYearRef(int newYear) {
    year = newYear;
    return (*this);
}

Date & Date::setMonthRef(int newMonth) {
    month = newMonth;
    return (*this);
}

Date & Date::setDayRef(int newDay) {
    day = newDay;
    return (*this);
}

// Getters
int Date::getYear() const {
    return year;
}

int Date::getMonth() const {
    return month;
}

int Date::getDay() const {
    return day;
}

// Advance one date
void Date::advanceOneYear() {
    year = year + 1;
}

void Date::advanceOneMonth() {
    if (month == 1 || month == 3 || month == 5 || month == 7 || month == 9 || month == 11) {
        month = (month + 1) % 31;
    } else if (month == 2) {
        month = (month + 1) % 28;
    } else {
        month = (month + 1) % 30;
    }
}

void Date::advanceOneDay() {
    day = (day + 1) % 365;
}

// Printing methods
void Date::printTwoDigits(int number) const {
    cout << setw(2) << setfill('0') << number;
}

void Date::printDate() const {
    printTwoDigits(year); cout << ":";
    printTwoDigits(month); cout << ":";
    printTwoDigits(day); cout << endl;
}

日期.h

// Date.h
// This class describes a Clock.

class Date {
    private:
        int day;
        int month;
        int year;
    public:
        // Constructor
        Date();

        // Normal Setters
        void setYear(int newYear);
        void setMonth(int newMonth);
        void setDay(int newDate);

        // Cascading Setters returning an instance
        Date setYearIns(int newYear);
        Date setMonthIns(int newMonth);
        Date setDayIns(int newDate);

        // Cascading Setters returning a pointer
        Date * setYearPtr(int newYear);
        Date * setMonthPtr(int newMonth);
        Date * setDayPtr(int newDate);

        // Cascading Setters returning a reference
        Date & setYearRef(int newYear);
        Date & setMonthRef(int newMonth);
        Date & setDayRef(int newDate);

        // Getters
        int getYear() const;
        int getMonth() const;
        int getDay() const;

        // Advance one hour
        void advanceOneYear();
        void advanceOneMonth();
        void advanceOneDay();

        // Printing Methods
        void printTwoDigits(int number) const;
        void printDate() const;
};

日期主程序.cpp

// DateMain.cpp

#include <iostream>
#include "Date.h"

using namespace std;

int main() {
    Date d1;

    cout << "Setting the date to 2018/02/17 using cascading setters" << endl;
    d1.setYear(2018).setMonth(2).setDay(17);
    d1.printDate();

    cout << "Advancing the date one month" << endl;
    advanceOneMonth(d1);
    d1.printDate();

    cout << "Happy St. Patrick's Day!!" << endl << endl;

    cout << "Setting the date to 12/31/2018 (US Style) using cascading setters" << endl;
    d1.setMonth(12).setDay(31).setYear(2018);
    d1.printDate();

    cout << "Advancing the date one day" << endl;
    advanceOneDay(d1);
    d1.printDate();

    cout << "Happy New Year!!" << endl;
}

标签: c++

解决方案


问题是你只看seconds变量。您需要检查是否有溢出。像这样的东西:

if((seconds +1) >= 60) {
    //advance one minute...
}

您也需要在时钟的其余时间(分钟、小时等)做类似的事情。


推荐阅读