常见几何图形的C实现

 

#include <iostream>

#include "iomanip"

#include <math.h>

using namespace std;

 

class Triangle

{

private:

int A;

int B;

int C;

public:

Triangle(){};

Triangle(int x, int y, int z)

{

A = x;

B = y;

C = z;

}

void    EnterABC(int x, int y, int z);

float  getArea();

/*

s= a+b+c/2;

sqrt(s*(s-a)*(s-b)*(s-c));

*/

int    getZC();

bool   IsTriangle(); 

};

 

 

 

int Triangle::getZC()

{

return A + B + C;

}

 

void Triangle::EnterABC(int x, int y, int z)

{

A = x;

B = y;

C = z;

}

 

bool Triangle::IsTriangle()

{

if(    A + B > C || B + C > A || C + A > B)

return 1;

else

return 0;

}

 

float Triangle::getArea()

{

float s =  A + B + C;

return sqrt(s*(s-A)*(s-B)*(s-C));

}

 

void main()

{

Triangle m_Triangle;

int x, y, z;

cout<<"请输入三角形的三条边:"<<endl;

cout<<"边1(Enter键确认):";

cin>>x;

 

cout<<"边2(Enter键确认):";

cin>>y;

 

cout<<"边3(Enter键确认):";

cin>>z;

 

m_Triangle.EnterABC(x,y,z);

 

cout<<endl;

cout<<"结果描述如下:"<<endl;

if( m_Triangle.IsTriangle() )

{

cout<<"-----------------------三条边:"<<setw(5)<<x<<setw(5)<<y<<setw(5)<<z<<",能构成三角形"<<endl;

cout<<"-----------------------三角形周长为:"<<m_Triangle.getZC()<<endl;

cout<<"-----------------------三角形面积为:"<<m_Triangle.getArea()<<endl;

}

else

{

cout<<"-----------------------三条边:"<<setw(5)<<x<<setw(5)<<y<<setw(5)<<z<<",不能构成三角形!!!"<<endl;

cout<<"-----------------------三角形周长为:空"<<endl;

cout<<"-----------------------三角形面积为:空"<<endl;

}

}

 

正方形、圆形、长方形面积:

#include <iostream>

using namespace std;

class shape {
    public:
  virtual double area()=0;
};

class square:public shape{  //正方形
    protected:
  double H;
    public:
  square(double i) {H=i;}
  double area() {return H*H;}
};

class circle:public shape{
    protected:
  double r;
    public:
  circle(double i) {r=i;}
  double area() {return r*r*3.1415926;}
};

class rectangle:public square{
    protected:
  double W;
    public:
  rectangle(double w,double h):square(h)
  {
   W=w;
  }
  double area() {return H*W;}
};

double total(shape * s[],int n){
 double sum=0.0;
 for(int i=0;i<n;i++) sum=sum+s[i]->area();
 return sum;
}

void main(){
 shape *s[3];
 s[0]=&square(4);
    s[1]=&circle(5);
 s[2]=&rectangle(5, 3);
 for(int i=0;i<3;i++)
  cout<<"S["<<i<<"]的面积是:"<<s[i]->area()<<endl;
 double sum=total(s,3);
 cout<<"总的面积是:"<<sum<<" !/n";
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值