Сформировать массив структур, каждый элемент которого содержит
следующие поля:
координаты вершин треугольника на плоскости (вводим).
площадь треугольника;
Для каждого треугольника определить площадь. Рассортировать массив
структур по площади.
C/C++
Создать структуру с++
#include <algorithm>
#include <iostream>
#include <array>
using namespace std;
double coord(istream& inp, const char* msg) {
cout << msg;
double value;
inp >> value;
inp.ignore(inp.rdbuf()->in_avail());
return value;
}
struct Point {
double x, y;
Point() : x(0), y(0) {}
double length(const Point& p)const {
return sqrt(pow(x - p.x, 2) + pow(y - p.y, 2));
}
friend istream& operator>>(istream& inp, Point& p) {
cin >> p.x >> p.y;
return inp;
}
friend ostream& operator<<(ostream& out, const Point& p) {
return out << p.x << ' ' << p.y;
}
};
struct Triangle {
Point a, b, c;
double perimeter()const {
return (a.length(b) + b.length(c) + c.length(a)) / 2;
}
double area()const {
auto p = perimeter() / 2;
return sqrt(p * (p - a.length(b)) * (p - b.length(c)) * (p - c.length(a)));
}
bool exist()const {
auto p = perimeter() / 2;
return (p - a.length(b)) * (p - b.length(c)) * (p - c.length(a)) > 0;
}
friend istream& operator>>(istream& inp, Triangle& t) {
auto x = 0;
do {
if (x++) cout << "Треугольник не существует! \nВведите координаты треугольника: ";
inp >> t.a >> t.b >> t.c;
} while (!t.exist());
return inp;
}
friend ostream& operator<<(ostream& out, const Triangle& t) {
return out << t.a << ' ' << t.b << ' ' << t.c << " = " << t.area();
}
friend bool operator<(const Triangle& a, const Triangle& b) {
return a.area() < b.area();
}
};
int main() {
system("chcp 1251 > nul");
array<Triangle, 5> box;
for (auto& x : box) {
cout << "Введите координаты треугольника: ";
cin >> x;
}
sort(box.begin(), box.end());
puts("Отсортированный массив:");
for (const auto& x : box) cout << x << '\n';
system("pause > nul");
}
#include <iostream>
#include <array>
using namespace std;
double coord(istream& inp, const char* msg) {
cout << msg;
double value;
inp >> value;
inp.ignore(inp.rdbuf()->in_avail());
return value;
}
struct Point {
double x, y;
Point() : x(0), y(0) {}
double length(const Point& p)const {
return sqrt(pow(x - p.x, 2) + pow(y - p.y, 2));
}
friend istream& operator>>(istream& inp, Point& p) {
cin >> p.x >> p.y;
return inp;
}
friend ostream& operator<<(ostream& out, const Point& p) {
return out << p.x << ' ' << p.y;
}
};
struct Triangle {
Point a, b, c;
double perimeter()const {
return (a.length(b) + b.length(c) + c.length(a)) / 2;
}
double area()const {
auto p = perimeter() / 2;
return sqrt(p * (p - a.length(b)) * (p - b.length(c)) * (p - c.length(a)));
}
bool exist()const {
auto p = perimeter() / 2;
return (p - a.length(b)) * (p - b.length(c)) * (p - c.length(a)) > 0;
}
friend istream& operator>>(istream& inp, Triangle& t) {
auto x = 0;
do {
if (x++) cout << "Треугольник не существует! \nВведите координаты треугольника: ";
inp >> t.a >> t.b >> t.c;
} while (!t.exist());
return inp;
}
friend ostream& operator<<(ostream& out, const Triangle& t) {
return out << t.a << ' ' << t.b << ' ' << t.c << " = " << t.area();
}
friend bool operator<(const Triangle& a, const Triangle& b) {
return a.area() < b.area();
}
};
int main() {
system("chcp 1251 > nul");
array<Triangle, 5> box;
for (auto& x : box) {
cout << "Введите координаты треугольника: ";
cin >> x;
}
sort(box.begin(), box.end());
puts("Отсортированный массив:");
for (const auto& x : box) cout << x << '\n';
system("pause > nul");
}
Михаил Лабутин
спасибо большое
Михаил Лабутин
А не могли бы вы, пожалуйста, прокомментировать строки кода ?
Похожие вопросы
- Создать структуры в с++
- Создать структуру для описания членов вашей семьи
- Создать файл ABONENT.dat, содержащий записи следующей структуры: ФИО абонента; его номер телефона. на языке си++
- [C++] Помогите написать задачу на структуры!
- Программа на c++ не работает. Написать структуру и прочее. Подскажите, пожалуйста, что я делаю не так.
- Вложенные структуры. С++
- Помогите решить задачу на структуры в Си.
- Как правильно запихнуть в поле структуры саму структуру? C++
- Язык Си. Тема: структуры данных.
- С++ структуры. Помогите ппжпж жпж пж ж