C/C++

Задача по c++ на векторы. Часть программы написана. Нужны правки.

На входе нам даны координаты некоторых 5 векторов. Стоит задача находить площадь пятиугольника путём сложения площадей содержащихся в нём треугольников, вычисленных по теореме Герона.

Код набросали приблизительно такой. Можете предложить правки, как заставить программу работать? =)

#include <iostream>
#include <iomanip>
#include <cmath>

double x1, y1, x2, y2, x3, y3, x4, y4, x5, y5;
double a, b, c, d, e, f, g, S, s1, s2, s3, p1, p2, p3;

long double vekt ()
{
a = sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2));
b = sqrt(pow(x3 - x2, 2) + pow(y3 - y2, 2));
c = sqrt(pow(x4 - x3, 2) + pow(y4 - y3, 2));
d = sqrt(pow(x5 - x4, 2) + pow(y5 - y4, 2));
e = sqrt(pow(x1 - x5, 2) + pow(y1 - y5, 2));
f = sqrt(pow(x4 - x1, 2) + pow(y4 - y1, 2));
g = sqrt(pow(x3 - x1, 2) + pow(y3 - y1, 2));

return a, b, c, d, e, f, g;
}
int main()
{
std::cin >> x1 >> y1 >> x2 >> y2 >> x3 >> y3 >> x4 >> y4 >> x5 >> y5;

vekt (a, b, c, d, e, f, g);

p1 = (a + b + g) / 2;
p2 = (f + g + c) / 2;
p3 = (e + f + d) / 2;
s1 = sqrt(p1 * (p1 - a) * (p1 - b) * (p1 - g));
s2 = sqrt(p2 * (p2 - f) * (p2 - g) * (p2 - c));
s3 = sqrt(p3 * (p3 - e) * (p3 - f) * (p3 - d));
S = s1 + s2 + s3;

std::cout << S;

return 0;
}
#include <iostream>
#include <cmath>
using namespace std;
double coord(istream& inp, const char* msg) {
cout << msg;
double value;
cin >> value;
cin.ignore(numeric_limits<size_t>::max(), '\n');
return value;
}
struct Point {
double x, y;
Point() : x(0), y(0) {}
double length(const Point& p)const {
return sqrt(pow(p.x - x, 2) + pow(p.y - y, 2));
}
private:
friend istream& operator>>(istream& inp, Point& p) {
p.x = coord(inp, "x: ");
p.y = coord(inp, "y: ");
return inp;
}
};
struct Triangle {
Point a, b, c;
Triangle() = default;
Triangle(const Point& a, const Point& b, const Point& c) : a(a), b(b), c(c) {}
bool exist()const {
return argument() > 0;
}
double perimeter()const {
return a.length(b) + b.length(c) + c.length(a);
}
double area()const {
return sqrt(argument());
}
private:
double argument()const {
auto p = perimeter() / 2.0;
return p * (p - a.length(b)) * (p - b.length(c)) * (p - c.length(a));
}
};
Point point(const char* msg) {
cout << msg;
Point p;
cin >> p;
return p;
}
int main() {
auto a = point("Point A:\n");
auto b = point("Point B:\n");
auto c = point("Point C:\n");
auto d = point("Point D:\n");
auto e = point("Point E:\n");
Triangle abc(a, b, c);
Triangle acd(a, c, d);
Triangle ade(a, d, e);
if (abc.exist() && acd.exist() && ade.exist()) {
auto area = abc.area() + acd.area() + ade.area();
cout << "Area: " << area << '\n';
} else {
puts("The pentagon doesn't exist!");
}
system("pause > nul");
}
АИ
Алмас Исаев
53 962
Лучший ответ
Максим Трофимов Вы лучший. Спаисбо.