注册 登录
编程论坛 C++教室

求助。闰年判断问题

King_IT 发布于 2013-06-17 11:53, 917 次点击
此c++程序依次输入年份、月份、日期,并在输入的同时判断输入是否正确:比如闰年2月份有29日、4月份没有31日等,最后输出年月日以及该年是否是闰年。
我写了一早上都不对郁闷啊 。
#include<iostream>
#include<string>
using namespace std;
class date
{
private:
    int year;
    int month;
    int day;
public:date(int y=2012,int m=1,int d=1)
       {year=y;month=m;day=d;}
       void display1()
       {
           cout<<"输出员工的参加工作时间:"<<endl;
           cout<<year<<"/"<<month<<"/"<<day<<endl;
       }
       void input1()
       {
           cout<<"输入员工的参加工作时间:"<<endl;
           cin>>year>>month>>day;
          bool  flag = isLeap( year );
           if( flag )
           {//cout<<year<<" 是闰年."<<endl;
             if(month=2 && day>29)
                 cout<<"输入有误,请重新输入!"<<endl;
           }
           else
           {//cout<<year<<" 不是闰年。"<<endl;
            if(month=2 && day>28)
                 cout<<"输入有误,请重新输入"<<endl;
           }
       }

       bool  isLeap(int year)  //判断闰年
       {
         return (year%4==0&&year%100!=0||year%400==0);
       }
};

int main ()
{
    date d(2004,1,26);
    d.input1();
    d.display1();
    return 0;
}
程序运行后月份老是显示为0啊。
10 回复
#2
love云彩2013-06-17 13:01
你还是直接把题目放上来,这样才知道你想要表达什么
#3
justlxy2013-06-17 13:41
有一个小问题,程序已修改
#include<iostream>
 #include<string>
 using namespace std;
 class date
 {
 private:
     int year;
     int month;
     int day;
 public:
     date(int y=2012,int m=1,int d=1)
        {year=y;month=m;day=d;}
     bool  isLeap(int year)  //判断闰年
       {
          return (year%4==0&&year%100!=0||year%400==0);
        }
        void display1()
        {
            cout<<"输出员工的参加工作时间:"<<endl;
            cout<<year<<"/"<<month<<"/"<<day<<endl;
        }
        void input1()
        {
            cout<<"输入员工的参加工作时间:"<<endl;
            cin>>year>>month>>day;
           bool  flag = isLeap( year );
            if( flag )
            {cout<<year<<" 是闰年."<<endl;
              if(month==2 && day>29)//条件错误!是==不是=
                  cout<<"输入有误,请重新输入!"<<endl;
            }
            else
            {cout<<year<<" 不是闰年。"<<endl;
             if(month==2 && day>28)//条件错误!
                  cout<<"输入有误,请重新输入"<<endl;
            }
        }

      
 };

 int main ()
 {
     date d(2004,1,26);
     d.input1();
     d.display1();
     return 0;
 }
#4
子楠2013-06-17 13:56
回复 3楼 justlxy
重新输入的时候就不行了,你这个
#5
子楠2013-06-17 13:57
#include<iostream>
#include<string>
using namespace std;
class date
{
private:
    int year;
    int month;
    int day;
public:date(int y=2012,int m=1,int d=1)
       {year=y;month=m;day=d;}
       void display1()
       {
           cout<<"输出员工的参加工作时间:"<<endl;
           cout<<year<<"/"<<month<<"/"<<day<<endl;
       }
       void input1()
       {
           cout<<"输入员工的参加工作时间:"<<endl;
           cin>>year>>month>>day;
          bool  flag = isLeap( year );
           if( flag )
           {//cout<<year<<" 是闰年."<<endl;
             while((month==2 && day>29))
             {
                 cout<<"输入有误,请重新输入!"<<endl;
                cin>>year>>month>>day;
             }
           }
           else
           {
                while((month==2 && day>28))
                {
                     cout<<"输入有误,请重新输入!"<<endl;
                    cin>>year>>month>>day;
                }
            }
       }

       bool  isLeap(int year)  //判断闰年
       {
         return (year%4==0&&year%100!=0||year%400==0);
       }
};

int main ()
{
    date d(2004,1,26);
    d.input1();
    d.display1();
    return 0;
}
这我改的
#6
ydown2013-06-17 14:24
public:date(int y=2012,int m=1,int d=1)
改成:
public:date(int y,int m,int d)

算法有些小问题,需改改.
1.    date d(2004,2.31);方式传值,程序不会对此判断闰年问题,只有通过input1()操作才有.如果 date d(2004,2,31);也会正常输出 2004/2/31,需要修改算法.
2.    display1(); 输入错误后,未有重新输入的动作.
#7
King_IT2013-06-17 17:14
回复 3楼 justlxy
恩恩,谢谢。不过若是输入改为2004。6.32.它无法判断日期啊。怎么样该才能让day也在合理范围?
#8
King_IT2013-06-17 17:30
回复 5楼 子楠
怎么改才能让这个程序同时判断day,比如说,4月没有31号,这个程序如果输入2004.4.31它判断不出来啊!
#9
King_IT2013-06-17 17:31
回复 2楼 love云彩
依次输入年份、月份、日期,并在输入的同时判断输入是否正确:比如闰年2月份有29日、4月份没有31日等,最后输出年月日以及该年是否是闰年。
这是题目,谢谢。
#10
lzj125302013-06-18 08:23
switch(month>7?(month-7)%2:month%2) //条件语句 如果输入月份大于7就执行(month-7)%2 否则就是month%2进行余数判断就能判断出月份是大是小.
#11
ydown2013-06-20 16:43
程序代码:
// example.cpp
#include <iostream>
using namespace std;

#include <string>
#include "Test.h"

int main()
{
    Test d(2004,2,30);    //用个错误的,借此可以测试程序.
    d.input1();
    d.display();
    return 0;
}

// Test.h
#include<iostream>
class Test
{
private:
    int year;
    int month;
    int day;
    int monthArray[12];
public:
    Test(int y,int m,int d);   
    void setYearMonthDay(int y,int m,int d);
    void judge();    //判断输入
    void input1();    //输入年月日
    void display();    //显示
    bool isLeap(int y);    //判断闰年
};


// Test.cpp
#include<iostream>
using namespace std;

#include <string>
#include "Test.h"

Test::Test(int y,int m,int d)   
{
    setYearMonthDay(y,m,d);
    judge();
    display();
    cout<<endl;
    cout<<"-----=====以上带参方式=====以下手工输入数据方式=====-----"<<endl;
    cout<<endl;
}
void Test::setYearMonthDay(int y,int m,int d)
{
    year=y;
    month=m;
    day=d;
    monthArray[0]=31;
    monthArray[1]=28;
    monthArray[2]=31;
    monthArray[3]=30;
    monthArray[4]=31;
    monthArray[5]=30;
    monthArray[6]=31;
    monthArray[7]=31;
    monthArray[8]=30;
    monthArray[9]=31;
    monthArray[10]=30;
    monthArray[11]=31;
}
void Test::display()
{
    cout<<"输出员工的参加工作时间:"<<endl;
    cout<<year<<"/"<<month<<"/"<<day<<endl;
}
void Test::judge()    //判断部分;
{
    if (isLeap(year))
    {
        monthArray[1]=29;
    }
    else
    {
        monthArray[1]=28;
    }
    int isRight=0;    //输入错误isRight=0
    do    //判断月份的输入是否正确
    {
        if (month>0 && month<=12)
        {
            isRight=1;    //输入正确isRight=1
        }
        else
        {
            cout<<"月份必须是1-12之间的整数,请重新输入:";
            cin>>month;
        }
    } while (!isRight);
    isRight=0;    //重置isRight
    do
    {
        if (day>0 && day<=monthArray[month-1])
        {
            isRight=1;
        }
        else
        {
            cout<<""<<month<<"月的日期必须是1-"<<monthArray[month-1]<<"之间的整数,请重新输入:";
            cin>>day;
        }
    } while (!isRight);

}
void Test::input1()    //输入员工参加工作的年月日
{
    cout<<"输入员工的参加工作年份:"<<endl;
    cin>>year;
    cout<<"输入员工的参加工作月份:"<<endl;
    cin>>month;
    cout<<"输入员工的参加工作日期:"<<endl;
    cin>>day;
    judge();    //进入判断
}
bool Test::isLeap(int testYear)    //判断闰年
{
    return ((testYear%4==0 && testYear%100!=0) || testYear%400==0);
}


一共3个文件.

[ 本帖最后由 ydown 于 2013-6-20 16:52 编辑 ]
1