初学cpp<class类01>

//运用class例题

#include<iostream>
using namespace std;
class Date
{
private:
	int Year;
	int Month;
	int Day;
public:
	Date(){}//默认构造函数
	Date(int y,int m,int d);//构造函数
	void ShowDate();
	void SetYear(int y);
	void SetMonth(int m);
	void SetDay(int d);
	Date NextDay();//定义Date型的函数,将Date型的值返回
	bool IsWeekend();
	//~Date(){cout<<"执行析构函数\n";};//析构函数
};

Date::Date(int y,int m,int d)
{
	Year=y;
	Month=m;
	Day=d;
}

void Date::ShowDate()//显示日期
{
	cout<<Year<<"-"<<Month<<"-"<<Day<<endl;
}

void Date::SetYear(int y)//设置年份
{
	Year=y;
}

void Date::SetMonth(int m)//设置月份
{
	Month=m;
}

void Date::SetDay(int d)//设置日期
{
	Day=d;
}

Date Date::NextDay()           //设置后的第二天的日期
{
	bool IsLeapyear(int);
	Date today;              //定义一个将要返回的Date类的值
	today.Year=Year;       //将刚定义的today 初始化
	today.Month=Month;
	today.Day=Day;
	int mon[13]={0,31,29,31,30,31,30,31,31,30,31,30,31};
	if(IsLeapyear(today.Year)==true)
	{
		today.Day++;
		if(today.Day>mon[Month])
		{
			today.Day=1;
			today.Month++;
			if(today.Month>12)
			{
				today.Month=1;
				today.Year++;
			}
		}
	}
	else
	{
		mon[2]=28;
		today.Day++;
		if(today.Day>mon[Month])
		{
			today.Day=1;
			today.Month++;
			if(today.Month>12)
			{
				today.Month=1;
				today.Year++;
			}
		}
	}
	//cout<<Year<<"-"<<Month<<"-"<<Day<<endl;
	return today;
}

bool Date::IsWeekend()     //判断是否为周末
{
	bool IsLeapyear(int);
	int days,i;
	int mon[13]={0,31,29,31,30,31,30,31,31,30,31,30,31};
	days=0;
	for(i=1;i<Year;i++)
		if(IsLeapyear(i)==true)
			days+=366;
		else
			days+=365;
	if(Month<2)
		days=days+31+Day;
	else
	{
		if(IsLeapyear(Year)==true)
		{
			for(i=1;i<Month;i++)
				days+=mon[i];
			days+=Day;
		}
		else
		{
			for(i=1;i<Month;i++)
				days+=mon[i];
			days+=Day;
			days--;
		}
	}
	if(days%7==0||days%7==6)
		return true;
	else
		return false;
}

bool IsLeapyear(int Year)     //判断是否为闰年
{
	if(Year%4==0&&Year%100!=0||Year%400==0)
		return true;
	else
		return false;
}

int main()
{
	int y,m,d;
	Date tomorrow;
	//t.ShowDate();
	Date today(2012,2,11);
	today.ShowDate();
	cout<<"please input year:";
	cin>>y;
	cout<<"after set time :\n";
	today.SetYear(y);
	today.ShowDate();
	cout<<"please input month:";
	cin>>m;
	cout<<"after set time :\n";
	today.SetMonth(m);
	today.ShowDate();
	cout<<"please input day:";
	cin>>d;
	today.SetDay(d);
	cout<<"after set time :\n";
	today.ShowDate();
	if(today.IsWeekend()==true)
		cout<<"today is weekend !\n\n";
	else
		cout<<"today is not weekend .\n\n";
	//cout<<"Today is Arbor Day !\n";
	cout<<"Tomorrow's Date is:\n";
	tomorrow=today.NextDay();   //执行函数,将today的下一天给tomorrow
	tomorrow.ShowDate();
	if(tomorrow.IsWeekend()==true)
		cout<<"tomorrow is weekend !\n\n";
	else
		cout<<"tomorrow is not weekend .\n\n";
	system("PAUSE");
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值