利用全局变量计算长方体的体积及三个面的面积c++
时间: 2024-02-27 16:58:43 浏览: 34
好的,我理解了你的问题。下面是利用全局变量计算长方体的体积及三个面的面积的C++代码:
```c++
#include <iostream>
using namespace std;
int length, width, height; //定义全局变量
int main() {
cout << "请输入长方体的长、宽、高:" << endl;
cin >> length >> width >> height;
int volume = length * width * height; //计算体积
int area1 = length * width; //计算底面积
int area2 = length * height; //计算侧面积1
int area3 = width * height; //计算侧面积2
cout << "长方体的体积为:" << volume << endl;
cout << "长方体的三个面分别为:" << endl;
cout << "底面积为:" << area1 << endl;
cout << "侧面积1为:" << area2 << endl;
cout << "侧面积2为:" << area3 << endl;
return 0;
}
```
在这段代码中,我们定义了全局变量`length`、`width`、`height`,然后在`main()`函数中输入长方体的长、宽、高,并计算长方体的体积及三个面的面积。最后输出结果。
注意,使用全局变量虽然方便了计算,但也容易造成程序的混乱和错误,因此要慎重使用。