| 给你找到一个
#include<iostream>
#include<cmath>
#include<iomanip>
#ifndef CSHAPE_H
#define CSHAPE_h
usingnamespace std;
constfloat PI=3.1;
class CShape//CShape抽象类定义
{
public:
virtualfloat length()//图形长度
{
return 0.0;
}
virtualfloat area()//图形面积
{
return 0.0;
}
virtualfloat valum()//图形体积
{
return 0.0;
}
virtualfloat zc()//图形周长
{
return 0.0;
}
virtualvoid draw()=0;//描绘图形
};
class CPoint:virtualpublic CShape//CPoint类定义
{
protected:
float x,y;
public:
CPoint(float a,float b)
{
x=a;
y=b;
}
void setpoint(float a,float b)
{
x=a;
y=b;
}
void draw()
{
cout<<"点的坐标为:"<<"("<<x<<","<<y<<")"<<endl;
}
};
class CLine:virtualpublic CShape//线类定义
{
protected:
float x,y,z,k;
public:
CLine(float a,float b,float c,float d)
{
x=a;
y=b;
z=c;
k=d;
}
void setline(float a,float b,float c,float d)
{
x=a;
y=b;
z=c;
k=d;
}
float length()
{
return(sqrt((x-z)*(z-z)+(y-k)*(y-k)));
}
void draw()
{
cout<<"两坐标分别为:"<<"("<<x<<","<<y<<")"<<",("<<z<<","<<k<<")"<<endl;
}
};
class CRectangle:virtualpublic CShape//长方形类定义
{
protected:
float length,width;
public:
CRectangle(float l,float w)
{
length=l;
width=w;
}
void setrectangle(float l,float w)
{
length=l;
width=w;
}
float area()
{
return(length*width);
}
float zc()
{
return(2*(length+width));
}
void draw()
{
cout<<"长方形的长为:"<<length<<setw(10)<<"宽为:"<<width<<endl;
}
};
class CCircle:virtualpublic CShape//圆类定义
{
protected:
float radius;
public:
CCircle(float r)
{
radius=r;
}
void setcircle(float r)
{
radius=r;
}
float area()
{
return(PI*radius*radius);
}
float zc()
{
return(2*PI*radius);
}
void draw()
{
cout<<"圆的半径为:"<<radius<<endl;
}
};
class CCylinder:virtualpublic CShape//圆柱类定义
{
protected:
float heigth,radius;
public:
CCylinder(float r,float h)
{
heigth=h;
radius=r;
}
void setcylinder(float r,float h)
{
heigth=h;
radius=r;
}
float area()
{
return(PI*radius*radius+2*PI*radius);
}
float valum()
{
return(PI*radius*radius*heigth);
}
void draw()
{
cout<<"圆柱高度为:"<<heigth<<setw(10)<<"半径为:"<<radius<<endl;
}
};
class CCone:virtualpublic CShape//定义一个圆锥类
{
protected:
float mx,radius;
public:
CCone(float a,float b)
{
mx=a;
radius=b;
}
void setcone(float a,float b)
{
mx=a;
radius=b;
}
float valum()
{
return(PI*radius*radius*sqrt(mx*mx-radius*radius));
}
void draw()
{
cout<<"母线长为:"<<mx<<setw(10)<<"半径长为:"<<radius<<endl;
}
};
class CTriangle:virtualpublic CShape//三角形类定义
{
protected:
float x,y,z;
public:
CTriangle(float a,float b,float c)
{
x=a;y=b;z=c;
}
void settriangle(float a,float b,float c)
{
x=a;y=b;z=c;
}
float zc()
{
return(x+y+z);
}
float area()
{
float t=(x+y+z)/2;
return(sqrt((t-x)*(t-y)*(t-z)));
}
void draw()
{
cout<<"三角形三边长分别为:"<<x<<","<<y<<","<<z<<endl;
}
};
class CPolygon:virtualpublic CShape//多边形类定义
{
protected:
float x;
int y;
public:
CPolygon(float a,int b)
{
x=a;
y=b;
}
void setpolygon(float a,int b)
{
x=a;
y=b;
}
float area()
{
return(sqrt(3.0)*x*x*y/4);
}
float zc()
{
return(x*y);
}
void draw()
{
cout<<"多边形的一边长为:"<<x<<setw(10)<<"边数为:"<<y<<endl;
}
};
#endif
#include"CShape.h"
#include<iostream>
#include<iomanip>
usingnamespace std;
void main()
{
char d='y';
int i(0);
cout<<"欢迎进入本系统!";
cout<<"请选择服务:"<<endl;
cout<<" 1.点的计算。"<<endl;
cout<<" 2.线的计算。"<<endl;
cout<<" 3.圆的计算。"<<endl;
cout<<" 4.圆柱的计算。"<<endl;
cout<<" 5.圆锥的计算。"<<endl;
cout<<" 6.长方形的计算。"<<endl;
cout<<" 7.多边形的计算。"<<endl;
cout<<" 8.三角形的计算。"<<endl;
cout<<" 9.退出。"<<endl;
while(d=='Y'||d=='y')
{
cout<<"请输入你的选择:"<<endl;
cin>>i;
if(i==1)
{
CShape *pshape;
CPoint point(2.0,3.0);
float a,b;
cout<<"请输入点的坐标:";
cin>>a>>b;
point.setpoint(a,b);
pshape=&point;
pshape->draw();
}
elseif(i==2)
{
CShape *pshape;
CLine line(1.0,2.0,3.0,4.0);
float a,b,c,d;
cout<<"请依次输入两端点的坐标:";
cin>>a>>b>>c>>d;
line.setline(a,b,c,d);
pshape=&line;
pshape->draw();
cout<<"线的长度为:"<<pshape->length()<<endl;
}
elseif(i==3)
{
CShape *pshape;
CCircle circle(2.0);
float a;
cout<<"请输入圆的半径:";
cin>>a;
circle.setcircle(a);
pshape=&circle;
pshape->draw();
cout<<"圆的面积为;"<<pshape->area()<<setw(10)<<"圆的周长为;"<<pshape->zc()<<endl;
}
elseif(i==4)
{
CShape *pshape;
CCylinder yz(2.0,3.0);
float a,b;
cout<<"请输入圆柱的底面半径和高:";
cin>>a>>b;
yz.setcylinder(a,b);
pshape=&yz;
pshape->draw();
cout<<"圆柱的面积为:"<<pshape->area()<<setw(10)<<"圆柱的体积为:"<<pshape->valum()<<endl;
}
elseif(i==5)
{
CShape *pshape;
CCone cone(5.0,4.0);
float a,b;
cout<<"请输入母线长和底面半径:";
cin>>a>>b;
cone.setcone(a,b);
pshape=&cone;
pshape->draw();
cout<<"圆锥的体积为:"<<pshape->valum()<<endl;
}
elseif(i==6)
{
CShape *pshape;
CRectangle rectangle(3.0,2.0);
float a,b;
cout<<"请输入长方形的长和宽:";
cin>>a>>b;
rectangle.setrectangle(a,b);
pshape=&rectangle;
pshape->draw();
cout<<"长方形的面积为:"<<pshape->area()<<setw(10)<<"长方形的周长为:"<<pshape->zc()<<endl;
}
elseif(i==7)
{
CShape *pshape;
CPolygon polygon(2.0,4);
float a;
int b;
cout<<"请输入多边形的边长和边数:";
cin>>a>>b;
polygon.setpolygon(a,b);
pshape=&polygon;
pshape->draw();
cout<<"多边形的面积为:"<<pshape->area()<<setw(10)<<"周长为:"<<pshape->zc()<<endl;
}
elseif(i==8)
{
CShape *pshape;
CTriangle triangle(3.0,4.0,5.0);
float a,b,c;
cout<<"请输入三角形三边长:";
cin>>a>>b>>c;
triangle.settriangle(a,b,c);
pshape=▵
pshape->draw();
cout<<"三角形的周长为:"<<pshape->zc()<<"面积为:"<<pshape->area()<<endl;
}
elseif(i==9)
{
return;
}
else
{
cout<<"输入错误,请重新输入!";
}
cout<<"是否继续其他操作(Y/N)?"<<endl;
cin>>d;
}
}
| |