用类 定位三角形求三角形三边及面积

来源:互联网 发布:伊藤忠 知乎 编辑:程序博客网 时间:2024/03/29 00:57
//新人第一次发哈哈//
//版本号 1.0    //
//作者暴走竹竿   //
#include<iostream>#include<cmath>using namespace std;class point {public:point(int xx = 0, int yy = 0) {x = xx;y = yy;}int getx() { return x; }int gety() { return y; }private:int x, y;};class Line {public:Line(point xp1, point xp2, point xp3);Line(Line &l);double getlen() { return len; }private:point p1, p2, p3;double len;};Line::Line(point xp1, point xp2, point xp3) :p1(xp1), p2(xp2), p3(xp3){double x = static_cast<double>(p1.getx() - p2.getx());double y = static_cast<double>(p1.gety() - p2.gety());len = sqrt(x*x + y*y);}int main() {double a = 0;double area = 0;point myp1(1, 1), myp2(4, 5), myp3(1, 5);Line line(myp3, myp2, myp1);Line line2(myp1, myp2, myp3);Line line3(myp1, myp3, myp2);cout << line.getlen() << endl;cout << line2.getlen() << endl;cout << line3.getlen() << endl;a = (line.getlen() + line2.getlen() + line3.getlen()) / 2;area= sqrt(a*(a - line.getlen())*(a-line2.getlen())*(a - line3.getlen()));cout << "面积为" << area << endl;return 0;}

0 0
原创粉丝点击