c++ - 如何计算创建空心立方体所需的材料体积
问题描述
我想计算创建一个 10mm 10mm 10mm 厚度为 1mm 的空心立方体所需的材料体积。我可以使用以下代码获得空心立方体的体积,但我不知道如何获得创建空心立方体所需的材料体积。我认为这是一个愚蠢的问题,但我没有合适的答案。任何建议将不胜感激。谢谢
#include <iostream>
using namespace std;
class Volume // Class definition to calculate the volume of the hollow
{
public:
Volume(){};
double cubeVolume(double length)
{
double volume= length * length * length;
return volume;
}
double cube(double num)
{
return (num*num*num);
}
double getHollowCubeVolume(double length, double thickness)
{
double outerVol=cubeVolume(length);
double innerVol= cube((length-(thickness+thickness)));
double HollowCubeVolume = (cube(outerVol)-cube(innerVol));
return HollowCubeVolume;
}
};
int main()
{
cout << "Volume of the material required for hollow cube" << endl;
Volume volObj;
cout<<"Volume of material used to create a hallow cube: "<<volObj.getHollowCubeVolume(10,1)<<endl;
return 0;
}
解决方案
首先关注评论。看来这只是一个愚蠢的错误。但是错误出现在以下函数中-
double getHollowCubeVolume(double length, double thickness)
{
double outerVol=cubeVolume(length);
double innerVol= cube(length-(thickness+thickness));
double HollowCubeVolume = outerVol-innerVol; //Just take the difference.
// No need to pass them in cube function again.
return HollowCubeVolume;
}
推荐阅读
- java - For Loop 不能正确地分隔字符
- perl - 当我只期待一个匹配时,我可以将 grep(或 map)限制为第一个匹配吗?
- javascript - 为什么同时调用 promise .then() 和 .catch() ?
- android - 在我的 Android 应用程序中未收到推送通知
- python-3.x - 展开“查看更多”以获取 Python 中的隐藏列表
- python - 未捕获(承诺中)错误:检查时出错:预期 flatten_1_input 的形状为 [null,7,7,512] 但得到的数组的形状为 [1,224,224,3]
- conv-neural-network - 在 DGL 中使用 GCN 的边缘特征
- parsing - 如何从 WebM 文件中提取音频
- c# - 如何修复 ToggleButton 悬停检测?
- microsoft-teams - 为什么我在使用 Teams Shifts API 时经常出现超时错误?(微软图形)