| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 917 人关注过本帖
标题:求助。闰年判断问题
只看楼主 加入收藏
ydown
Rank: 2
等 级:论坛游民
帖 子:31
专家分:92
注 册:2013-5-27
收藏
得分:0 
程序代码:
// 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 编辑 ]
2013-06-20 16:43
快速回复:求助。闰年判断问题
数据加载中...
 
   



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

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