#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";
}