C/C++
Здравствуйте помогите пожалуйста программирование c++
Выполните класс Rectangle, объекты которого расположены прямоугольными сторонами, параллельными осям координат двумерного пространства.
#include <iostream>
#include <cmath>
#include <limits>
#include <string>
#include <iomanip>
using namespace std;
double coord(const string& msg) {
cout << msg;
double value;
cin >> value;
cin.ignore(numeric_limits<double>::max(), '\n');
return value;
}
struct Point {
double x;
double y;
Point() : x(0), y(0) {}
Point(const double x, const double y) : x(x), y(y) {}
double length(const Point& p)const {
return sqrt(pow(p.x - x, 2) + pow(p.y - y, 2));
}
void set_x(const double x) {
this->x = x;
}
double get_x()const {
return x;
}
void set_y(const double y) {
this->y = y;
}
double get_y()const {
return y;
}
void move_x(const double dx) {
x += dx;
}
void move_y(const double dy) {
y += dy;
}
void move_xy(const double dx, const double dy) {
x += dx;
y += dy;
}
};
Point point(const string& name) {
Point p;
p.x = coord(name + "x: ");
p.y = coord(name + "y: ");
return p;
}
class Rectangle {
public:
Rectangle() = default;
Rectangle(const Point& a, const Point& b) : a(a), b(b) {
rotate();
}
void set_a(const Point& a) {
this->a = a;
}
Point get_a()const {
return a;
}
void set_b(const Point& b) {
this->b = b;
}
Point get_b()const {
return b;
}
double area()const {
return sqrt(pow(a.x - b.x, 2)) * sqrt(pow(a.y - b.y, 2));
}
double perimeter()const {
return (sqrt(pow(a.x - b.x, 2)) + sqrt(pow(a.y - b.y, 2))) * 2.0;
}
double diagonal()const {
return a.length(b);
}
private:
Point a;
Point b;
void rotate() {
if (a.x > b.x) swap(a.x, b.x);
if (a.y < b.y) swap(a.y, b.y);
}
};
int main() {
auto a = point("A");
auto b = point("B");
Rectangle rect{ a, b };
auto area = rect.area();
auto perimeter = rect.perimeter();
auto diagonal = rect.diagonal();
const auto w = 2U;
cout << fixed
<< "Diagonal: " << setprecision(w << 1) << area << '\n'
<< "Perimeter: " << setprecision(w) << area << '\n'
<< "Area: " << setprecision(w << 1) << area << '\n';
system("pause > nul");
}
#include <cmath>
#include <limits>
#include <string>
#include <iomanip>
using namespace std;
double coord(const string& msg) {
cout << msg;
double value;
cin >> value;
cin.ignore(numeric_limits<double>::max(), '\n');
return value;
}
struct Point {
double x;
double y;
Point() : x(0), y(0) {}
Point(const double x, const double y) : x(x), y(y) {}
double length(const Point& p)const {
return sqrt(pow(p.x - x, 2) + pow(p.y - y, 2));
}
void set_x(const double x) {
this->x = x;
}
double get_x()const {
return x;
}
void set_y(const double y) {
this->y = y;
}
double get_y()const {
return y;
}
void move_x(const double dx) {
x += dx;
}
void move_y(const double dy) {
y += dy;
}
void move_xy(const double dx, const double dy) {
x += dx;
y += dy;
}
};
Point point(const string& name) {
Point p;
p.x = coord(name + "x: ");
p.y = coord(name + "y: ");
return p;
}
class Rectangle {
public:
Rectangle() = default;
Rectangle(const Point& a, const Point& b) : a(a), b(b) {
rotate();
}
void set_a(const Point& a) {
this->a = a;
}
Point get_a()const {
return a;
}
void set_b(const Point& b) {
this->b = b;
}
Point get_b()const {
return b;
}
double area()const {
return sqrt(pow(a.x - b.x, 2)) * sqrt(pow(a.y - b.y, 2));
}
double perimeter()const {
return (sqrt(pow(a.x - b.x, 2)) + sqrt(pow(a.y - b.y, 2))) * 2.0;
}
double diagonal()const {
return a.length(b);
}
private:
Point a;
Point b;
void rotate() {
if (a.x > b.x) swap(a.x, b.x);
if (a.y < b.y) swap(a.y, b.y);
}
};
int main() {
auto a = point("A");
auto b = point("B");
Rectangle rect{ a, b };
auto area = rect.area();
auto perimeter = rect.perimeter();
auto diagonal = rect.diagonal();
const auto w = 2U;
cout << fixed
<< "Diagonal: " << setprecision(w << 1) << area << '\n'
<< "Perimeter: " << setprecision(w) << area << '\n'
<< "Area: " << setprecision(w << 1) << area << '\n';
system("pause > nul");
}
Евгений Губанов
спасибо огромное вам))
Похожие вопросы
- Здравствуйте, помогите пожалуйста написать программы на языке C++.
- Здравствуйте, помогите пожалуйста написать программу на C++ с использованием функций
- Помогите пожалуйста с C++
- ПОМОГИТЕ ПО ПРОГРАММИРОВАНИЮ!! C++
- Прошу, помогите пожалуйста с C++
- Помогите пожалуйста с c++
- Помогите пожалуйста с C++
- Помогите пожалуйста с C++.
- Здравствуйте! Помогите, пожалуйста, с программой С++
- Программирование C++ ПРОШУ ПОМОЧЬ!