C++求某日期的昨天、明天并判断两日期是否相等。

定义一个由年月日构成的日期类型,并设计函数实现以下操作:
①设计函数实现日期类型数据的初始化(三种方式实现:返回值、引用和指针法);
②为了保证输入的日期的合法性,要求设计一个函数对输入的日期进行正确性的检测;
③设计函数求某日期的昨天对应的日期;
④设计函数求某日期的明天对应的日期;
⑤设计函数判断两个日期是否相等。

#include <iostream>
#include <iomanip> 
#include <stdlib.h>
using namespace std;
struct Date{
	int year,month,day;
};
bool isLeap(int year){
	return year%400||(year%4==0&&year%100!=0);
}
int getDayNumberDate(const Date d){
	int num;
	switch(d.month){
		case 4:
		case 6:
		case 9:
		case 11:
			num=30;
			break;
		case 2:
			if(isLeap(d.year)){
				num=29;
			}else{
			num=28;
			}
		case 1:
		case 3:
		case 5:
		case 7:
			case 8:
				case 10:
					case 12:
		num=31;	
	}
	return num;
}
bool checkDate(Date d){
	bool flag=1;
	if(d.year<1000||d.year>9999){
		cout<<"输入的年号错误!"<<endl;
		flag=0;
	}else if(d.month<1||d.mon
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以回答这个问题。 首先,我们需要使用C++语言编写一个长方体类。长方体类应该具有长度(length)、宽度(width)和高度(height)这三个成员变量,以及计算表面积和体积的方法。 下面是一个简单的长方体类示例: ```c++ class Cuboid { public: // 构造函数 Cuboid(double l, double w, double h) : length(l), width(w), height(h) {} // 计算表面积 double surface_area() const { return 2 * (length * width + length * height + width * height); } // 计算体积 double volume() const { return length * width * height; } // 判断个长方体是否相等 bool operator==(const Cuboid& other) const { return length == other.length && width == other.width && height == other.height; } private: double length; double width; double height; }; ``` 在这个示例中,我们定义了一个Cuboid类,并在构造函数中初始化了长度、宽度和高度。然后,我们定义了计算表面积和体积的个方法,并使用运算符重载来判断个长方体是否相等。 现在,我们可以使用这个类来计算个长方体的表面积和体积,并判断它们是否相等: ```c++ Cuboid cuboid1(2.0, 3.0, 4.0); Cuboid cuboid2(3.0, 4.0, 2.0); // 计算表面积和体积 double surface_area1 = cuboid1.surface_area(); double volume1 = cuboid1.volume(); // 输出结果 std::cout << "Cuboid 1 surface area: " << surface_area1 << std::endl; std::cout << "Cuboid 1 volume: " << volume1 << std::endl; // 计算表面积和体积 double surface_area2 = cuboid2.surface_area(); double volume2 = cuboid2.volume(); // 输出结果 std::cout << "Cuboid 2 surface area: " << surface_area2 << std::endl; std::cout << "Cuboid 2 volume: " << volume2 << std::endl; // 判断个长方体是否相等 if (cuboid1 == cuboid2) { std::cout << "The two cuboids are equal." << std::endl; } else { std::cout << "The two cuboids are not equal." << std::endl; } ``` 这段代码将输出个长方体的表面积和体积,并判断它们是否相等

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值