中易网

用C++做一个【求圆柱体积】小程序:

答案:6  悬赏:80  
解决时间 2021-04-27 18:44
  • 提问者网友:我是我
  • 2021-04-26 23:15
在主函数中编写代码,让用户输入圆柱的半径和高度,如果输入的数据合理,计算圆柱体积并打印结果,【重点】:如果输入数据不合理,提示用户重新输入,用户有三次输入机会。
最佳答案
  • 二级知识专家网友:星痕之殇
  • 2021-04-26 23:53
这次应该ok了,可以处理输入非数字的情况,刚才的代码还有点问题

#include <iostream>
using namespace std;

int main()
{
int n = 4;
double r, h, v;
while(--n) {
cout << "请输入圆柱的半径和高:";
cin >> r >> h;
if(cin.fail() || r <= 0 || h <= 0) {
cout << "数据输入不合理,请重新输入!\n";
cin.clear();
cin.ignore(1024, '\n');
} else break;
cout << "你还有 " << n-1 << " 次重试机会。\n";
}
if(n != 0) {
v = 3.14 * r*r * h;
cout << "\n圆柱的体积是:" << v << endl;
} else {
cout << "\n机会用完,程序结束。\n";
}
}
全部回答
  • 1楼网友:情窦初殇
  • 2021-04-27 02:13
#include <iostream> using namespace std; const PI = 3.1415926;class Cylinder { public: void SetCylinderValue(); void OutputCylinder(); BOOL Check(); private: double r; double h; };void Cylinder::SetCylinderValue() { double sr,sh; cout<<"Please input cylinder's information:"<<endl; cout<<"r = "; cin>>sr; cout<<"h = "; cin>>sh; r = sr; h = sh; }BOOL Cylinder::Check() { if(r <= 0 || h <= 0) { cout<<"Error!Please check the data!"<<endl; return FALSE; } else return TRUE; }void Cylinder::OutputCylinder() { cout<<"Result : "<<PI * r * r * h<<endl; }int main() { Cylinder cylinder; int i = 0; while(i != 3) { cylinder.SetCylinderValue(); if(!cylinder.Check()) { i++; continue; } else { cylinder.OutputCylinder(); break; } } if(i == 3) cout<<"You have no chance to continue!"<<endl; }
  • 2楼网友:堕落奶泡
  • 2021-04-27 01:55
#include <cstdio> int main() {     double radius = -1.0, height = -1.0;     while(radius <= 0.0) {         printf("请输入圆柱体的半径");         scanf("%lf",&radius);     }     while(height <= 0.0) {         printf("请输入圆柱体的高度");         scanf("%lf",&height);     }     printf("你输入的圆柱体 半径 %lf, 高度 %lf, 体积为 %lf\n", radius, height, PI*radius*radius*height);     return 0; }
  • 3楼网友:摧毁过往
  • 2021-04-27 01:21
#include <iostream> #define PI 3.1415926 using namespace std;int main(void) { float a,b; int i = 0; while (i < 3) { i++; cout<<"输入半径"; cin>>a; cout<<"输入高度:"; cin>>b; if (a>0 && b>0) break; else { if (i>=3) exit(1); cout<<"数据有误,请重新输入"<<endl; } } cout<<"体积:"<<PI*a*a*b<<endl; return 0; }
  • 4楼网友:懂得ㄋ、沉默
  • 2021-04-27 00:14

楼主让你看看 goto语句的魅力

#include<iostream> #define pi 3.14 using namespace std; int main() { float r,h,n=3,v; cout<<" 请你输入半径,高度:"<<endl; cin>>r>>h; if(r<=0&&h<=0) goto to1; else goto to2; to1:to: { cout<<"你还有"<<n<<"次机会"<<endl; cin>>r>>h; } if(r<=0&&h<=0&&n!=1) { --n; goto to; } else if(r>0&&h>0&&n<5) { to2: v=2*pi*h*r*r; cout<<"圆柱体体积:"<<v<<endl; } else cout<<"抱歉你几次都没有做对,输入错误"<<endl; return 0;

}

  • 5楼网友:啵啵桃汀
  • 2021-04-27 00:05
以下代码包括了球、圆柱和圆锥的表面积和体积的计算,做一下修改即可源程序与注释:#include "iostream.h" //库函数#include “math.h”class Circle //基类圆 { public: //公共成员double r; public: void print() //输出圆的半径 { cout<<"半径为:"<<r<<endl; } Circle(double x)//圆的构造函数 { r=x; } double GetR()//获取圆的半径 { return r; //返回圆的半径 } }; class Sphere:public Circle//球类 { public: //公共成员Sphere(double x):Circle(x)//球的构造函数 { } double GetTheSphereArea()//获取球的表面积 { double R=GetR(); return (4*3.14*R*R); //球的表面积公式} double GetTheSphereVolume()//获取球的体积 { double R=GetR(); return ((3.14*R*R*R)*4/3); //球的体积公式} }; class Cylinder:public Circle//圆柱类 { public: double h; Cylinder(double x ,double y):Circle(x)//圆柱类的构造函数 { h=y; } double GetTheCylinderArea()//获圆柱类的表面积 { double R=GetR(); return (2*3.14*R*R+2*3.14*R*h); //圆柱表面积公式} double GetTheCylinderVolume()//获圆柱类的体积 { double R=GetR(); return (3.14*R*R*h); //圆柱体积公式} }; class Vec:public Circle//圆锥类 { public: double h; Vec(double x ,double y):Circle(x)//圆锥类的构造函数 { h=y; } double GetTheVecArea()//获圆锥类的表面积 { double R=GetR(); return (3.14*R*R+3.14*R*sqrt(R*R+h*h));//表面积 S=π*r^2+πrl (l为母线长) } double GetTheVecVolume()//获圆锥类的体积 { double R=GetR(); return (3.14*R*R*h)/3; } }; void main()//主函数 { Sphere Sphere1(2.5);//初始球类对象 double a=Sphere1.GetTheSphereArea(); //获取球的表面积cout<<"(1)球的表面积为"<<a<<endl; a=Sphere1.GetTheSphereVolume(); //获取球的体积cout<<" 球的体积为"<<a<<endl; Cylinder Cylinder1(2,3);//初始圆柱类对象double b=Cylinder1.GetTheCylinderArea(); //圆柱的表面积cout<<"(2)圆柱的表面积为"<<b<<endl; b=Cylinder1.GetTheCylinderVolume(); //圆柱的体积cout<<" 圆柱的体积为"<<b<<endl; Vec Vec1(2,3);//初始圆锥类对象double c=Vec1.GetTheVecArea(); //圆锥的表面积cout<<"(3)圆锥的表面积为"<<c<<endl; c=Vec1.GetTheVecVolume(); //圆锥的体积cout<<" 圆锥的体积为"<<c<<endl; }
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息!
大家都在看
推荐信息