| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 917 人关注过本帖
标题:求助。闰年判断问题
只看楼主 加入收藏
King_IT
Rank: 1
等 级:新手上路
帖 子:7
专家分:3
注 册:2013-6-17
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:10 
求助。闰年判断问题
此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啊。
搜索更多相关主题的帖子: private include public 年月日 
2013-06-17 11:53
love云彩
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:青藏高原
等 级:贵宾
威 望:53
帖 子:3663
专家分:11416
注 册:2012-11-17
收藏
得分:4 
你还是直接把题目放上来,这样才知道你想要表达什么

思考赐予新生,时间在于定义
2013-06-17 13:01
justlxy
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:28
专家分:158
注 册:2013-5-14
收藏
得分:4 
有一个小问题,程序已修改
#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;
 }
收到的鲜花
  • King_IT2013-06-17 17:38 送鲜花  3朵   附言:我很赞同
2013-06-17 13:41
子楠
Rank: 3Rank: 3
来 自:武汉
等 级:论坛游侠
帖 子:111
专家分:164
注 册:2013-4-9
收藏
得分:4 
回复 3楼 justlxy
重新输入的时候就不行了,你这个
2013-06-17 13:56
子楠
Rank: 3Rank: 3
来 自:武汉
等 级:论坛游侠
帖 子:111
专家分:164
注 册:2013-4-9
收藏
得分:0 
#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;
}
这我改的
收到的鲜花
  • King_IT2013-06-17 17:37 送鲜花  3朵   附言:我很赞同
2013-06-17 13:57
ydown
Rank: 2
等 级:论坛游民
帖 子:31
专家分:92
注 册:2013-5-27
收藏
得分:4 
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(); 输入错误后,未有重新输入的动作.
收到的鲜花
  • King_IT2013-06-17 17:36 送鲜花  3朵   附言:我很赞同
  • King_IT2013-06-17 17:36 送鲜花  3朵   附言:我很赞同
2013-06-17 14:24
King_IT
Rank: 1
等 级:新手上路
帖 子:7
专家分:3
注 册:2013-6-17
收藏
得分:0 
回复 3楼 justlxy
恩恩,谢谢。不过若是输入改为2004。6.32.它无法判断日期啊。怎么样该才能让day也在合理范围?
2013-06-17 17:14
King_IT
Rank: 1
等 级:新手上路
帖 子:7
专家分:3
注 册:2013-6-17
收藏
得分:0 
回复 5楼 子楠
怎么改才能让这个程序同时判断day,比如说,4月没有31号,这个程序如果输入2004.4.31它判断不出来啊!
2013-06-17 17:30
King_IT
Rank: 1
等 级:新手上路
帖 子:7
专家分:3
注 册:2013-6-17
收藏
得分:0 
回复 2楼 love云彩
依次输入年份、月份、日期,并在输入的同时判断输入是否正确:比如闰年2月份有29日、4月份没有31日等,最后输出年月日以及该年是否是闰年。
这是题目,谢谢。
2013-06-17 17:31
lzj12530
Rank: 8Rank: 8
等 级:蝙蝠侠
威 望:2
帖 子:264
专家分:841
注 册:2013-3-28
收藏
得分:4 
switch(month>7?(month-7)%2:month%2) //条件语句 如果输入月份大于7就执行(month-7)%2 否则就是month%2进行余数判断就能判断出月份是大是小.

C++菜鸟
2013-06-18 08:23
快速回复:求助。闰年判断问题
数据加载中...
 
   



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

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