以下代码包括了球、
圆柱和
圆锥的
表面积和体积的计算,做一下修改即可源程序陪瞎嫌与注释:#include "iostream.h" //库函数#include “math.h”class Circle //基类圆 { public: //公共成员double rpublic: 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 hCylinder(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 hVec(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<<endla=Sphere1.GetTheSphereVolume()//获取球的体积cout<<" 球的体积为"<<a<<endlCylinder Cylinder1(2,3)//初始圆柱类对象double b=Cylinder1.GetTheCylinderArea()//圆柱的表面积cout<<"(2)圆柱的表面积为"<<b<<endlb=Cylinder1.GetTheCylinderVolume()//圆柱的体积cout<<" 圆柱的体积为"<<b<<endlVec Vec1(2,3)//初始神族圆锥类对象double c=Vec1.GetTheVecArea()//圆锥的表面积cout<<"(3)圆锥的表面积为"<<c<<endlc=Vec1.GetTheVecVolume()//圆锥的芦手体积cout<<" 圆锥的体积为"<<c<<endl}#include<stdio.h>
#define PI 3.14
main()
{
float r,h,l,s1,s2,v1,v2
printf("%s","please input the radius and high")
scanf("%f %f",&r,&h)
l=2*PI*r
s1=PI*r*r
s2=4*PI*r*r
v1=4*PI*r*r*r/3
v2=PI*r*r*h
printf("%d%d",r,h)
printf("circumference is:%5.2f\n",l)
printf("area of the circle is:%5.2f\n",s1)
printf("ball size is:%5.2f\n",s2)
printf("如掘ball volume is :%5.2f\n",v1)
printf("cylindrical volume is :%5.2f\n",v2)
getch()
}输笑笑入数据的时候一渣升核定要用实数类型,用空格分开.
import java.io.*
public class Cal
{
public static void main(String[] args)
{
float height=0
float r=0
double area=0
try
{
while(true)
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in))
System.out.println ("搏蔽请输入圆的坦银陆高让顷:")
try
{
height=Float.parseFloat(br.readLine())
System.out.println ("请输入圆的半径:")
r=Float.parseFloat(br.readLine())
area=(r*r*Math.PI)
System.out.println ("面积为:"+area)
break
}
catch(Exception ex)
{
System.out.println ("请输入数字!")
}
}
}
catch(Exception ex)
{
ex.printStackTrace()
}
}
}
评论列表(0条)