PTA-实验五-圆形体体积计算器
7-1 圓形體體積計算器 (20分)
本題要求實現(xiàn)一個常用圓形體體積的計算器。計算公式如下:
球體體積 V=
?3
?
?4
?? πr
?3
?? ,其中r是球體半徑。
圓柱體體積 V=πr
?2
?? h,其中r是底圓半徑,h是高。
圓錐體體積 V=
?3
?
?1
?? πr
?2
?? h,其中r是底圓半徑,h是高。
輸入格式:
在每次計算之前,要求輸出如下界面:
1-Ball
2-Cylinder
3-Cone
other-Exit
Please enter your command:
然后從標準輸入讀進一個整數(shù)指令。
輸出格式:
如果讀入的指令是1或2或3,則執(zhí)行相應的體積計算;如果是其他整數(shù),則程序結(jié)束運行。
當輸入為1時,在計算球體體積之前,打印Please enter the radius:,然后讀入球體半徑,完成計算;
當輸入為2時,在計算圓柱體體積之前,打印Please enter the radius and the height:,然后讀入底圓半徑和高,完成計算;
當輸入為3時,在計算圓錐體體積之前,打印Please enter the radius and the height:,然后讀入底圓半徑和高,完成計算。
計算結(jié)果在一行內(nèi)輸出,保留小數(shù)點后兩位。
輸入樣例:
1
2
3
2.4 3
0
輸出樣例:
1-Ball
2-Cylinder
3-Cone
other-Exit
Please enter your command:
Please enter the radius:
33.51
1-Ball
2-Cylinder
3-Cone
other-Exit
Please enter your command:
Please enter the radius and the height:
18.10
1-Ball
2-Cylinder
3-Cone
other-Exit
Please enter your command:
自己重做了一遍實驗之后不知道在PTA哪里可以提交檢查,PTA里那題規(guī)定是用C語言的。。。
不知道在最后加個endl可不可以咧,不加的話就不會分行
#include<iostream> #include<iomanip> #define PI 3.1415926535 using namespace std; int main() {int n;double r,h,V;while(1) { cout<<"1-Ball"<<endl<<"2-Cylinder"<<endl<<"3-Cone"<<endl<<"other-Exit"<<endl<<"Please enter your command:";cin>>n;switch(n){case 1:cout<<"Please enter the radius:";cin>>r;V=4*PI*r*r*r/3;cout<<setiosflags(ios::fixed)<<setprecision(2)<<V;break;case 2:cout<<"Please enter the radius:";cin>>r>>h;V=PI*r*r*h;cout<<setiosflags(ios::fixed)<<setprecision(2)<<V;break;case 3:cout<<"Please enter the radius:";cin>>r>>h;V=PI*r*r*h/3;cout<<setiosflags(ios::fixed)<<setprecision(2)<<V;break;default:return 0;}cout<<endl;}}總結(jié)
以上是生活随笔為你收集整理的PTA-实验五-圆形体体积计算器的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 解决从微信公众号复制图片显示“此图片来自
- 下一篇: wemall商城15套商用源码即将开放免