2016-09-19 163 views
0

所以這就是我想實現簡單的C++計算星期幾,有點卡住了?

enter image description here

這是我的代碼。我現在完全陷入困境,我在這裏錯過了什麼,那正好在我面前?我覺得我有所有的功能,但我不認爲它會正確計算它。我在這裏有一些錯誤,我無法修復甚至試圖運行它。 謝謝你的任何幫助; -

#include <iostream> 
#include <cmath> 
#include <string> 

using namespace std; 

class dayType 
{ 
public: 
    void setDay(int dayNum); 
    // set the day with the dayNum as parameter 
    void printd(dayNum); 
    // print the dayNum 
    int returnDay(); 
    // return the day of the week 
    void dayAfter(); 
    // return next day 
    void dayBefore(); 
    // return previous day 
    void randomDay(int dayNum); 
    // function to return a day after a certain number of days 


    dayType(int dayNum); 
    // Constructor with parameters setting dayNum according to parameters 
    dayType(); 
    //Default constructor 

    private: 
     int today; 
     int yest; 
     int tom; 
     int dayN; 

    }; 




    void dayType::printd(int dayNum) 
    { 
     if (dayNum == 1) 
      cout << "Monday" << endl; 

     if (dayNum == 2) 
      cout << "Tuesday" << endl; 

     if (dayNum == 3) 
      cout << "Wednesday" << endl; 

     if (dayNum == 4) 
      cout << "Thursday" << endl; 

     if (dayNum == 5) 
      cout << "Friday" << endl; 

     if (dayNum == 6) 
      cout << "Saturday" << endl; 

     if (dayNum == 7) 
      cout << "Sunday" << endl; 

    } 


    void dayType::setDay(int dayNum) 
    { 
    today = dayNum; 

    }; 

    int dayType::returnDay() 
    { 
     return today; 

    }; 

    void dayType::printd(<#int dayNum#>); 
    { 
    cout << "The current day is: " << today << endl; 
    } 


    void dayType::dayBefore() 
    { 
     if(today == 0) 
      yest = 6; 
     else today--; 
    }; 


    void dayType::dayAfter() 
    { 
     if(today == 6) 
     tom = 0; 

    }; 

    void dayType::randomDay(int dayNum) 
    { 
     dayN=(today+dayNum); 
     today =(dayN%7); 

    }; 

    dayType::dayType() 
    { 
     today = 0; 
    } 


    dayType::dayType(int daynum) 
    { 
    today = daynum; 
    } 
    // do I need these constructors here doing this? 



      int main() 

      { 
       int dayWeek; 

       cout << "Please enter a number for the day of the week: " << endl; 
       cout << "1 - Monday" << endl; 
       cout << "2 - Tuesday" << endl; 
       cout << "3 - Wednesday" << endl; 
       cout << "4 - Thursday" << endl; 
       cout << "5 - Friday" << endl; 
       cout << "6 - Saturday" << endl; 
       cout << "7 - Sunday" << endl; 

       while (dayWeek<= 7) 

        cin >> dayWeek; 

       dayType thisDay; 

       cout << "Today is: "; 
       thisDay.returnDay(); 
       thisDay.printd(int dayNum); 


       cout << "Yesterday was: "; 
       thisDay.dayBefore(); 
       thisDay.printd(int dayNum); 


       cout << "Tomorrow is: "; 
       thisDay.dayAfter(); 
       thisDay.printd(int dayNum); 

       cout << "Type a number of days from today and it will be: "; 
       thisDay.randomDay(dayNum); 

       return 0; 
      }; 
     } 
+0

嗨Croset!我在代碼中看到了一些錯誤。但首先,你的問題是什麼?你有錯誤嗎?行爲不是你所期望的嗎? – Dylan

+0

嗨迪倫,老實說,我覺得我有那裏的功能,但我不認爲它會按照我的計劃工作。不確定我的打印是否正確。我首先收到一些基本的語法錯誤,所以我不能運行程序來檢查 - 真的可以感謝任何幫助,以瞭解我出錯的地方!謝謝:) – Croset

+0

在你的書中閱讀更多有關*返回*函數中的值以及如何調用函數的內容。 (你還沒有完成XCode中'printd'參數的定義。) – molbdnilo

回答

1

你的代碼應該是更多這樣的:

#include <iostream> 
#include <cmath> 
#include <string> 

using namespace std; 

class dayType 
{ 
public: 
    void setDay(int dayNum); 
    void printd(); 
    // set the day with the dayNum as parameter 
    void printd(int dayNum); 
    // print the dayNum 
    int returnDay(); 
    // return the day of the week 
    int dayAfter(); 
    // return next day 
    int dayBefore(); 
    // return previous day 
    int randomDay(int dayNum); 
    // function to return a day after a certain number of days 


    dayType(int dayNum); 
    // Constructor with parameters setting dayNum according to parameters 
    dayType(); 
    //Default constructor 

    private: 
     int today; 

}; 




void dayType::printd(int dayNum) 
{ 
    if (dayNum == 1) 
     cout << "Monday" << endl; 

    if (dayNum == 2) 
     cout << "Tuesday" << endl; 

    if (dayNum == 3) 
     cout << "Wednesday" << endl; 

    if (dayNum == 4) 
     cout << "Thursday" << endl; 

    if (dayNum == 5) 
     cout << "Friday" << endl; 

    if (dayNum == 6) 
     cout << "Saturday" << endl; 

    if (dayNum == 7) 
     cout << "Sunday" << endl; 

} 


void dayType::setDay(int dayNum) 
{ 
    today = dayNum; 

} 

int dayType::returnDay() 
{ 
    return today; 

} 

void dayType::printd() 
{ 
cout << "The current day is: " << today << endl; 
} 


int dayType::dayBefore() 
{ 
    int yest; 
    if(today == 0) 
     yest = 6; 
    else 
     yest = today - 1; 
    return yest; 
}; 


int dayType::dayAfter() 
{ 
    int tom; 

    if(today == 6) 
     tom = 0; 
    else 
     tom = today + 1; 
    return tom; 

}; 

int dayType::randomDay(int dayNum) 
{ 
    int dayN = (today+dayNum); 
    return (dayN % 7) 

}; 

dayType::dayType() 
{ 
    today = 0; 
} 


dayType::dayType(int daynum) 
{ 
    today = daynum; 
} 
// do I need these constructors here doing this? 



int main() 

{ 
    int dayWeek = -1; 
    int random = -1; 

    cout << "Please enter a number for the day of the week: " << endl; 
    cout << "1 - Monday" << endl; 
    cout << "2 - Tuesday" << endl; 
    cout << "3 - Wednesday" << endl; 
    cout << "4 - Thursday" << endl; 
    cout << "5 - Friday" << endl; 
    cout << "6 - Saturday" << endl; 
    cout << "7 - Sunday" << endl; 

    while (dayWeek >= 7 || dayWeek < 0) 

     cin >> dayWeek; 

    dayType thisDay; 
    thisDay.today = dayWeek; 

    thisDay.printd(); 


    cout << "Yesterday was: "; 
    thisDay.printd(thisDay.dayBefore()); 


    cout << "Tomorrow is: "; 
    thisDay.printd(thisDay.dayAfter()); 

    cout << "Type a number of days : "; 

    while (random < 0) 

     cin >> random; 

    cout << "Now we are "; 
    thisDay.printd(thisDay.randomDay(random)); 

    return 0; 
} 

當你調用一個函數是這樣的:void fonction(int a, int b),你可以這樣調用:fonction(1, 2);。您不必編寫參數的類型。在你的課堂上,只聲明你需要的功能值。在您之前的代碼中,您聲明tom,yesdayN,但您不需要保留其值。

我沒有測試代碼,所以如果你有錯誤,寫下來,我會編輯這篇文章。

+0

謝謝你的幫助!我有幾個錯誤 - 在這裏脫機聲明 'void dayType :: printd(); {' 和這裏 'thisDay.printd();'似乎不喜歡printD?說太少的函數調用和dayNum沒有指定? – Croset

+0

你只需要聲明void'dayType :: printd()'。我忘了刪除;實施後。 – Dylan

+0

AHHHHHH GOTCHA。這現在有道理。謝謝你:) – Croset

1

就個人而言,我建議使用enum而不是int跟蹤當天,以及必要的操作員讓您循環日子。這將允許您更輕鬆地定義和與dayType進行交互,輕鬆解決您遇到的錯誤。

#include <iostream> 
#include <cmath> 
#include <string> 
#include <limits> 

using namespace std; 

class dayType 
{ 
public: 
    // Days enum: 
    enum Days { 
     D_MONDAY = 1, 
     D_TUESDAY, 
     D_WEDNESDAY, 
     D_THURSDAY, 
     D_FRIDAY, 
     D_SATURDAY, 
     D_SUNDAY 
    }; 

    void setDay(int dayNum); 
    // set the day with the dayNum as parameter 

    void printd(int dayNum); 
    // print the dayNum 
    void printd(); 
    // Prints the current day. 

    Days returnDay(); 
    // return the day of the week 
    Days dayAfter(); 
    // return next day 
    Days dayBefore(); 
    // return previous day 
    Days randomDay(int dayNum); 
    // function to return a day after a certain number of days 


    dayType(int dayNum); 
    // Constructor with parameters setting dayNum according to parameters 
    dayType(); 
    //Default constructor 

private: 
    Days today; 
}; 

// Compound assignment addition: 
dayType::Days& operator+=(dayType::Days& left, int right) 
{ 
    typedef dayType::Days Days; 

    int temp = static_cast<int>(left) + right; 

    // In case of adding negative numbers. 
    while (temp < dayType::D_MONDAY) 
     temp += dayType::D_SUNDAY; 

    while (temp > dayType::D_SUNDAY) 
     temp -= dayType::D_SUNDAY; 

    left = static_cast<Days>(temp); 

    return left; 
} 

// Compound assignment subtraction: 
dayType::Days& operator-=(dayType::Days& left, int right) 
{ 
    typedef dayType::Days Days; 

    int temp = static_cast<int>(left) - right; 

    while (temp < dayType::D_MONDAY) 
     temp += dayType::D_SUNDAY; 

    // In case of subtracting negative numbers. 
    while (temp > dayType::D_SUNDAY) 
     temp -= dayType::D_SUNDAY; 

    left = static_cast<Days>(temp); 

    return left; 
} 


// Addition. Uses compound assignment addition internally. 
dayType::Days operator+(dayType::Days left, int right) 
{ 
    return left += right; 
} 

// Subtraction. Uses compound assignment subtraction internally. 
dayType::Days operator-(dayType::Days left, int right) 
{ 
    return left -= right; 
} 


// Prefix increment (++day): 
dayType::Days operator++(dayType::Days& day) 
{ 
    return day += 1; 
} 

// Postfix increment (day++): 
dayType::Days operator++(dayType::Days& day, int) 
{ 
    typedef dayType::Days Days; 

    Days temp = day; 
    ++day; 
    return temp; 
} 


// Prefix decrement (--day): 
dayType::Days operator--(dayType::Days& day) 
{ 
    return day -= 1; 
} 

// Postfix decrement (day--): 
dayType::Days operator--(dayType::Days& day, int) 
{ 
    typedef dayType::Days Days; 

    Days temp = day; 
    --day; 
    return temp; 
} 


void dayType::printd(int dayNum) 
{ 
    if (dayNum == D_MONDAY) 
     cout << "Monday" << endl; 

    if (dayNum == D_TUESDAY) 
     cout << "Tuesday" << endl; 

    if (dayNum == D_WEDNESDAY) 
     cout << "Wednesday" << endl; 

    if (dayNum == D_THURSDAY) 
     cout << "Thursday" << endl; 

    if (dayNum == D_FRIDAY) 
     cout << "Friday" << endl; 

    if (dayNum == D_SATURDAY) 
     cout << "Saturday" << endl; 

    if (dayNum == D_SUNDAY) 
     cout << "Sunday" << endl; 
} 

void dayType::printd() 
{ 
    cout << "The current day is: "; 
    printd(today); 
} 


void dayType::setDay(int dayNum) 
{ 
    today = static_cast<Days>(dayNum); 
}; 

dayType::Days dayType::returnDay() 
{ 
    return today; 
}; 

dayType::Days dayType::dayBefore() 
{ 
    return today - 1; 
}; 


dayType::Days dayType::dayAfter() 
{ 
    return today + 1; 
}; 

dayType::Days dayType::randomDay(int dayNum) 
{ 
    return today + dayNum; 
}; 

dayType::dayType() 
{ 
    today = D_SUNDAY; 
} 


dayType::dayType(int daynum) 
{ 
    today = static_cast<Days>(daynum); 
} 



int main() 
{ 
    int dayWeek = 0; 

    cout << "Please enter a number for the day of the week: " << endl; 
    cout << "1 - Monday" << endl; 
    cout << "2 - Tuesday" << endl; 
    cout << "3 - Wednesday" << endl; 
    cout << "4 - Thursday" << endl; 
    cout << "5 - Friday" << endl; 
    cout << "6 - Saturday" << endl; 
    cout << "7 - Sunday" << endl; 

    while (!((dayWeek > 0) && (dayWeek < 8))) 
     cin >> dayWeek; 

    dayType thisDay(dayWeek); 

    // ----- 

    cout << "Today is: "; 
    thisDay.printd(thisDay.returnDay()); 

    // ----- 

    cout << "Yesterday was: "; 
    thisDay.printd(thisDay.dayBefore()); 

    // ----- 

    cout << "Tomorrow is: "; 
    thisDay.printd(thisDay.dayAfter()); 

    // ----- 

    cout << "Type a number of days: "; 

    int random; 
    cin >> random; 
    cin.ignore(std::numeric_limits<streamsize>::max(), '\n'); 

    cout << "In " << random << " days, it will be: "; 
    thisDay.printd(thisDay.randomDay(random)); 
}; 

目前,這應該爲你想要什麼,需要提醒的是,如果你想使用intDays(如初始化中dayType::dayType(int daynum)today),你必須明確地將它轉換成一。 (相反,Days可以隱式用作int,受dayType::returnDay()返回值傳遞給dayType::printd()作爲證明,或者如果你傳遞一個Dayscout。)

如果你想使用的dayTypeDays之外,你可以使用它作爲dayType::Days(例如在運營商的回報類型中)。您也可以使用typedef dayType::Days Days;將其帶入當前範圍,允許您將其作爲簡單的Days(如dayType::Days operator++(dayType::Days& day, int)dayType::Days operator--(dayType::Days& day, int))使用。同樣,要使用枚舉值本身,請在dayType::前加上它們的名稱。

當你在這裏使用這個練習和你的問題,作爲一個學習的機會,我留下了一些可以改進的地方。它目前正常工作,但有修改空間,可以使其更好地工作。

+0

@Croset不客氣。我只是稍微修改了'dayType :: printd()'(沒有參數的那個),所以它會打印當天的名字而不是底層的'int'值,並修正了我在代碼下面的第一段中犯的錯誤(我錯誤地指出,如果你真的可以傳遞它,而不需要投射,你明確需要將'Days'轉換爲'int'以將它傳遞給'std :: cout')。 –