『代码』··By/蜜汁炒酸奶

operator重载之三维坐标计算—C++

/*
功能:三维坐标计算
作者:wind
日期:2013-11-29
*/
#include <iostream>
#include <string>
using namespace std;
/************************************************************/
class Point
{
public:
	Point()
	{
		m_iX = 0;
		m_iY = 0;
		m_iZ = 0;
	}
	void set(double aX,double aY,double aZ);
	void get()const;
	Point operator + (Point c1);
	Point operator - (Point c1);
	void display(Point c1);
private:
	double m_iX;
	double m_iY;
	double m_iZ;
};
/************************************************************/
void Point::set(double aX,double aY,double aZ)
{
	m_iX = aX;
	m_iY = aY;
	m_iZ = aZ;

}
void Point::get()const
{
	cout<<"The result is:"<<endl;
	cout<<"("<<m_iX<<","<<m_iY<<","<<m_iZ<<")"<<endl;
}
Point Point::operator + (Point c1)
{
	Point c;
	c.m_iX = m_iX + c1.m_iX;
	c.m_iY = m_iY + c1.m_iY;
	c.m_iZ = m_iZ + c1.m_iZ;
	return c;
}
Point Point::operator - (Point c1)
{
	Point c;
	c.m_iX = m_iX - c1.m_iX;
	c.m_iY = m_iY - c1.m_iY;
	c.m_iZ = m_iZ - c1.m_iZ;
	return c;
}

void Point::display(Point c1)
{
	string n;
	Point c;
	cout<<"Please input the sign(-/+):";
	cin>>n;
	if  ("-" == n)
	{
		c = operator- (c1);
		cout<<"The result is:"<<endl;
	    cout<<"("<<c.m_iX<<","<<c.m_iY<<","<<c.m_iZ<<")"<<endl;
	}
	else if ("+" == n)
	{
		c = operator+ (c1);
		cout<<"The result is:"<<endl;
		cout<<"("<<c.m_iX<<","<<c.m_iY<<","<<c.m_iZ<<")"<<endl;
	}
	else
	{
		cout<<"The operator is error";
	}
}
/************************************************************/
int main(void)
{

	Point c1,c2;
	double x = 0;
	double y = 0;
	double z = 0;
    cout<<"请输入第一个点所在的三维坐标:";
	cin>>x>>y>>z;
	c1.set(x,y,z);

	cout<<"请输入第二个点所在的三维坐标:";
	cin>>x>>y>>z;
	c2.set(x,y,z);

	c1.display(c2);
	system("pause");
	return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101

预览
Loading comments...
0 条评论

暂无数据

example
预览