使用C++实现下述简单图形程序的类继承结构,画出类图。

使用C++实现下述简单图形程序的类继承结构,画出类图。,第1张

去csdn找下,csdn的博客也可以找,下载那可以找相关资料找找

网站:www.csdn.net

下载:download.csdn.net 注册个账号就能下载

博~~~客:blog.csdn.net

#include <iostream>

#include <cmath>

using namespace std

const double dPI=3.1415926

typedef enum

{

Exit=0,

Round=1,

Rect=2,

Tri=3,

Trap=4,

}shape

class CGraphicArea

{

public:

CGraphicArea()

~CGraphicArea()

double Area(double)

double Area(double width,double length)

double Area(double a,double b,double c)

double Area(double up,double down,double left,double right)

void Input( )

int Handle( )

int Output( )

private:

double *a

double area

int type

}

CGraphicArea::CGraphicArea(){

a = new double[5]

area = 0.0

type = -1

}

CGraphicArea::~CGraphicArea(){

delete a

}

double CGraphicArea::Area(double r)

{

area = dPI*r*r

}

double CGraphicArea::Area(double width,double length)

{

area = width*length

}

double CGraphicArea::Area(double a,double b,double c)

{

double p=(a+b+c)/2

area = sqrt(p*(p-a)*(p-b)*(p-c))

}

double CGraphicArea::Area(double up,double down,double left,double right)

{

double td=down-up

double area1=Area(td,left,right)

double h=area1*2/td

cout<<"------ h = "<<h<<endl

area = (up+down)*h/2

}

void CGraphicArea::Input()

{

cout<<endl<<"请依次输入要计算的图形:"<<endl

cout<<"圆代号1 矩形代号2 三角形代号3 梯形代号4"<<endl

cout<<"无效边长设定为-1,终止输入请输0"<<endl

cout<<"输入格式为:"<<endl

cout<<"图形:"<<endl<<"边1 边2 边3 边4"<<endl

cin>>type

cout<<"-------"<<type<<endl

if(type<0||type>4)

{

return

}

switch(type){

case Round:

cin>>a[0]

break

case Rect:

cin>>a[0]>>a[1]

break

case Tri:

cin>>a[0]>>a[1]>>a[2]

break

case Trap:

cin>>a[0]>>a[1]>>a[2]>>a[3]

break

}

}

//return count

int CGraphicArea::Handle()

{

switch(type)

{

case Round:

{

if(a[0]<0)

{

area=-1

break

}

area=Area(a[0])

break

}

case Rect:

{

if(a[0]<=0||a[1]<=0)

{

area=-1

break

}

area=Area(a[0],a[1])

break

}

case Tri:

{

if(a[0]<=0||a[1]<=0||a[2]<=0)

{

area=-1

break

}

if(a[0]+a[1]<a[2] || a[0]+a[2]<a[1] || a[1]+a[2]<a[0])

{

area=-1

break

}

area=Area(a[0],a[1],a[2])

break

}

case Trap:

{

if(a[0]<=0 || a[1]<=0 || a[2]<=0 || a[3]<=0)

{

area=-1

break

}

if( a[1]-a[0]+a[2]<a[3]

|| a[1]-a[0]+a[3]<a[2]

|| a[2]+a[3]<a[1]-a[0])

{

area=-1

break

}

area=Area(a[0],a[1],a[2],a[3])

break

}

case Exit:

return 0

default:

return 0

}

return 0

}

int CGraphicArea::Output() //注意这里

{

cout<<"图形\t"<<"面积"<<endl

switch(type)

{

case Round:

{cout<<"圆\t"break}

case Rect:

{cout<<"矩形\t"break}

case Tri:

{cout<<"三角形\t"break}

case Trap:

{cout<<"梯形\t"break}

case Exit:

exit(1)

default:{

break

}

}

if(area==-1)

cout<<"输入数据错误"<<endl

else

cout<<area<<endl

cout<<"------"<<endl

return 0

}

int main()

{

CGraphicArea *graphic

while(1){

graphic = new CGraphicArea

graphic->Input()

graphic->Handle()

graphic->Output()

graphic->~CGraphicArea()

}

}

这个是圆类package areapublic class Round {

private double radius//半径

public Round(){}

public Round(double radius){

this.radius=radius

}

public double getArea(){

return 3.141592653*radius*radius

}

public double getRadius() {

return radius

}

public void setRadius(double radius) {

this.radius = radius

}

public Round compareArea(Round round){

if(this.getArea()>=round.getArea()){

return this

}else

return round

}

}

这个是长方形类package areapublic class Oblang {

private double chang//长方形的长

private double kuan//长方形的宽

public Oblang(){}

public Oblang(double chang,double kuan){

this.chang=chang

this.kuan=kuan

}

/*

* 得到长方形的面积

*/

public double getArea(){

return chang*kuan

}

public double getChang() {

return chang

}

public void setChang(double chang) {

this.chang = chang

}

public double getKuan() {

return kuan

}

public void setKuan(double kuan) {

this.kuan = kuan

}

public Oblang compareArea(Oblang oblang){

if(this.getArea()>=oblang.getArea())

return this

else return oblang

}

}

这个是测试比较package area

public class CompareArea {

public static Object compareArea(Round round,Oblang oblang){

if(round.getArea()>=oblang.getArea())

return round//返回两个面积相比较大的一个的对象

else return oblang

} public static void main(String[] args) {

Oblang oblang1=new Oblang(3,5)

Oblang oblang2=new Oblang(3,8)

Round round1=new Round(3)

Round round2=new Round(5)

oblang1=oblang1.compareArea(oblang2)//得到最大的一个长方形面积对象

round1=round1.compareArea(round2)//得到最大的一个圆面积对象

Object o=compareArea(round1,oblang1)//得到最大的一个面积

Oblang ob

Round r

if(o instanceof Oblang){

ob=(Oblang)o

System.out.println("最大面积是长方形,长方形的长是="+ob.getChang()+"宽="+ob.getKuan()+"面积="+ob.getArea())

}

else if(o instanceof Round) {

r=(Round)o

System.out.println("最大面积是圆,圆的半径是="+r.getRadius()+"面积是="+r.getArea())

}

}}

以上全是我现在做的,可能不是很完善,希望对你有帮助!


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/12173449.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-21
下一篇 2023-05-21

发表评论

登录后才能评论

评论列表(0条)

保存