首页 > 解决方案 > 使用星期几的类

问题描述

我有一个任务,基本上要求我使用一个类来根据用户 EX 的输入输出星期几:用户输入sat(前三个字母)-> output = This day of the week is: 6。我们还需要程序在用户指​​定的日期之后输出接下来的六天,例如:

输入:周二

输出:一周中的这一天是:2 一周中的这一天是:3 一周中的这一天是:4 一周中的这一天是:5 一周中的这一天是:6 一周中的这一天是:7一周中的这一天是:1

我的程序有点混乱,因为我最初误解了这个问题。我能够开发一个功能正常的程序,允许用户输入当天的数字,但不能输入前三个字母,因为这部分代码似乎没有任何功能。

任何建议或解决方案将不胜感激。先感谢您。

#include <iostream>
#include <string>
using namespace std;

//
// Definition of the DayOfWeek class
//
class DayOfWeek
{
public:

    DayOfWeek(int dayNum);
    // Precondition: The parameter dayNum contains a valid
    // day number (1 - 7)
    // Postcondition: The member variable day has been set to
    // the value of the parameter dayNum.

    DayOfWeek();
    // Sets the member variable month to 1 (defaults to January).

    DayOfWeek(char fL, char sL, char tL);

    void outputDayNumber();
    // Postcondition: The member variable day has been output
    // to the screen if it is valid; otherwise a "not valid"
    // message is printed.

    void outputDayLetters();
    // Postcondition: The first three letters of the name of the
    // day has been output to the screen if the day is
    // valid (1 - 12); otherwise a message indicating the month
    // is not valid is output.

    DayOfWeek NextDay();
    // Precondition: The month is defined and valid.
    // Returns the next day as an object of type Day.

private:
    int day;
};


int main()
{
    //
    // Variable declarations
    //
    int DayNum;
    string DayWord;
    char letter1, letter2, letter3; // first 3 letters of the day
    char testAgain; // y or n - loop control

    //
    // Loop to test the next month function
    //
    do {

        cout << endl;
        cout << "Enter a day number: ";
        cin >> DayNum;

        DayOfWeek testDay(DayNum);
        cout << endl;
        cout << "This day ..." << endl;
        testDay.outputDayNumber();
        testDay.outputDayLetters();

        DayOfWeek next = testDay.NextDay();
        cout << endl;
        cout << "Next day ..." << endl;
        next.outputDayNumber();
        next.outputDayLetters();

        //
        // See if user wants to try another month
        //
        cout << endl;
        cout << "Do you want to test again? (y or n) ";
        cin >> testAgain;
    } while (testAgain == 'y' || testAgain == 'Y');

    return 0;
}


DayOfWeek::DayOfWeek()
{
    day = 1;
}


DayOfWeek::DayOfWeek(int dayNum)
{
    if (dayNum >= 1 && dayNum <= 7)
        day = dayNum;
    else {
        dayNum = 1;

    }

}

void DayOfWeek::outputDayNumber()
{
    if (day >= 1 && day <= 7)
        cout << "Day: " << day << endl;
    else
        cout << "Error - The day is not valid!" << endl;
}


void DayOfWeek::outputDayLetters()
{
    switch (day)
    {
    case 1:
        cout << "1" << endl;
        break;
    case 2:
        cout << "2" << endl;
        break;
    case 3:
        cout << "3" << endl;
        break;
    case 4:
        cout << "4" << endl;
        break;
    case 5:
        cout << "5" << endl;
        break;
    case 6:
        cout << "6" << endl;
        break;
    case 7:
        cout << "7" << endl;
        break;
    default:
        cout << "Error - the day is not valid!" << endl;
    }
}

DayOfWeek::DayOfWeek(char firstL, char secondL, char thirdL)
{
    /**
    *check to for the first characters or letter of the day
    *prints the day based on the characters user input
    *check if the characters is valid
    */
    if (firstL >= 65 && firstL <= 90) {
        firstL += 32;
    }
    if (secondL >= 65 && secondL <= 90) {
        secondL += 32;
    }
    if (thirdL >= 65 && thirdL <= 90) {
        thirdL += 32;
    }

    if (firstL == 'm' && secondL == 'o' && thirdL == 'n') {
        day = 1;
    }
    else if (firstL == 't' && secondL == 'u' && thirdL == 'e') {
        day = 2;
    }
    else if (firstL == 'w' && secondL == 'e' && thirdL == 'd') {
        day = 3;
    }
    else if (firstL == 't' && secondL == 'h' && thirdL == 'u') {
        day = 4;
    }
    else if (firstL == 'f' && secondL == 'r' && thirdL == 'i') {
        day = 5;
    }
    else if (firstL == 's' && secondL == 'a' && thirdL == 't') {
        day = 6;
    }
    else if (firstL == 's' && secondL == 'u' && thirdL == 'n') {
        day = 7;
    }
    else {
        day = 0;
        cout << "Invalid Day" << endl;
    }
}

DayOfWeek DayOfWeek::NextDay() {
    int d = (day % 7) + 1;
    return DayOfWeek(d);
}

更新:完全改变了我的代码,但仍然需要实现第二天和接下来的 6 天功能。新代码:

#include <iostream>
#include <string>
using namespace std;

class DayOfWeek
{
    string day;

    public:
        DayOfWeek();
        DayOfWeek(string day_name);
        void print_car(ostream& ins);
};


int main() {
    string day_name;
    cout << "Enter the first three letters of the day :" << endl;
    cin >> day_name;
    DayOfWeek my_car(day_name);
    my_car.print_car(cout);

    return 0;
}


DayOfWeek::DayOfWeek(string day_name) {
    day = day_name;
}

void DayOfWeek::print_car(ostream& outs) {
    if (day == "Mon" || day == "mon") {
        outs << "This is day 1 of the week" << endl;
    }
    if (day == "Tue" || day == "tue") {
        outs << "This is day 2 of the week" << endl;
    }
    if (day == "Wed" || day == "wed") {
        outs << "This is day 3 of the week" << endl;
    }
    if (day == "Thu" || day == "thu") {
        outs << "This is day 4 of the week" << endl;
    }
    if (day == "Fri" || day == "fri") {
        outs << "This is day 5 of the week" << endl;
    }
    if (day == "Sat" || day == "sat") {
        outs << "This is day 6 of the week" << endl;
    }
    if (day == "Sun" || day == "sun") {
        outs << "This is day 7 of the week" << endl;
    }   

}

最终更新:

我几乎是最终解决方案,但我在为我的问题的最后部分声明一个新对象时遇到问题。

代码在这里...

#include <iostream>
#include <string>
using namespace std;

class DayOfWeek
{
    string day;
//  int nday;

public:
    DayOfWeek();
    DayOfWeek(string day_name);

    void print_day(ostream& ins);
};


int main() {
    string day_name;
    int nday;
    cout << "Enter the first three letters of the day :" << endl;
    cin >> day_name;
    DayOfWeek week_day(day_name);
    week_day.print_day(cout);

    DayOfWeek next_day;
    int d = _____;
    int current = (d % 7) + 1;
    int count = 0;

    while (count < 7) {
        next_day = DayOfWeek(current);
        next_day.print_day(cout);
        count++;
    }

    return 0;
}

DayOfWeek::DayOfWeek(string day_name) {
    day = day_name;
}

void DayOfWeek::print_day(ostream& outs) {
    int dayNumber = 0;
    if (day == "Mon" || day == "mon") {
        dayNumber = 1;
    }
    if (day == "Tue" || day == "tue") {
        dayNumber = 2;
    }
    if (day == "Wed" || day == "wed") {
        dayNumber = 3;
    }
    if (day == "Thu" || day == "thu") {
        dayNumber = 4;
    }
    if (day == "Fri" || day == "fri") {
        dayNumber = 5;
    }
    if (day == "Sat" || day == "sat") {
        dayNumber = 6;
    }
    if (day == "Sun" || day == "sun") {
        dayNumber = 7;
    }

    outs << "This is day " << dayNumber << " of the week." << endl;

}

我有一些问题能够将用户输入的日期名称转换为可以用来插入 next_day 函数的整数。我在while循环中也有错误

while (count < 7) {
        next_day = DayOfWeek(current);
        next_day.print_day(cout);
        count++;
    }

具体线路:

next_day = DayOfWeek(current);

该错误表明没有与参数列表匹配的 DayOfWeek::DayOfWeek 实例。如果不是因为这几个颠簸,我觉得我快到了。

标签: c++class

解决方案


下面的代码应该做一些你需要的。

我没有使用字符,而是使用了字符串。这样,您可以一次比较整个字符串,而不是一次比较 1 个字符。

此外,为了将输入与日词进行比较,我创建了一个字符串数组。该索引对应于日期,即 0=星期日,1=星期一等。

这有两个原因,我们可以轻松地与循环进行比较,一旦我们找到匹配的日期,我们就抓住索引,我们就有了我们的天数。存储该数字,然后我们可以将其作为日期编号返回,或者将日期用作数组中的索引来返回日期单词。

希望这很清楚,您仍然需要创建一个函数以在第二天返回,但是 3 个字母输入应该可以工作。

#include <iostream>
#include <cctype>     // std::tolower
#include <algorithm>  // std::transform


class DayOfWeek
{
private:
    const static std::string days[7];
private:
    int day;
public:
    DayOfWeek();
    DayOfWeek(int);
    DayOfWeek(std::string);

    void outputDayNumber();

    void outputDayLetters();

};

// if we have an array of strings, we can use these for comparison and also Day value
const std::string DayOfWeek::days[7] = {"sun", "mon", "tue", "wed", "thu", "fri", "sat"};

DayOfWeek::DayOfWeek() {

}

DayOfWeek::DayOfWeek(int dayNum)
{
    day = dayNum;
}

DayOfWeek::DayOfWeek(std::string dayWord)
{
    day = 0; // set Sunday as default

    // truncate the string to 3 letters
    if (dayWord.length() > 3)
        dayWord = dayWord.substr(0, 3);

    // to lower case - makes comparison case insensitive
    std::transform(dayWord.begin(), dayWord.end(), dayWord.begin(), [](unsigned char c) { return std::tolower(c);});

    // compare with our list of strings, if none found, it will stay as default
    for (int i = 0; i < 7; ++i)
        if (days[i] == dayWord)
        {
            day = i;
            break;
        }
}

void DayOfWeek::outputDayLetters() {
    std::cout << std::endl << "Day Letters: " << days[day];
}

void DayOfWeek::outputDayNumber() {
    std::cout << std::endl << "Day Numbers: " << day;
}



int main()
{
    int dayNum;
    std::string dayWord;

    std::cout << std::endl << "Enter a weekday: ";
    std::cin >> dayWord;

    std::cout << std::endl << "You entered " << dayWord;

    DayOfWeek dayObject = DayOfWeek(dayWord);
    dayObject.outputDayNumber();
    dayObject.outputDayLetters();
}

推荐阅读