码迷,mamicode.com
首页 > 其他好文 > 详细

面向对象第一次实验参考代码

时间:2015-10-18 11:33:17      阅读:174      评论:0      收藏:0      [点我收藏+]

标签:

#include <iostream.h>  
#include <stdlib.h>  

//Global variable  
int days[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31};//days of month  
char *Week[7] = {"Sunday","Monday","Tuesday","Wednesday","Thurday","Friday","Saturday"};  //Week  

class Date{//class defination  
public:  
    void SetDate(int,int,int);// initialization  
    int IsLeapyear(int );  //Leap year or not  
    int Week_day(int);  //Week_day  
    void Show();  //days number  
private:  
    int year,month,day;  
};  

void Date::SetDate(int y,int m,int d)  
{  
    year = y;  
    month = m;  
    day = d;  
    if(year <= 0 || month <= 0 || day <= 0 || month > 12
		 || day > days[month])  //Date validity check  
    {  
        cout<<"The Date is invalid!"<<endl;  
        exit(0);  
    }  
}  

int Date::IsLeapyear(int y)  
{  
    return y % 4 == 0 && y % 100 != 0 || y % 400 == 0;  
}  

int Date::Week_day(int flag)    
//flag 0:week 1:day_num;  
{  
    long sum = 0;  
    int i;  
    if(flag == 0)  
        i = 1;  
    else  
        i = year;  
    for(;i < year;i++)  
    {  
        if(IsLeapyear(i))  
            sum += 366;  
        else  
            sum += 365;  
    }  
    for(int j = 1;j < month;j++)  
        sum += days[j];  
    if(month > 2 && IsLeapyear(year))  
        sum++;  
    sum += day;  
    if(flag == 0)  
        return sum % 7;  
    return sum;  
};  

void Date::Show()  
{  
    cout<<"The Date is:"<<year<<"-"<<month<<"-"<<day<<endl;  
    cout<<"It‘s "<<*(Week+Week_day(0))<<endl;  
    cout<<"It‘s "<<Week_day(1)<<" days of this year!"<<endl;  
}  

void main()  
{  
    Date D;  
    int y,m,d;  
    cout<<"Please input the Date:"<<endl;  
    cin>>y>>m>>d;  
    D.SetDate(y,m,d);  
    D.Show();  
} 

版权声明:本文为博主原创文章,未经博主允许不得转载。

面向对象第一次实验参考代码

标签:

原文地址:http://blog.csdn.net/laoduan_78/article/details/49225953

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!