求一份C++程序
分别输入圆柱体的半径和高,求其体积;输入球半径,求其表面积;输入长方体的长、宽、高,求其体积。...
分别输入圆柱体的半径和高,求其体积;输入球半径,求其表面积;输入长方体的长、宽、高,求其体积。
展开
2个回答
2014-03-21
展开全部
#include <iostream>
using namespace std;
#define PI 3.14int main()
{
float cr,ch,cv;//分别是圆柱体的半径,高,和体积
float br,bs;//分别是球的半径和表面积
float ra,rb,rc,rv;//分别是长方体的长,宽,高和体积.
cout<<"输入圆柱体的半径和高:";
cin>>cr>>ch;
cv = PI*cr*cr*ch;
cout<<"圆柱体的体积为:"<<cv<<endl; cout<<"输入球的半径:";
cin>>br;
bs = 4*PI*br*br;
cout<<"球的表面积为:"<<bs<<endl; cout<<"输入长方体的长,宽和高:";
cin>>ra>>rb>>rc;
rv = ra*rb*rc;
cout<<"长方体的体积为:"<<rv<<endl;
return 0;
}
using namespace std;
#define PI 3.14int main()
{
float cr,ch,cv;//分别是圆柱体的半径,高,和体积
float br,bs;//分别是球的半径和表面积
float ra,rb,rc,rv;//分别是长方体的长,宽,高和体积.
cout<<"输入圆柱体的半径和高:";
cin>>cr>>ch;
cv = PI*cr*cr*ch;
cout<<"圆柱体的体积为:"<<cv<<endl; cout<<"输入球的半径:";
cin>>br;
bs = 4*PI*br*br;
cout<<"球的表面积为:"<<bs<<endl; cout<<"输入长方体的长,宽和高:";
cin>>ra>>rb>>rc;
rv = ra*rb*rc;
cout<<"长方体的体积为:"<<rv<<endl;
return 0;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2014-03-21
展开全部
#include<iostream.h>
//#include<ctype.h>
//h#include<stdlib.h>
//#include<dos.h>
#include<math.h>
#include<conio.h>
#define PI 3.14
class Shape
{
protected:
float length;
};
class TwoDimShape:public Shape
{
protected:
float width;
};
class ThreeDimShape:public TwoDimShape
{
protected:
float hight;
};
class PMJX:public TwoDimShape//“PMJX”为平面矩形缩写
{
public:
PMJX(){float x,y;cout<<"请输入矩形的长和宽:";cin>>x>>y;length=x;width=y;}
void get_area()
{
float m;if(length==width)cout<<"该矩形为正方形。"<<endl;
m=length*width;cout<<"面积为:"<<m<<endl;
}
void get_arround(){float l;l=2*(length+width);cout<<"周长为:"<<l<<endl;}
};
class PMSJX:public ThreeDimShape//“PMSJX”为平面三角形缩写
{
public:
PMSJX()
{
int i,count=0;float a[3];cout<<"请输入三角形的三边的长:";
for(i=0;i<3;i++)
{cin>>a[i];count++;}
length=a[0];width=a[1];hight=a[2];//这里以及下面的“length”、“width”、“hight”分别为三角形三边
}
void get_area()
{
double m,h;
while(1)
{
if(length>=width+hight||width>=length+hight||hight>=length+width)
{
cout<<"不合法的三角形,请从新输入:";
cin>>length>>width>>hight;
}
else break;
}
h=0.5*(length+width+hight);m=sqrt(h*(h-length)*(h-width)*(h-hight));
if(length==width&&length==hight)cout<<"该三角形为正三角形。"<<endl;
else if(length*length==width*width+hight*hight||width*width==length*length+hight*hight||hight*hight==length*length+width*width)
cout<<"该三角形为直角三角形。"<<endl;
else
if(length*length>=width*width+hight*hight||width*width>=length*length+hight*hight||hight*hight>=length*length+width*width)
cout<<"该三角形为钝角三角形。"<<endl;
else cout<<"该三角形为锐角三角形。"<<endl;
cout<<"面积为:"<<m<<endl;
}
void get_arround(){float l;l=length+width+hight;cout<<"周长为:"<<l<<endl;}
};
class PMYX//“PMYX”为平面圆形缩写
{
public:
PMYX(){cout<<"请输入圆半径:";cin>>R;}
void get_area(){double m;m=PI*R;cout<<"面积为:"<<m<<endl;}
void get_arround(){double l;l=2*PI*R;cout<<"周长为:"<<l<<endl;}
protected:
float R;
};
class PMTY:public TwoDimShape//椭圆类
{
public:
PMTY(){cout<<"请输入椭圆的长轴和短轴:";cin>>length>>width;}
void get_area()
{
double m,e;
m=PI*length*width;
e=sqrt(1+width*width/(length*length));
cout<<"面积为:"<<m<<"\n离心率为:"<<e<<endl;
}
};
class LFT:public ThreeDimShape//“LFT”为立方体形
{
public:
LFT(){float a,b,c;cout<<"请输入长、宽、高:";cin>>a>>b>>c;length=a;width=b;hight=c;}
void get_volume()
{
float v;v=length*width*hight;if(length==width&&length==hight)cout<<"该立体图形为正方体。"<<endl;
cout<<"体积为:"<<v<<endl;
}
void get_allarea(){float a;a=2*(length*width+length*hight+width*hight);cout<<"表面积为:"<<a<<endl;}
};
class YQ:protected PMYX//圆球类
{
public:
void get_volume(){double v;v=4*PI*R*R*R/3;cout<<"该圆球体积为:"<<v<<endl;}
void get_allarea(){double a;a=4*PI*R*R;cout<<"圆球的表面积为:"<<a<<endl;}
};
class ZHUIX:public ThreeDimShape //圆锥类
{
public:
ZHUIX(){cout<<"请输入圆锥底面半径和高:";cin>>R>>hight;}
void get_volume(){double v;v=PI*R*R*hight/3;cout<<"体积为:"<<v<<endl;}
void get_allarea()
{
double l,L,m,M;
l=2*PI*R;
L=sqrt(R*R+hight*hight);
M=PI*R*R;
m=(l/360)*PI*L*L+M;
cout<<"表面积为:"<<m<<endl;
}
protected:
float R;
};
class YZHU:public ThreeDimShape, protected PMYX //圆柱类
{
public:
YZHU(){cout<<"请输入圆柱的高:";cin>>hight;}
void get_volume(){double v;v=hight*PI*R*R;cout<<"体积为:"<<v<<endl;}
void get_allarea(){double m;m=2*PI*R*R+2*PI*R*hight;cout<<"表面积:"<<m<<endl;}
};
class XLZHUI:public ThreeDimShape //四棱锥类
{
public:
XLZHUI(){cout<<"请输入四棱锥的长、宽和高:";cin>>length>>width>>hight;}
void get_volume(){double v;v=length*width*hight/3;cout<<"体积为:"<<v<<endl;}
};
void main()
{
char c, i;
while(c!='n')
{
cout<<"请输入要求的图形代码或按N退出:\n"<<"1.矩形\t2.圆柱\t3.圆形\t4.三角形\n5.椭圆\t6.圆球\t7.圆锥\t8.立方体 9.四棱锥"<<endl;
i=getch();if(i>='1'&&i<='9')
{
switch(i)
{
case '1':{PMJX a;a.get_area();a.get_arround();break;}
case '2':{YZHU a;a.get_volume();a.get_allarea();break;}
case '3':{PMYX a;a.get_area();a.get_arround();break;}
case '4':{PMSJX a;a.get_area();a.get_arround();break;}
case '5':{PMTY a;a.get_area();break;}
case '6':{YQ a;a.get_volume();a.get_allarea();break;}
case '7':{ZHUIX a;a.get_volume();a.get_allarea();break;}
case '8':{LFT a;a.get_volume();a.get_allarea();break;}
case '9':{XLZHUI a;a.get_volume();break;}
}
cout<<"是否继续Y/N"<<endl;c=getch();
}else if(i=='n')break;
else continue;
}
}这是我刚学C++时写的,代码并不怎么好,但是可以计算很多图型,奏合着用吧。
//#include<ctype.h>
//h#include<stdlib.h>
//#include<dos.h>
#include<math.h>
#include<conio.h>
#define PI 3.14
class Shape
{
protected:
float length;
};
class TwoDimShape:public Shape
{
protected:
float width;
};
class ThreeDimShape:public TwoDimShape
{
protected:
float hight;
};
class PMJX:public TwoDimShape//“PMJX”为平面矩形缩写
{
public:
PMJX(){float x,y;cout<<"请输入矩形的长和宽:";cin>>x>>y;length=x;width=y;}
void get_area()
{
float m;if(length==width)cout<<"该矩形为正方形。"<<endl;
m=length*width;cout<<"面积为:"<<m<<endl;
}
void get_arround(){float l;l=2*(length+width);cout<<"周长为:"<<l<<endl;}
};
class PMSJX:public ThreeDimShape//“PMSJX”为平面三角形缩写
{
public:
PMSJX()
{
int i,count=0;float a[3];cout<<"请输入三角形的三边的长:";
for(i=0;i<3;i++)
{cin>>a[i];count++;}
length=a[0];width=a[1];hight=a[2];//这里以及下面的“length”、“width”、“hight”分别为三角形三边
}
void get_area()
{
double m,h;
while(1)
{
if(length>=width+hight||width>=length+hight||hight>=length+width)
{
cout<<"不合法的三角形,请从新输入:";
cin>>length>>width>>hight;
}
else break;
}
h=0.5*(length+width+hight);m=sqrt(h*(h-length)*(h-width)*(h-hight));
if(length==width&&length==hight)cout<<"该三角形为正三角形。"<<endl;
else if(length*length==width*width+hight*hight||width*width==length*length+hight*hight||hight*hight==length*length+width*width)
cout<<"该三角形为直角三角形。"<<endl;
else
if(length*length>=width*width+hight*hight||width*width>=length*length+hight*hight||hight*hight>=length*length+width*width)
cout<<"该三角形为钝角三角形。"<<endl;
else cout<<"该三角形为锐角三角形。"<<endl;
cout<<"面积为:"<<m<<endl;
}
void get_arround(){float l;l=length+width+hight;cout<<"周长为:"<<l<<endl;}
};
class PMYX//“PMYX”为平面圆形缩写
{
public:
PMYX(){cout<<"请输入圆半径:";cin>>R;}
void get_area(){double m;m=PI*R;cout<<"面积为:"<<m<<endl;}
void get_arround(){double l;l=2*PI*R;cout<<"周长为:"<<l<<endl;}
protected:
float R;
};
class PMTY:public TwoDimShape//椭圆类
{
public:
PMTY(){cout<<"请输入椭圆的长轴和短轴:";cin>>length>>width;}
void get_area()
{
double m,e;
m=PI*length*width;
e=sqrt(1+width*width/(length*length));
cout<<"面积为:"<<m<<"\n离心率为:"<<e<<endl;
}
};
class LFT:public ThreeDimShape//“LFT”为立方体形
{
public:
LFT(){float a,b,c;cout<<"请输入长、宽、高:";cin>>a>>b>>c;length=a;width=b;hight=c;}
void get_volume()
{
float v;v=length*width*hight;if(length==width&&length==hight)cout<<"该立体图形为正方体。"<<endl;
cout<<"体积为:"<<v<<endl;
}
void get_allarea(){float a;a=2*(length*width+length*hight+width*hight);cout<<"表面积为:"<<a<<endl;}
};
class YQ:protected PMYX//圆球类
{
public:
void get_volume(){double v;v=4*PI*R*R*R/3;cout<<"该圆球体积为:"<<v<<endl;}
void get_allarea(){double a;a=4*PI*R*R;cout<<"圆球的表面积为:"<<a<<endl;}
};
class ZHUIX:public ThreeDimShape //圆锥类
{
public:
ZHUIX(){cout<<"请输入圆锥底面半径和高:";cin>>R>>hight;}
void get_volume(){double v;v=PI*R*R*hight/3;cout<<"体积为:"<<v<<endl;}
void get_allarea()
{
double l,L,m,M;
l=2*PI*R;
L=sqrt(R*R+hight*hight);
M=PI*R*R;
m=(l/360)*PI*L*L+M;
cout<<"表面积为:"<<m<<endl;
}
protected:
float R;
};
class YZHU:public ThreeDimShape, protected PMYX //圆柱类
{
public:
YZHU(){cout<<"请输入圆柱的高:";cin>>hight;}
void get_volume(){double v;v=hight*PI*R*R;cout<<"体积为:"<<v<<endl;}
void get_allarea(){double m;m=2*PI*R*R+2*PI*R*hight;cout<<"表面积:"<<m<<endl;}
};
class XLZHUI:public ThreeDimShape //四棱锥类
{
public:
XLZHUI(){cout<<"请输入四棱锥的长、宽和高:";cin>>length>>width>>hight;}
void get_volume(){double v;v=length*width*hight/3;cout<<"体积为:"<<v<<endl;}
};
void main()
{
char c, i;
while(c!='n')
{
cout<<"请输入要求的图形代码或按N退出:\n"<<"1.矩形\t2.圆柱\t3.圆形\t4.三角形\n5.椭圆\t6.圆球\t7.圆锥\t8.立方体 9.四棱锥"<<endl;
i=getch();if(i>='1'&&i<='9')
{
switch(i)
{
case '1':{PMJX a;a.get_area();a.get_arround();break;}
case '2':{YZHU a;a.get_volume();a.get_allarea();break;}
case '3':{PMYX a;a.get_area();a.get_arround();break;}
case '4':{PMSJX a;a.get_area();a.get_arround();break;}
case '5':{PMTY a;a.get_area();break;}
case '6':{YQ a;a.get_volume();a.get_allarea();break;}
case '7':{ZHUIX a;a.get_volume();a.get_allarea();break;}
case '8':{LFT a;a.get_volume();a.get_allarea();break;}
case '9':{XLZHUI a;a.get_volume();break;}
}
cout<<"是否继续Y/N"<<endl;c=getch();
}else if(i=='n')break;
else continue;
}
}这是我刚学C++时写的,代码并不怎么好,但是可以计算很多图型,奏合着用吧。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询