C++ 计算几何的体积
#include <iostream>
#include <cmath>
using namespace std;
void V_Cuboid(); //计算长方体的体积
void V_Cyblinder(); //计算圆柱体的体积
void V_Cone(); //计算圆锥体的体积
void V_Cuboid()
{
//输入长宽高
double length, width, height;
cout << "请输入长宽高:";
cin >> length >> width >> height;
//计算体积
double v = length * width * height;
//结果保留两位小数
cout.setf(ios::fixed);
cout << setprecision(2);
//cout<<fixed<<setprecision(2);
cout << "长方体的体积为:" << v << endl;
}
void V_Cyblinder()
{
//输入半径、高
const double pi = 3.14;
double radius, height;
cout << "请输入半径、高:";
cin >> radius >> height;
//计算体积
double v = pi * pow(radius,2) * height;
//结果保留两位小数
cout.setf(ios::fixed);
cout << setprecision(2);
//cout<<fixed<<setprecision(2);
cout << "长方体的体积为:" << v << endl;
}
void V_Cone()
{
//输入半径、高
const double pi = 3.14;
double radius, height;
cout << "请输入半径、高:";
cin >> radius >> height;
//计算体积
double v = pi * pow(radius, 2) * height * 1/3;
//结果保留两位小数
cout.setf(ios::fixed);
cout << setprecision(2);
//cout<<fixed<<setprecision(2);
cout << "圆锥体的体积为:" << v << endl;
}
int main() {
int choice = -1;
while(choice){
cout << "1. 长方体" << endl;
cout << "2. 圆柱体" << endl;
cout << "3. 圆锥体" << endl;
cout << "0. 退出" << endl;
cin >> choice;
switch (choice) {
case 1:
V_Cuboid();
break;
case 2:
V_Cyblinder();
break;
case 3:
V_Cone();
break;
}
}
cout << "感谢使用!" << endl;
return 0;
}