[凡鸽鸽]《实验报告01》增加01:设计方法求长方体的面积和体积。

2021年3月18日

【实验题目内容】增加01:

设计方法求长方体的面积和体积,此题目比较简单,运用数学公式即可解出。

直接上代码:

#include <iostream>
using namespace std;
//求长方体的底面周长的方法
double Bottomperimeter(double longside, double shortside)
{
	double Bottomperimeter = 2 * (longside + shortside);
	return Bottomperimeter;
}
//求长方体的底面积的方法
double Bottomarea(double longside, double shortside)
{
	double Bottomarea = longside * shortside;
	return Bottomarea;
}
//求长方体的表面积的方法
double Surfacearea(double longside, double shortside, double height)
{
	double surfacearea = 2 * Bottomarea(longside, shortside) + 2 * (height * shortside) + 2 * (height * longside);
	return surfacearea;
} 
//求长方体的体积的方法
double Volume(double longside, double shortside, double height)
{
	double volume = longside * height * shortside;
	return volume;
}
int main()
{
	system("color F0");
	double longside, shortside, height;
	cout << "请输入所求长方形的长:" << endl; cin >> longside;
	cout << "请输入所求长方形的宽:" << endl; cin >> shortside;
	cout << "请输入所求长方形的高:" << endl; cin >> height;
	cout << "所求长方体的底面周长为:" << Bottomperimeter(longside, shortside) << " 米;" << endl;
	cout << "所求长方体的底面积为:" << Bottomarea(longside, shortside) << " 平方米;" << endl;
	cout << "所求长方体的表面积为:" << Surfacearea(longside, shortside, height) << " 平方米;" << endl;
	cout << "所求长方体的体积为;" << Volume(longside, shortside, height) << " 立方米。" << endl;
	system("pause");
	return 0;
}

测试运行结果如下:
运行结果

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值