您的位置:首页 > 新闻 > 资讯 > 图片链接生成器软件_武汉设计工程学院学费_网站建设制作流程_广东新闻今日大件事

图片链接生成器软件_武汉设计工程学院学费_网站建设制作流程_广东新闻今日大件事

2025/1/3 3:55:03 来源:https://blog.csdn.net/m0_74841364/article/details/144805188  浏览:    关键词:图片链接生成器软件_武汉设计工程学院学费_网站建设制作流程_广东新闻今日大件事
图片链接生成器软件_武汉设计工程学院学费_网站建设制作流程_广东新闻今日大件事
//点和三角形
//浮点数,列表初始化,运算符的重载#include<ostream>
#include<array>
class Point {friend class Triangle;//声明友元类❗//输出运算符的重载friend ostream& operator<<(ostream& o, const Point& p)//❗{o << "[" << p. x << "," << p.y <<"]";return o;//考❗}public:Point(int xx,int yy):x(xx),y(yy){} //构造函数列表初始化❗~Point();//析构函数//计算两点距离float GetDistance(const Point& other) {return sqrt((x - other.x) * (x - other.x) + (y - other.y) * ((y - other.y)));}//+重载 两点相加没有& ❗Point operator+ (const Point& other) {return Point(x + other.x, y + other.y);}private:int x, y;//float};class Triangle {//输出运算符的重载friend ostream& operator>>(ostream& o, const Triangle& t){o << "[" << t.all[0] << "," << t.all[1] << ","<<t.all[2]<<"}";return o;//考❗}public://三个点-用数组元素 去列表初始化❗Triangle(const Point& p1, const Point& p2, const Point& p3) :all{p1, p2, p3}{}  //构造函数-列表初始化//三个边-根据三个点初始化❗Triangle(float a, float b, float c) :all{ Point{ 0,0 }, Point{ a,0 }, Point{ 0,0 } } {all[2].x = ........;all[2].y = ........;}//计算面积float GetArea() {//变长float a = all[0].GetDistance(all[1]);❗float b = all[1].GetDistance(all[2]);float c = all[2].GetDistance(all[0]);float p = (a + b + c) / 2.0f;return sqrt(....);}//Triangle t;// t[0]=Point{1,2};//下标运算符重载❗Point& operator[](int index) { return all[index]; }private://表示定义了一个名为 all 的数组,该数组存储了3个 Point 类型的元素。array<Point, 3> all; //考<>❗
};//下标运算符重载
//Point& Triangle::operator[](int index) { return all[index]; }
//点和三角形
#include <iostream>
using namespace std;
class  Point {//cout << a << b;friend ostream& operator<<(ostream& o, const Point& p) {o << "[" << p.x << "," << p.y << "]";return o;}
public:Point(int xx = 0, int yy = 0) : x(xx), y(yy) {}~Point();void print() {cout << "[" << x << "," << y << "]";}float GetDistance(const Point& other) {return sqrt((x - other.x) * (x - other.x) + (y - other.y) * ((y - other.y)));}Point operator+ (const Point& other) {return Point(x + other.x, y + other.y);}
private:float x, y;
};
class Triangle {friend ostream& operator <<(ostream& o, const Triangle& t) {o << "[" << t.p1 << "," << t.p2 << "," << t.p3 << "]";return o;}
public:Triangle(const Point& pp1, const Point& pp2, const Point& pp3) :p1(pp1), p2(pp2), p3(pp3) {}void print() {cout << "{";p1.print(); cout << ","; p2.print(); cout << ","; p3.print();cout << "}";}Point& operator[](int index) {switch (index) {case 0:return p1;case 1:return p2;case 2:return p3;}}float GetArea() {return 0.5 * abs(p1.x * (p2.y - p3.y) + p2.x * (p3.y - p1.y) + p3.x * (p1.y - p2.y));}
private:Point p1, p2, p3;    //成员对象
};
int main()
{Point a(1, 2), b(3, 4), c;c = a + b;cout << a << "+" << b << "=" << c << endl;Triangle t(a, b, c);t[0] = Point(0, 0);cout << "三角形:" << t << endl;
}
test.cpp//三角形和点
//使用float 和 int必须一致#include<iostream>
using namespace std;
#include"Triangle.h"
int main()
{/*Point p(3, 4);Point q(1, 2);p.Show();cout << p.Distance(q) << endl;*///点Triangle t1(Point(1, 1), Point(1, 3), Point(3, 3));t1.Show();cout << "Area=" << t1.Area() << endl << endl;//边Triangle t2(2,sqrt(2),sqrt(2));t2.Show();cout << "Area=" << t2.Area() << endl << endl;//边Triangle t3(10,1,1);t3.Show();cout << "Area=" << t3.Area() << endl << endl;}
//Triangle.h#pragma onceclass Point
{friend class Triangle;//友元类
public://构造函数Point(float xx = 0, float yy = 0);void SetX(float xx);void SetY(float yy);void Show();//两点间的距离float Distance(const Point& other);
private:float x;//intfloat y;//int
};class Triangle
{public://构造函数Triangle(const Point& pp1,const Point& pp2, const Point& pp3);//构造函数,根据边长设计三个顶点Triangle(float a, float b, float c);//计算面积的函数float Area();void Show(); //委托三个顶点的Show()完成  
private:Point p1;Point p2;Point p3;
};
//Triangle.cpp
#include<iostream>
#include "Triangle.h"
#include"cmath"
using namespace std;
//构造函数,列表初始化
Point::Point(float xx, float yy):x(xx),y(yy)
{
}//SetX
void Point::SetX(float xx)
{//一般要做有效性的检查,这里坐标可以不用x = xx;
}//SetY
void Point::SetY(float yy)
{y = yy;
}//Show
void Point::Show()
{cout << "[" << x << "," << y << "]" << endl;
}//两点之间的距离:(x1-x2)²+(y1-y2)²整体开根号
float Point::Distance(const Point& other)
{return sqrt((x - other.x) * (x - other.x) + (y - other.y) * (y - other.y));
}//构造函数,列表初始化
Triangle::Triangle(const Point& pp1, const Point& pp2, const Point& pp3):p1(pp1),p2(pp2),p3(pp3)
{
}//假设p1(0,0) p2(a,0) p3(x,y)
//假设边长是a,b,c
//
Triangle::Triangle(float a, float b, float c):p1(0,0),p2(a,0)  //调用的是成员对象的函数
{if (a + b > c && b + c > a && c + a > b){float x = (a * a + b * b - c * c) / a / 2;float y = sqrt(c * c - x * x);p3.x = x; // p3.SetX(x);p3.y = y; // p3.SetY(y);}else {p2.x = p2.y = p3.x = p3.y = 0;//p2.SetX(0);//p2.SetY(0);//p3.SetX(0);//p3.SetY(0);}
}//计算面积
//根据p1,p2,p3计算三角形的面积
float Triangle::Area()
{float a= p1.Distance(p2);float b = p2.Distance(p3);float c = p3.Distance(p1);float p = (a + b + c) / 2;return sqrt(p * (p - a) * (p - b) * (p - c));
}void Triangle::Show()
{cout << "{"; p1.Show();cout << ",";p2.Show();cout << ",";p3.Show();cout << "}";
}

 

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com