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

C++ write a program (дайте пожалуйста код)

5.Write a program that calculates area of a triangle if coordinates of points are given. Below shown an example (values inputted by a user highlighted as bold).

Calculating area of a triangle:
Input the coordinates of angles (leave a space between points):
x1,y1 → -2 5
x2,y2 → 1 7
x3,y3 → 5 -3

Area of the triangle: 19 cm^2
#include <iostream>
#include <string>
using namespace std;
struct coord {
    coord() : x(0), y (0) { }
    coord(double _x, double _y) : x(_x), y (_y) { }
    double x;
    double y;
};
class triangle {
public:
    triangle() : a(coord()), b(coord()), c(coord()) { }
    triangle(coord _a, coord _b, coord _c) : a(_a), b(_b), c(_c) { }
    double get_area()const { return abs((a.x - c.x) * (b.y - c.y) - (b.x - c.x) * (a.y - c.y)) / 2; }
private:
    coord a, b, c;
};
void input_double(double &, string);
void create_triangle(triangle &);
int main() {    
    triangle tr;
    create_triangle(tr);
    cout << " Area = " << tr.get_area() << endl;
    cin.get(); cin.get();
    return 0;
}
void create_triangle(triangle & _tr) {
    string coords_str[] = { "x1", "y1", "x2", "y2", "x3", "y3" };
    coord coords[3];
    for (int n = 0; n < 6; n++) {
        string msg = " Input coord " + coords_str[n] + ": ";
        if (~n & 1) input_double(coords[n >> 1].x, msg);
        else input_double(coords[n >> 1].y, msg);
    }
    triangle temp(coords[0], coords[1], coords[2]);
    _tr = temp;
}
void input_double(double & _dbl_t, string _msg) {
    do {        
        cout << _msg;
        cin >> _dbl_t;
        if (cin.good()) break;
        else {
            cin.clear();
            cin.ignore(80,'\n');
            cout << "\a Error!\n";
        }
    } while (true);
}
Жомарт Шауенов
Жомарт Шауенов
98 033
Лучший ответ
#include <iostream>
using namespace std;
int main()
{
double x1,x2,x3,y1,y2,y3=0;
double S=0;

cout<<"Enter x1"<<endl;>>x1;
cout<<"Enter y1"<<endl;>>y1;
cout<<"Enter x2"<<endl;>>x2;
cout<<"Enter y2"<<endl;>>y2;
cout<<"Enter x3"<<endl;>>x3;
cout<<"Enter y3"<<endl;>>y3;
S=(0.5)*abs(((x1-x3)*(y2-y3))-((x2-x3)*(y1-y3)));
cout<<"Area of the triangle "<