#include <iostream>
#include"math.h"
using namespace std;
class triangle
{
public:
void setABC(double x,double y,double z);
double perimeter();
double area();
private:
double a,b,c;
};
int main()
{
triangle tril;
tril.setABC(4,5,6);
cout<<"三角形的周长为:"<<tril.perimeter()<<'\t'<<"面积为:"<<tril.area()<<endl;
return 0;
}
void triangle::setABC(double x,double y,double z)
{
a=x;
b=y;
c=z;
}
double triangle::perimeter()
{
int C;
C=a+b+c;
return C;
}
double triangle::area()
{
int p=(a+b+c)/2;
int s;
s=sqrt((p*(p-a)*(p-b)*(p-c)));
return s;
}
运行结果:
#include <iostream>
#include "stdbool.h"
#include"math.h"
using namespace std;
class triangle
{
public:
void setA(double x)
{a=x;}
void setB(double y)
{b=y;}
void setC(double z)
{c=z;}
double getA()
{return a;}
double getB()
{return b;}
double getC()
{return c;}
bool istriangle();
double perimeter()
{
return (a+b+c);
}
double area()
{
double s;
double p;
p=(a+b+c)/2;
return sqrt((p*(p-a)*(p-b)*(p-c)));
}
private:
double a,b,c;
};
bool triangle::istriangle()
{
if(a+b>c&&a+c>b&&b+c>a)
return true;
else
return false;
}
int main()
{
triangle tril;
double x,y,z;
cout<<"请输入三角形三边";
cin>>x>>y>>z;
tril.setA(x);
tril.setB(y);
tril.setC(z);
if(tril.istriangle())
{
cout<<"三条边为:"<<tril.getA()<<','<<tril.getB()<<','<<tril.getC()<<endl;
cout<<"三角形的周长为:"<<tril.perimeter()<<'\t'<<"面积为:"<<tril.area()<<endl;
}
else
cout<<"不能构成三角形"<<endl;
return 0;
}
运行结果:
#include <iostream>
#include"math.h"
using namespace std;
class triangle
{
public:
triangle(double x,double y,double z);
void showmessage()
{
cout<<a<<' '<<b<<' '<<c<<endl;
}
private:
double a,b,c;
};
triangle::triangle(double x,double y,double z)
{
a=x;
b=y;
c=z;
}
int main()
{
triangle tri(7,8,9);
tri.showmessage();
return 0;
}
运行结果: