double Triangle::Square() { double a = this->a.distance(this->b), b = this->b.distance(this->c), c = this->a.distance(this->c), p = (a+b+c)/2; return sqrt(p*(p-a)*(p-b)*(p-c)); }
int main() { Vertice a, b , c; cin >> a >> b >> c; Triangle t(a,b,c); cout << t.Perimetr() << endl << t.Square() << endl; }
Answers & Comments
#include <math.h>
using namespace std;
class Vertice {
public:
double x, y;
friend istream &operator>>(istream &is, Vertice &v) {
is >> v.x >> v.y ;
}
double distance(Vertice &w);
};
double Vertice::distance(Vertice &w) {
return sqrt( pow(this->x-w.x,2) + pow(this->y-w.y,2));
}
class Triangle {
public:
Vertice a, b, c;
Triangle(Vertice v, Vertice w, Vertice u);
double Square();
double Perimetr();
};
Triangle::Triangle(Vertice v, Vertice w, Vertice u) {
this->a = v, this->b = w, this->c = u;
}
double Triangle::Perimetr() {
return this->a.distance(this->b) + this->a.distance(this->c) + this->b.distance(this->c);
}
double Triangle::Square() {
double a = this->a.distance(this->b), b = this->b.distance(this->c), c = this->a.distance(this->c),
p = (a+b+c)/2;
return sqrt(p*(p-a)*(p-b)*(p-c));
}
int main() {
Vertice a, b , c;
cin >> a >> b >> c;
Triangle t(a,b,c);
cout << t.Perimetr() << endl << t.Square() << endl;
}
//язык c++, ООП