2014-09-19 79 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