chenchenfan
幼苗
共回答了16个问题采纳率:62.5% 举报
#include
#include
#include
#include
#define PI 3.14159
using namespace std;
class Shape
{
public:
virtual float getArea()=0;//纯虚函数
};
class Circle:public Shape
{
float x,y,r;
public:
Circle(float argx=0,float argy=0,float argr=0);
float getArea();
float getR();
};
Circle::Circle(float argx,float argy,float argr)
{
x=argx;y=argy;r=argr;
}
float Circle::getArea()
{
return PI*r*r;
}
float Circle::getR()
{
return r;
}
class Rectangle:public Shape
{
float x1,x2,y1,y2;
public:
Rectangle(float argx1=0,float argy1=0,float argx2=0,float argy2=0);
float getArea();
};
Rectangle::Rectangle(float argx1,float argy1,float argx2,float argy2)
{
x1=argx1;y1=argy1;x2=argx2;y2=argy2;
}
float Rectangle::getArea()
{
float length,width;
length=x1>x2 ? x1-x2 : x2-x1;
width=y1>y2 ? y1-y2 : y2-y1;
return length*width;
}
class Triangle:public Shape
{
float x1,x2,y1,y2,x3,y3;
public:
Triangle(float argx1=0,float argy1=0,float argx2=0,float argy2=0,float argx3=0,float argy3=0);
float getArea();
};
Triangle::Triangle(float argx1,float argy1,float argx2,float argy2,float argx3,float argy3)
{
x1=argx1;y1=argy1;x2=argx2;y2=argy2;x3=argx3;y3=argy3;
}
float Triangle::getArea()
{
float a,b,c,s;
//分别求三边
a=sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
b=sqrt((x1-x3)*(x1-x3)+(y1-y3)*(y1-y3));
c=sqrt((x2-x3)*(x2-x3)+(y2-y3)*(y2-y3));
s=(a+b+c)/2;
return sqrt(s*(s-a)*(s-b)*(s-c)); //海伦公式求三角形面积
}
void main()
{
char shape;
Shape *ps;
float x1,y1,x2,y2,x3,y3,r;
ifstream inf("shape.txt");
if(!inf)
{
couty1>>r;
Circle c(x1,y1,r);
coutx2>>y2>>x3>>y3;
Triangle t(x1,y1,x2,y2,x3,y3);
cout
1年前
5