C/C++

Помогите по программированию!!! с++ используя структуры

Каждая точка на евклидовой плоскости координатами xi, yi. Расстояние между двумя точками вычисляется по формуле
d=√((x_1-x_2 )^2+(y_1-y_2 )^2 )
Площадь треугольника вычисляется по формуле Герона
S =√(p(p-a)(p-b)(p-c)),
где р - полупериметр, a, b и с - длины соответствующих сторон.
Используя типы данных запись с именем Punct и Triunghi, напишите программу, которая считывает с клавиатуры информацию о n треугольниках (n<10) и выводит на экран:
площадь каждого треугольника;
координаты вершин треугольника, площадь которого минимальна;
#include <algorithm>
#include <iostream>
#include <cmath>
#include <vector>
using namespace std;
struct Punct {
double x, y;
Punct() : x(0), y(0) {}
Punct(const double x, const double y) : x(x), y(y) {}
double length(const Punct& p)const {
return sqrt(pow(p.x - x, 2) + pow(p.y - y, 2));
}
friend ostream& operator<<(ostream& out, const Punct& t) {
return out << "{ " << t.x << ", " << t.y << " }";
}
};
struct Triunghi {
Punct a, b, c;
Triunghi() = default;
Triunghi(const Punct a, const Punct b, const Punct c) : a(a), b(b), c(c) {}
double area()const {
auto ab = a.length(b);
auto bc = b.length(c);
auto ca = c.length(a);
auto p = (ab + bc + ca) / 2;
return sqrt(p * (p - ab) * (p - bc) * (p - ca));
}
friend bool operator<(const Triunghi& x, const Triunghi& y) {
return x.area() < y.area();
}
friend bool operator==(const Triunghi& x, const Triunghi& y) {
return x.area() == y.area();
}
friend ostream& operator<<(ostream& out, const Triunghi& t) {
return out << "A: " << t.a << " B: " << t.b << " C: " << t.c;
}
};
void flush() {
cin.ignore(cin.rdbuf()->in_avail());
}
Punct set_coords(const char* name) {
Punct p;
cout << name << "x: ";
cin >> p.x;
flush();
cout << name << "y: ";
cin >> p.y;
flush();
puts("");
return p;
}
Triunghi create() {
auto a = set_coords("A");
auto b = set_coords("B");
auto c = set_coords("C");
system("cls");
return { a, b, c };
}
int main() {
cout << "Size: ";
size_t size;
cin >> size;
vector<Triunghi> box(size);
for (auto& triangle : box) {
triangle = create();
}
sort(box.begin(), box.end());
cout << box.front() << '\n';
for (auto i = 1U; i < size; ++i) {
if (box.front() == box.at(i)) cout << box.at(i) << '\n';
else break;
}
system("pause > nul");
}

P.S. Румынский гранит науки оказался не по зубам?
Андрей Пономарев
Андрей Пономарев
85 193
Лучший ответ
#include "iostream"
#include "cmath"
#include "cfloat"
using namespace std;
int main(){
struct Punct{float x,y;};
struct Triunghi{Punct a,b,c; float s(){
return abs((b.x-a.x)*(c.y-a.y)-(c.x-a.x)*(b.y-a.y))/2;}};
Triunghi tp,tr{0,0,0,1e9,1e9,0};
int n,s; cout<<"N: "; cin>>n;
for(int i=0;i< n;i++){cout<<"x1 y1 x2 y2 x3 y3: ";
cin>>tp.a.x>>tp.a.y>>tp.b.x>>tp.b.y>>tp.c.x>>tp.c.y;
s=tp.s(); cout<<"S="<< s<< endl; if(s<tr.s())tr=tp;}
cout<<"Max: "<<tr.a.x<<' '<<tr.a.y<<' '<<tr.b.x<<' '<<tr.b.y
<<' '<<tr.c.x<<' '<<tr.c.y<<", S="<< s<< endl;}
ВФ
Влад Фоминых
89 551
Влад Фоминых Поправка: не int n,s; а int n; float s;
#include <iostream>
#include <cmath>

#define LIMIT 10

using namespace std;

struct Punct
{
double x;
double y;
};

struct Triunghi
{
Punct A, B, C;
double a, b, c, p, S;
};

void entry_point(Punct &pu)
{
cout << "Entry x: ";
cin >> pu.x;
cout << "Entry y: ";
cin >> pu.y;
}

void find_a_b_c(Triunghi &tr)
{
tr.a = pow(pow(tr.A.x-tr.B.x,2.0) + pow(tr.A.y-tr.B.y,2.0),0.5);
tr.b = pow(pow(tr.B.x-tr.C.x,2.0) + pow(tr.B.y-tr.C.y,2.0),0.5);
tr.c = pow(pow(tr.C.x-tr.A.x,2.0) + pow(tr.C.y-tr.A.y,2.0),0.5);
}

void fint_p(Triunghi &tr)
{
tr.p = (tr.a + tr.b + tr.c) / 2.0;
}

void fins_S(Triunghi &tr)
{
tr.S = pow(tr.p*(tr.p-tr.a)*(tr.p-tr.b)*(tr.p-tr.c),0.5);
}

int main()
{
int i = 0;
char c = 'y';
Triunghi T[LIMIT - 1];
while (i < LIMIT - 1 && c == 'y')
{
cout << "A: " << endl;
entry_point(T[i].A);
cout << "B: " << endl;
entry_point(T[i].B);
cout << "C: " << endl;
entry_point(T[i].C);
find_a_b_c(T[i]);
fint_p(T[i]);
fins_S(T[i]);
cout << 'S' << i << " = " << T[i].S << '.' << endl;
i++;
if ( i < LIMIT - 1 )
{
cout << "For continue entry \'y\' and ENTER: ";
cin >> c;
}
}

int mt = 0; // find min S
for (int x = 0; x < i; x++)
{
if (T[x].S < T[mt].S) mt = x;
}
cout << "A(" << T[mt].A.x << ',' << T[mt].A.y << ')' << endl;
cout << "B(" << T[mt].B.x << ',' << T[mt].B.y << ')' << endl;
cout << "C(" << T[mt].C.x << ',' << T[mt].C.y << ')' << endl;

return 0;
}
Николай Мусалимов Не знаю, есть ли ошибки в коде. Подумай над этим сама.
Николай Мусалимов Идёт по очереди по точкам A,B,C ввод координат треугольников. Чтобы ввести координаты нового треугольника, вводишь y.