2014-09-19 81 views
-16

Whats up world!我剛剛被介紹到C++的功能,但我有一個工作程序(完全運行在main(){})並將其分解成可用函數的問題。我認爲這是令我困惑的cin。首先是我的原始工作代碼,然後我正在嘗試使用它。C++:從main創建函數()

#include <iostream> 
#include <iomanip> 

using namespace std; 

int main() 
{ 
    int year, month, day; 
    char c; 
    bool validInput = false; 
    while ((cin) && !validInput) 
    { 
     cout << "Enter a date in the form YYYY-MM-DD: " << flush; 
     cin >> year >> c >> month >> c >> day; 

     // Check to see if this is a valid date 

     // The Gregorian calendar began On Oct 15, 1582. Earlier dates 
     // are invalid. 
     if (year < 1582) 
      validInput= false; 
     else if (year == 1582 && month < 10) 
      validInput = false; 
     else if (year == 1582 && month == 10 && day < 15) 
      validInput = false; 

     // Months must be in the range 1..12 
     else if (month < 1 || month > 12) 
      validInput = false; 

     // Days must be in the range 1..K where K is the number of 
     // days in that month. 
     else 
     { 
      int numberOfDaysInMonth = 0; 
      switch (month) 
      { 
      case 1: 
      case 3: 
      case 5: 
      case 7: 
      case 8: 
      case 10: 
      case 12: 
       numberOfDaysInMonth = 31; 
       break; 

      case 4: 
      case 6: 
      case 9: 
      case 11: 
       numberOfDaysInMonth = 30; 
       break; 

      case 2: 
       if (((year % 4 == 0) && (year % 100 != 0)) 
         || (year % 400 == 0)) 
        numberOfDaysInMonth = 29; 
       else 
        numberOfDaysInMonth = 28; 
      } 

      if (day < 1 || day > numberOfDaysInMonth) 
       validInput = false; 
      else 
       validInput = true; 
     } 

     if (!validInput) 
     { 
      cout << "Sorry, that is not a valid date" << endl; 
      string garbage; 
      getline (cin, garbage); // discard the rest ofthe input line 
     } 
    } 
    if (!cin) 
    { 
     cout << "Could not obtain valid input." << endl; 
     return -1; 
    } 


    // Input is valid - compute the day number 


    // Add up the number of days in all earlier months 
    // of this year 
    int sum = 0; 
    for (int m = 1; m < month; ++m) 
    { 
     int monthLength = 0; 
     switch (m) 
     { 
     case 1: 
     case 3: 
     case 5: 
     case 7: 
     case 8: 
     case 10: 
     case 12: 
      monthLength = 31; 
      break; 

     case 4: 
     case 6: 
     case 9: 
     case 11: 
      monthLength = 30; 
      break; 

     case 2: 
      if (((year % 4 == 0) && (year % 100 != 0)) 
        || (year % 400 == 0)) 
       monthLength = 29; 
      else 
       monthLength = 28; 
     } 

     sum += monthLength; 
    } 

    // Then add the day number to that sum 
    int dayNum = sum + day; 

    cout << setw(2) << setfill('0') << month 
     << "/" << setw(2) << setfill('0') << day << "/" 
     << setw(4) << year; 
    cout << " is day #" << dayNum << " of that year." << endl; 

    return 0; 
} 
+0

對於初學者來說,兩個開關都可以做成各自的功能。 – Medinoc 2014-09-19 15:20:47

+0

請在下面檢查我想如何解決問題,對不起,我沒有指定。我想把它分成四個函數:numberOfDaysInMonth(),dateIsValid(),isALeapYear(),dayOfTheYear()。 – YumBerrys 2014-09-19 15:27:28

回答

3

好的,我不打算在這裏重寫你的程序,而只是骨架。

主要功能是將會是什麼樣的:

int main() 
{ 
    int year, month, day; 
    bool validInput = false; 
    while ((cin) && !validInput) 
    { 
     cout << "Enter a date in the form YYYY-MM-DD: " << flush; 
     cin >> year >> c >> month >> c >> day; 

     validInput= dateIsValid(year, month, day); 
     if (!validInput) 
     { 
      cout << "Sorry, that is not a valid date" << endl; 
      string garbage; 
      getline (cin, garbage); // discard the rest ofthe input line 
     } 
     else 
     { 
      int numberOfDaysInMonth = daysInMonth(month); 
      cout << "There are " << numberOfDaysInMonth << " days in this month.\n"; 

      //... 
     } 
    } 

    return 0; 
} 

注意函數調用,調用傳遞它需要的括號內,然後如果該函數返回一個值,你可以在所有參數的功能它分配給一個變量,後來就用它來輸出...

的功能將類似於:

bool dateIsValid(const int year, const int month, const int day) 
{ 
    bool isValid = true; 
    //all the ifs and stuff here 
    // if (date is invalid) 
    //  isValid = false; 

    return isValid; 
} 

注意,我們必須明確地retur在C/C++編譯器中,該值不會使用與函數名稱相同的變量作爲返回值。

我希望這會有所幫助。

+0

這是一個了不起的答案。我現在正在改寫它。返回類型是我完全錯過的。謝謝! – YumBerrys 2014-09-19 16:38:13

+0

非常歡迎。正如一個註釋,看看[這裏](http://www.cplusplus.com/doc/tutorial/functions/)的一些功能教程,以及如何在C++中使用它們。這只是本教程的功能部分,您可能需要檢查其他部分,以及您掌握功能的時間。除了簡單的C++函數調用外,還有很多其他的功能。 – 2014-09-19 18:33:09