03.#include<iostream>
04.#include<Cmath>
05.using namespace std;
06.
07.enum SymmetricStyle {axisx, axisy, point}; //分别表示按x轴, y轴, 原点对称
08.class CPoint
09.{
10.private:
11. double x; // 横坐标
12. double y; // 纵坐标
13.public:
14. CPoint(double xx=0, double yy=0);
15. double Distance(CPoint p) const; // 两点之间的距离(一点是当前点,另一点为参数p)
16. double Distance0() const; // 到原点的距离
17. CPoint SymmetricAxis(SymmetricStyle style) const; // 返回对称点
18. void input(); //以x,y 形式输入坐标点
19. void output(); //以(x,y) 形式输出坐标点
20.};
21.
22.CPoint::CPoint(double xx, double yy):x(xx),y(yy){}
23.
24.int main()
25.{
26. CPoint p1, p2;
27. p1.input();
28. p2.input();
29. p1.output();
30. p2.output();
31. cout << "两点间的距离是:" << p1.Distance(p2) << endl;
32. cout << "点到原点的距离是:" << p2.Distance0() << endl;
33. p1.SymmetricAxis(axisx);
34. p1.SymmetricAxis(axisy);
35. p1.SymmetricAxis(point);
36.
37. system("pause");
38. return 0;
39.}
40.
41.CPoint CPoint::SymmetricAxis(SymmetricStyle style) const
42.{
43. switch(style)
44. {
45. case(axisx):
46. {
47. cout << "(" << x << "," << y << ")"<< "关于x轴对称点是: (" << x << "," << - y << ")" << endl;
48. break;
49. }
50. case(axisy):
51. {
52. cout << "(" << x << "," << y << ")" << "关于y轴对称点是: (" << - x << "," << y << ")" << endl;
53. break;
54. }
55. default:
56. {
57. cout << "(" << x << "," << y << ")"<< "关于原点对称点是: (" << - x << "," << - y << ")" << endl;
58. break;
59. }
60. }
61. return (x, y);
62.}
63.
64.double CPoint::Distance(CPoint p) const // 计算两点之间的距离(一点是当前点,另一点为参数p)
65.{
66. double dis;
67. dis = sqrt((p.x - x) * (p.x - x) + (p.y - y) * (p.y - y));
68. return dis;
69.}
70.
71.double CPoint::Distance0() const //计算当前点到原点的距离
72.{
73. double dis;
74. dis = sqrt(x * x + y * y);
75. return dis;
76.}
77.
78.
79.void CPoint::input() //以x,y 形式输入坐标点
80.{
81. char comma;
82. cout << "请输入点坐标,格式: x,y" << endl;
83.
84. while(1)
85. {
86. cin >> x >> comma >> y ;
87. if(comma != ',')
88. {
89. cout << "格式不正确,请重新输入:" << endl;
90. }
91. else
92. {
93. break;
94. }
95. }
96.}
97.
98.void CPoint::output()
99.{
100. cout << "点(" << x << "," << y << ")" << endl;
101.}