将课程用于星期几

Using Classes for Day of the Week

本文关键字:用于 程用于      更新时间:2023-10-16

我有一个作业,本质上要求我使用一个类根据用户的输入输出星期几 EX:用户输入 sat(前三个字母(-->输出 = 一周中的这一天是: 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::D ayOfWeek 实例。 如果不是因为这几个颠簸,我觉得我快到了。

下面的代码应该可以满足你的一些需求。

我没有使用字符,而是使用了一个字符串。这样,您可以一次性比较整个字符串,而不是一次比较 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();
}