首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >不使用类的C++验证日期

不使用类的C++验证日期
EN

Stack Overflow用户
提问于 2012-11-27 12:46:01
回答 3查看 10.8K关注 0票数 0

嗨,我希望有人能帮我解决我做错了什么。这是我要指出的作业,但我需要有人来告诉我我是个多么愚蠢的人,因为我看不出我做错了什么。

我应该让用户输入月份、日期和年份,以确定这是否是一个神奇的日期。这部分我已经为另一个项目完成了。我应该返回并使用一个名为isDateValid的函数来验证日期。我很确定我遗漏了一些明显的东西,在这个问题上需要任何人的帮助。谢谢!

问题是当我运行它的时候,我总是得到它是一个无效的日期!

代码语言:javascript
复制
 #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

编辑

好了,现在我的问题是,我没有得到它不是一个有效的日期,当它是。它跳过了所有的验证,只运行其中的“神奇日期”部分!

这里是重写加上我的完整代码,希望在这里适当地缩进,因为它是在血腥。

代码语言:javascript
复制
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

抱歉,如果我把错误的位置或任何东西!

也感谢你的帮助。我真的很感激。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-11-27 20:37:19

您在日期验证中有一些错误。

Luchian提到的运算符优先级是一个,但您还有其他几个:

  • 你测试month < 1 && month > 12 -这是不可能的,一个月不能小于1,并且大于12
  • validation == true不会做任何事情。==是一个比较运算符。幸运的是,这并没有影响您的函数,因为在执行此代码时,validation已经是true了。

我冒失地以我认为更简洁的方式重写了它,但如果您更喜欢保留您的版本,您可能只需要修复month检查即可

代码语言:javascript
复制
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;
}
票数 3
EN

Stack Overflow用户

发布于 2012-11-27 12:48:37

||是短路的,因此如果满足第一个条件,则(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)

其他月份也是如此。

另外,

代码语言:javascript
复制
validation == true; 

是个禁区你可能指的是validation = true

票数 1
EN

Stack Overflow用户

发布于 2012-11-28 08:12:03

就我个人而言,我会建议一些更直接和简洁的东西。?三进制运算符一开始可能有点混乱,但一旦清楚理解就会非常简单。我认为它是一种可以嵌入到表达式中的"if-else“。

代码语言:javascript
复制
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..."
}

如果对月份常量使用枚举,它可能会更具可读性:

代码语言:javascript
复制
enum Month { Jan = 1, Feb, Mar, Apr ... };

我的目标是实际编码:

代码语言:javascript
复制
day <= (month == Feb ? ...
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13577555

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档