⑴ 用c語言求圓錐的圓面積和體積
#include<iostream>
#include<cmath>
#define N 3.1415
using
namespace std;
class Height
{
private:
float
h;
public:
Height(float h):h(h)
{}
float getheight()
{
return
h;
}
};
class Circle
{
private:
float
radius;
public:
Circle(float r):radius(r)
{}
float
getradius();
float area();
};
float
Circle::getradius()
{
return radius;
}
float
Circle::area()
{
return N*radius*radius;
}
class cone:public
Height,public Circle
{
private:
float line;
public:
cone(float
h,float r):Height(h),Circle(r)
{}
void getline();
float
surarea();
float volume();
void show();
};
void
cone::getline()
{
float a=getradius();
float
b=getheight();
line=sqrt(a*a+b*b);
}
float
cone::surarea()
{
return area()+N*getradius()*line;
}
float
cone::volume()
{
return area()*getheight()/3;
}
void
cone::show()
{
cout<<"圓錐的表面積為:"<<surarea()<<endl;
cout<<"圓錐的體積為:"<<volume()<<endl;
}
int
main()
{
cone
A(4,3);
A.getline();
A.surarea();
A.volume();
A.show();
return
0;
}
⑵ 用c語言工程求長方體的體積,表面積,周長.
#include "stdafx.h"
float chang,kuang,gao;
void fun1(){
printf("請輸入長,寬,高:");
scanf("%f",&chang);
scanf("%f",&kuang);
scanf("%f",&gao);
}
float volume(){
return chang*kuang*gao;
}
float area(){
return 2*(chang*kuang+chang*gao+kuang*gao);
}
float perimeter(){
return 4*(chang+kuang+gao);
}
int main(int argc, char* argv[])
{
fun1();
printf("體積為:%f",volume());
printf("\n面積為:%f",area());
printf("\n周長為:%f\n",perimeter());
return 0;
}