| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 588 人关注过本帖
标题:初学循环if的问题 闰年2月28/29
只看楼主 加入收藏
nubore
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2010-9-14
结帖率:50%
收藏
已结贴  问题点数:20 回复次数:2 
初学循环if的问题 闰年2月28/29
程序代码:
// Month Day Year Class

using namespace std;

#include <string>

class CMonthDayYear
{
    public:
        // Default Constructor for CMonthDayYear
        CMonthDayYear(){ cout << "\nDefault MonthDayYear's Constructor is called!\n\n"; }

        // Validate Facilitator
        void ValidatingTheDate(int Month, int Day, int Year)
        {
            string NameOfMonth;
            bool Check;
            Check = true;
            if ( Month < 1 || Month > 12 )
            {
                cout << "\nYour Input is : " << Month << "/" << Day << "/" << Year << "\n"
                     << "INVALID input of month : *** " << Month << " ***  OUT OF RANGE" << endl << endl;
                Check = false;
            } // End if of Month input check
            if ( 1 <= Month <= 12 )
            {
                switch(Month)
                {
                    case 1:
                        NameOfMonth = "January";  break;
                    case 2:
                        NameOfMonth = "February"; break;
                    case 3:
                        NameOfMonth = "March";    break;
                    case 4:
                        NameOfMonth = "April";    break;
                    case 5:
                        NameOfMonth = "May";      break;
                    case 6:
                        NameOfMonth = "June";     break;
                    case 7:
                        NameOfMonth = "July";     break;
                    case 8:
                        NameOfMonth = "August";   break;
                    case 9:
                        NameOfMonth = "September";break;
                    case 10:
                        NameOfMonth = "October";  break;
                    case 11:
                        NameOfMonth = "November"; break;
                    case 12:
                        NameOfMonth = "December"; break;
                    default:;
                } // End of Switch number to nameofmonth
                for ( int LeapYear = 1500; LeapYear <= 2500; LeapYear ++ )
                {
                    if( (Month == 2) && (LeapYear % 4 == 0) && (Year == LeapYear) && (Day < 1 || Day > 29) )
                    {
                        cout << "\nYour Input is : " << Month << "/" << Day << "/" << Year << "\n"
                             << "INVALID input in Leap Year month : *** " << NameOfMonth << " ***"
                             << "\nYour input in month *** "
                              << NameOfMonth << " *** is : " << Day << endl << endl;
                        Check = false;
                        /*
                        if ( Year == LeapYear )
                        {
                            if ( Month == 2 )
                            {
                                if ( Day < 1 || Day > 29 )
                                {                   
                                } // LeapYear's Feb 1-29
                            } // End if of Day Check on Feb.
                        } // LeapYear is the Year input
                        */
                    } // End if of Day Check on Leap Year Feb.
                    else if ((Month == 2) && (LeapYear % 4 != 0) && (Day < 1 || Day > 28))
                    {
                        cout << "\nYour Input is : " << Month << "/" << Day << "/" << Year << "\n"
                             << "INVALID input in month : *** " << NameOfMonth << " ***"
                             << "\nYour input in month *** "
                             << NameOfMonth << " *** is : " << Day << endl << endl;
                        Check = false;
                    } // End if of Day Check on Normal Year Feb.
                } // End of For/If for checking leap year
               
                if ( (Month == 1 || Month == 3 || Month == 5 || Month == 7 || Month == 8 || Month == 10 || Month == 12) && ( Day < 1 || Day > 31 ))
                {
                    cout << "\nYour Input is : " << Month << "/" << Day << "/" << Year << "\n"
                         << "INVALID input in month : *** " << NameOfMonth << " ***"
                         << "\nYour input in month "
                         << NameOfMonth << " *** is : " << Day << endl << endl;
                    Check = false;
                } // End if of Day Check on long months
                if ( (Month == 4 || Month == 6 || Month == 9 || Month == 11) && ( Day < 1 || Day > 30 ))
                {
                    cout << "\nYour Input is : " << Month << "/" << Day << "/" << Year << "\n"
                         << "INVALID input in month : " << NameOfMonth
                         << "\nYour input in month *** "
                         << NameOfMonth << " *** is : " << Day << endl << endl;
                    Check = false;
                } // End if of Day Check on short months
            } // End of month check for loop

            if (Check)
                cout << "\nYour Input is : " << Month << "/" << Day << "/" << Year
                     << "\nThe Month is  : " << NameOfMonth
                     << "\nThe Day is    : " << Day
                     << "\nThe Year is   : " << Year
                     << "\n\n";
        }
    private:
};
那个for不知道怎么用了

把那个闰年二月或正常的二月拿出来都可以用, 放到for里就sb了。 那些判断没有错啊? ……  自我感觉应该是没错, 现在想到的只有放码的位置了, 试了很多不同的放法都不行, 有前辈来指教一下吗? 谢谢!!

PS: 初学C++初到这个网站,刚刚看到给分的问题, 这里问问题一定要分才可以吗?

[ 本帖最后由 nubore 于 2010-9-14 09:39 编辑 ]
搜索更多相关主题的帖子: 初学 闰年 
2010-09-14 09:37
hahayezhe
Rank: 15Rank: 15Rank: 15Rank: 15Rank: 15
来 自:湖南张家界
等 级:贵宾
威 望:24
帖 子:1386
专家分:6999
注 册:2010-3-8
收藏
得分:14 
为什么要if else if呢
直接 if else不行么
闰月只有是或不是两种情况

还有你的月份不能用枚举么!
那样代码多累赘!
2010-09-14 09:52
nubore
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2010-9-14
收藏
得分:0 
else if ((Month == 2) && (LeapYear % 4 != 0) && (Day < 1 || Day > 28))
改成
 if ((Month == 2) && (LeapYear % 4 != 0) && (Year = LeapYear)  && (Day < 1 || Day > 28))
就可以了

enum马上就学了, 感谢建议
2010-09-14 11:04
快速回复:初学循环if的问题 闰年2月28/29
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.031750 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved