输入一个日期,计算前一天的日期

#include<iostream>
#include<string>
using namespace std;
class date  //定义Date类
{
private:
	int day;
	int month;
	int year;
public:
	date();     //无参构造函数
	date(int,int,int);  //有参构造函数
	void prevdate();   //prevdate函数用于计算上前一天的日期
	void setdate(int,int,int);   //setdate函数用于设定Date类对象成员的值
};
date::date(int year,int month,int day)  //有参构造函数
{
	this->year=year;
	this->month=month;
	this->day=day;
}
void date::setdate(int year,int month,int day)  //setdate函数
{
	this->year=year;
	this->month=month;
	this->day=day;
}
void date::prevdate()   //prevdate函数,计算前一天日期
{
	int prevday;
	int prevmonth;
	int prevyear;
		if(day==1)
		{
			switch (month)  //当日期为1时 判断各个月份的情况
			{
			case 2:case 4:case 6:case 8:case 9:case 11:
				prevday=31;
				prevmonth=month-1;
				prevyear=year;
				break;
			case 5:case 7:case 10:case 12:
				prevday=30;
				prevmonth=month-1;
				prevyear=year;
				break;
			case 3:
				if(((year%4==0)&&(year%100!=0))||(year%400==0))
				{
					prevday=29;
					prevmonth=2;
					prevyear=year;
				}
				else
				{
					prevday=28;
					prevmonth=2;
					prevyear=year;
				}
				break;
			case 1:
				prevday=31;
				prevmonth=12;
				prevyear=year-1;
				break;
			}
		}
		else
		{
			prevday=day-1;
			prevmonth=month;
			prevyear=year;
		}
		cout<<"The prevdate is "<<prevyear<<" "<<prevmonth<<" "<<prevday<<endl;

}
int main()
{
	int year,month,day;
	while(cin>>year>>month>>day) //输入数据
	{
		   if(year<0)//判断非法日期输入
		   {
			   cout<<"Date error!Please input again!"<<endl;
			   continue;
		   }
		   else
		   {
			   switch (month)
			   {
			   case 1:case 3:case 5:case 7:case 8:case 10: case 12:
				   if(day<1||day>31)
				   {
					   cout<<"Date error!Please input again!"<<endl;
			           continue;
				   }
				   break;
			   case 4:case 6:case 9:case 11:
				   if(day<1||day>30)
				   {
					   cout<<"Date error!Please input again!"<<endl;
			           continue;
				   }
				   break;
			   case 2:
				   if((year%4==0&&year%100!=0)||year%400==0)
				   {
					   if(day<1||day>29)
					   {
                           cout<<"Date error!Please input again!"<<endl;
			               continue;
					   }
				   }
				   else
				   {
					   if(day<1||day>28)
					   {
                           cout<<"Date error!Please input again!"<<endl;
			               continue;
					   }
				   }
				   break;
			   default:
				   cout<<"Date error!Please input again!"<<endl;
			       continue;
				   break;
			   }
		   }
		   date Date(year,month,day);
		   Date.prevdate();
	}
}

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值