Другие языки программирования и технологии

помогите написать программу на c++

Упорядочить три данные точки на плоскости А (ах, ау), B(bх, bу),
А (cх, cу) по неубыванию расстояния от точки до начала координат.
помогите написать программу на c++
Юрий Лосев
Юрий Лосев
109
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
class Point {
public:
Point() : x_(0), y_(0), name_("O"s) { }
explicit Point(string& name) : x_(0), y_(0), name_(name) { }
double distance()const {
return sqrt(x_ * x_ + y_ * y_);
}
private:
double x_;
double y_;
string name_;
friend bool operator<(const Point& a, const Point& b) {
return a.distance() <= b.distance();
}
friend istream& operator>>(istream& inp, Point& point) {
cout << point.name_ << "x: ";
inp >> point.x_;
cout << point.name_ << "y: ";
inp >> point.y_;
return inp;
}
friend ostream& operator<<(ostream& out, const Point& point) {
out << point.name_ << "x: " << point.x_ << '\t'
<< point.name_ << "y: " << point.y_ << '\t'
<< " distance: " << point.distance() << '\n';
return out;
}
};
Point input(string&& name) {
Point tmp(name);
cin >> tmp;
return tmp;
}
int main() {
vector<Point> plane;
plane.emplace_back(input("A"s));
plane.emplace_back(input("B"s));
plane.emplace_back(input("C"s));
sort(plane.begin(), plane.end());
for (const auto &point : plane) cout << point;
system("pause");
}
КШ
Кристиан Шварц
58 348
Лучший ответ
Я тут написал программульку, которая делает примерно то, что вам надо, но ее надо еще малость доработать. Исходный код тут: https://pastebin.com/Lefd3sja

Работает так:
$ ./app
Введите координаты точки
X: 4
Y: 3
Введите координаты точки
X: 4
Y: 3
Введите координаты точки
X: 1
Y: 2
Введите координаты точки
X: 5
Y: 3
Введите координаты точки
X: d
Введен мусор вместо Х!
Точка ( 4, 3 ) дистанция 5
Точка ( 4, 3 ) дистанция 5
Точка ( 1, 2 ) дистанция 2.23607
Точка ( 5, 3 ) дистанция 5.83095
Сортировка по дистанции до начала координат
Точка ( 1, 2 ) дистанция 2.23607
Точка ( 4, 3 ) дистанция 5
Точка ( 4, 3 ) дистанция 5
Точка ( 5, 3 ) дистанция 5.83095
#include <iostream>
#include <windows.h>
#include <vector>
#include <algorithm>
#include <iomanip>

using namespace std;

int main()
{
SetConsoleCP(1251);
SetConsoleOutputCP(1251);
system("color 0A");

cout << "Укажите количество точек ";
size_t n, c = 0u;
cin >> n;
vector<pair<double, double>> v(n);
auto filler = [c]() mutable
{
cout << "x и y точки №" << ++c << " ";
double x, y;
cin >> x >> y;
return make_pair(x, y);
};
auto predicate = [](pair<double, double> p1, pair<double, double> p2)
{
auto len_left = p1.first * p1.first + p1.second * p1.second;
auto len_right = p2.first * p2.first + p2.second * p2.second;
return len_left <= len_right;
};
cout << "Введите координаты точек на плоскости" << endl;
generate(v.begin(), v.end(), filler);
sort(v.begin(), v.end(), predicate);
cout << "Упорядеченная последовательность точек" << endl;
cout << setw(8) << "x" << setw(8) << "y" << endl;
cout << endl;
for (const auto &t : v)
{
cout << setw(8) << t.first << setw(8) << t.second << endl;
}

system("pause");
return 0;
}