用c++编写图形的面积与体积的计算!

2024-11-24 19:07:10
推荐回答(3个)
回答1:

#include
#include
using namespace std;
//------------------------------------------------------------------------------
float AreaSquare();//正方形
float AreaRectangle();//长方形
float AreaRtriangle();//直角三角
float AreaCircle();//圆
float VolumeCube();//立方体
float VolumeSphere();//球体
void FlatArea(const char&,bool&);//平面图形面积
void ThreeDArea(const char&,bool&);//立体图形体积
const double PI=3.1415;
//------------------------------------------------------------------------------
int main()
{
char ch='3',ch2='5',ch3='3';
bool flag=1;//退出标记,为0退出或返回上级

cout<<"Input Corresponding Number \n";
do
{
cout<<"\n(1)Area of Shape (2) Volumes of Solids (3) Exit \n";
cin>>ch;
switch(ch)
{
case '1':do{
cout<<"\n(1)Square (2) Rectangle (3) Right-angled triangle ";
cout<<"(4) Circle (5) Return \n";
cin>>ch;
FlatArea(ch,flag);
}while(flag);
flag=1;//保证flag为1,不至于影响后面的使用
break;

case '2':do{
cout<<"\n(1) Cube (2) Sphere (3) Return \n";
cin>>ch;
ThreeDArea(ch,flag);
}while(flag);
flag=1;
break;

default: return 0;
}
}while(flag);
system("pause");
return 0;
}
//==============================================================================
float AreaSquare()
{
unsigned L=0;
cout<<"Please Input length:\n";
cin>>L;
return L*L;
}

float AreaRectangle()
{
unsigned L=0,W=0;
cout<<"Please Input length and width:\n";
cin>>L>>W;
return L*W;
}

float AreaRtriangle()
{
unsigned L=0,W=0;
cout<<"Please Input length and width:\n";
cin>>L>>W;
return L*W/2;
}

float AreaCircle()
{
unsigned R=0;
cout<<"Please Input radius:\n";
cin>>R;
return PI*R*R;
}

float VolumeCube()
{
unsigned L=0;
cout<<"Please Input length:\n";
cin>>L;
return L*L*L;
}

float VolumeSphere()
{
unsigned R=0;
cout<<"Please Input radius:\n";
cin>>R;
return 4*PI*R*R*R/3;
}

void FlatArea(const char& ch,bool& flag)//平面图形面积
{
switch(ch)
{
case '1':cout<<"Area :"< case '2':cout<<"Area :"< case '3':cout<<"Area :"< case '4':cout<<"Area :"< default:flag=0;
}
}

void ThreeDArea(const char& ch,bool& flag)//立体图形体积
{
switch(ch)
{
case '1':cout<<"Volume :"< case '2':cout<<"Volume :"< default:flag=0;
}
}
//------------------------------------------------------------------------------
应该还有许多方法,比如可以用类编写,这样更符合c++的标准,以后也能重用此类代码!不过你懂了的话,那是很好改的!

回答2:

数学公式...套下..

回答3:

很简单