0

こんにちは、誰かが私が間違っていることを手伝ってくれることを願っています。これは私が指摘する宿題ですが、私が間違っていることが何であるかを理解していないので、私がなんてバカなのか誰かに教えてもらう必要がありました.

ユーザーが月、日、年を入力して、それが魔法の日付かどうかを判断できるようにすることになっています。その部分は、別のプロジェクトですでに行っていました。戻って、isDateValid という名前の関数を使用して日付を検証することになっています。明らかな何かが欠けていると確信しており、この件について誰の助けも借りることができます。ありがとう!

問題は、私がそれを実行すると、それが無効な日付であることが常に得られることです!

 #include <iostream>
 using namespace std;

 bool leapYear(int);
 bool isDateValid(int, int, int);

 int main()
 {
 int month, day, year;

 cout << "Is the date you are thinking of a magic one? Let's see...\n";
 cout << "What is the month in numeric form only? "; cin >> month;
 cout << "What is the day of the month? "; cin >> day;
 cout << "What is the four digit year? "; cin >> year;

 if (isDateValid(month, day, year) == false)
 {
 cout << "The date you entered, "<< month << "/" << day << "/" << 
 year << " is NOT a VALID Date.\n";
 cout << "If you think I am wrong, please feel free to run me again";
 }

 else   if (month * day == year % 100) // if true then do this
 {
 cout << "The date you entered, " << 
 month << "/" << day << "/" << year << " is a magic date!!\n"; 
 }
 else // if false then do this
 {
 cout << "The date you entered, " << month << "/" << day << "/" <<
 year << " is NOT a magic date!!\n";
 }

                                                            //Program ID
 cout <<"\n" << "L5P1LB.cpp\n" << "11-26-12\n" << endl;
 system ("pause");
 return 0;
 }//end main

 // isDateValid function to validate date entered
 bool isDateValid(int month, int day, int year)
 {
 bool validation = true;
 if(!(year >= 1600 && year <=2100))
     validation = false;    
 if(leapYear(year))
 {
     if ((month == 2) && (day > 29))
         validation = false;
     }
 else if(!leapYear(year))
 {
      if ((month == 2) && (day > 28))
          validation = false;
          }    
 if((month < 1 && month > 12) || (day < 1))
     validation = false;
 if((month == 1) || (month ==3) || (month == 5) || (month == 7) ||
     (month == 8) || (month == 10) || (month == 12) && (day > 31))
     validation = false;
 else if (((month == 4) || (month == 6) || (month == 9) || (month == 11)) && 
      (day > 30))
      validation = false;
 else
      validation == true;
 return validation;
 }
 // leapYear function to determine if the year is a leap year for validation
 bool leapYear(int year)
 {
 return(year % 100 != 0 && year % 4 == 0) || (year % 400 == 0);
 } // end leapYear

編集

さて、私の問題は、それが有効な日付ではないことに気付いていないことです。検証をすべてスキップして、その「魔法の日付」部分を実行するだけです!

ここに書き直したものと、Bloodshed にあるように適切にインデントされていることを願っている私の完全なコードがあります。

Description: This program determines if when the user enters a month, 
day, and four digit year which will then be stripped down to its
two digit year and determined to be a magic date if the day entered 
multiplied by the month entered equals the years two digit format.
Design:
    begin main
        input data
            Ask user for a month in number form
            Ask user for a day of the month
            Ask user for four digit month
        Validate
            begin isDateValid
                if year is between 1600 and 2100, inclusive then return 
                    true
                if month is between 1 and 12, inclusive return true
                if month is Jan, Mar, May, July, Aug, Oct, Dec 
                   then if day is between 1 and 31 inclusive return true
                if month is Apr, June, Sept, Nov
                   then if day is between 1 and 30 inclusive return true
                if month is Feb
                   then determine if leap year by running function 
                   leapYear provided for us.
                   if leap year is true 
                   then if day is between 1 and 29, inclusive return true
                   if not leap year then if day is between 1 and 28, 
                   inclusive then return true
            end isDateValid
        if isDateValid is true then 
        Calculate
            if the month times the year is equal to the year modulus 100
            then output it is a magic date
            if it does not equal the year in 2 digit form
            then output that it is not a magic date    
        else output error message

        Output Program ID
    end main

    The output needs to include the books data of 6/10/1960 */
#include <iostream>
using namespace std;

bool leapYear(int);
bool isDateValid(int, int, int);

int main()
{
    int month, day, year;

    cout << "Is the date you are thinking of a magic one? Let's see...\n";
    cout << "What is the month in numeric form only? "; cin >> month;
    cout << "What is the day of the month? "; cin >> day;
    cout << "What is the four digit year? "; cin >> year;

if (isDateValid(month, day, year) == false)
{
    cout << "The date you entered, "<< month << "/" << day << "/" << 
         year << " is NOT a VALID Date.\n";
    cout << "If you think I am wrong, please feel free to run me again";
}

else  if (month * day == year % 100) // if true then do this
{
    cout << "The date you entered, " << 
         month << "/" << day << "/" << year << " is a magic date!!\n"; 
}
else // if false then do this
{
    cout << "The date you entered, " << month << "/" << day << "/" <<
         year << " is NOT a magic date!!\n";
}

                                                            //Program ID
cout <<"\n" << "L5P1LB.cpp\n" << "11-26-12\n" << endl;
system ("pause");
return 0;
} //end main

// isDateValid function to validate date entered
bool isDateValid(int month, int day, int year)
{
    bool validation = true;  // validation set to true
    // if it is not between 1600 and 2100 then set to false
    if(!(year >= 1600 && year <=2100))
        validation = false;
    // call leapYear function
    if(leapYear(year))
{
    // if February and day is greater than 29 then set to false
    if ((month == 2) && (day > 29))
         validation = false;
}
    // else if NOT leapYear
    else if(!leapYear(year))
{
    // if February and day is greater then 28 then set to false
    if ((month == 2) && (day > 28))
          validation = false;
}    
    // if month is less then 1 and over 12 then set to false
    if((month < 1 && month > 12))
          validation = false;
    // if day is less then 1 then set to false    
    if(day < 1)    
          validation = false;
    // if month is 1 (Jan), 3 (Mar), 5 (May), 7 (July), 8 (Aug), 10 (Oct),
    // or 12 (Dec) and day is greater then 31 set to false    
    if((month == 1) || (month ==3) || (month == 5) || (month == 7) ||
         (month == 8) || (month == 10) || (month == 12) && (day > 31))
         validation = false;
    // if month is 4 (Apr), 6 (June), 9 (Sept), or 11 (Nov) and day is 
    // greater then 30 set to false
    if (((month == 4) || (month == 6) || (month == 9) || (month == 11)) && 
         (day > 30))
         validation = false;
    // else everything that is left set validation to true
    else
         validation = true;
return validation;
}  // End isDateValid

// leapYear function to determine if the year is a leap year for validation
bool leapYear(int year)
{
    return(year % 100 != 0 && year % 4 == 0) || (year % 400 == 0);
} // end leapYear

場所とか間違ってたらごめんなさい!

また、ご協力いただきありがとうございます。本当に感謝しています。

4

3 に答える 3

3

日付の検証にいくつか誤りがありました。

Luchian が指摘した演算子の優先順位は 1 つですが、他にもいくつかありました。

  • あなたはそれをテストしますmonth < 1 && month > 12- これは不可能です。月は 1 より小さく 12 より大きくすることはできません
  • validation == true何もしません。==比較演算子です。幸いなことに、このコードに到達したときvalidationのように、これは関数に影響しませんでした。true

よりクリーンな方法であると思う方法で自由に書き直しましたがmonth、バージョンを保持したい場合は、おそらくチェックを修正するだけで済みます

bool isDateValid(int month, int day, int year)
{
    bool validation = true;
    if(!(year >= 1600 && year <=2100))
        validation = false;    

    if(day < 1)
        validation = false;

    switch(month)
    {
    case 2:
        if(leapYear(year)) // We only care about leap years in February 
            if(day > 29)
                validation = false;
        else
            if(day > 28)
                validation = false;
        break;
    case 1: case 3: case 5: case 7: case 8: case 10: case 12:
        if(day > 31)
            validation = false;
        break;
    case 4: case 6: case 9: case 11:
        if(day > 30)
            validation = false;
        break;
    default: // the month is not between 1 and 12
        validation = false;
        break;
    }
    return validation;
}
于 2012-11-27T12:37:19.223 に答える
1

個人的には、もっと直接的で簡潔なものをお勧めします。三?項演算子は最初は少し混乱するかもしれませんが、はっきりと理解すれば非常に単純です。式に埋め込むことができる一種の「if-else」と考えています。

bool isDateValid(int month, int day, int year)
{
    return year >= 1600 && year<= 2100 &&
           month >= 1 && month <= 12 &&
           day >= 1 &&
           day <= (month == 2 ? (leapyear(year) ? 29 : 28) :
                   month == 9 || month == 4 || month == 6 || month == 11 ? 30 : 31);
                   // "30 days has September, April, June and November..."
}

月の定数に列挙型を使用すると、もう少し読みやすくなる場合があります。

enum Month { Jan = 1, Feb, Mar, Apr ... };

実際にコーディングすることを目指しています:

day <= (month == Feb ? ...
于 2012-11-28T00:12:02.553 に答える
1

||は短絡しているため、最初の条件が満たされているかどうか(month == 1) || (month ==3) || (month == 5) || (month == 7) || (month == 8) || (month == 10) || (month == 12) && (day > 31)を評価します。true括弧のセットを追加する必要があります。

((month == 1) || (month ==3) || (month == 5) || (month == 7) || (month == 8) || (month == 10) || (month == 12)) && (day > 31)

他の月も同様です。

また、

validation == true; 

はノーオペレーションです。おそらくvalidation = true.

于 2012-11-27T04:48:37.333 に答える