|
发表于 2021-3-15 08:42:09
|
显示全部楼层
本帖最后由 xieglt 于 2021-3-15 09:17 编辑
- #ifndef H_COMPLEX
- #define H_COMPLEX
- #include <math.h>
- //+ , - , * , += , -= , *= 都有
- typedef struct tagComplex
- {
- double X;
- double Y;
- tagComplex()
- {
- X = 0;
- Y = 0;
- }
- tagComplex(const struct tagComplex & c)
- {
- X = c.X;
- Y = c.Y;
- }
- tagComplex(double x,double y = 0.0)
- {
- X = x;
- Y = y;
- }
-
- struct tagComplex & operator = (double x)
- {
- X = x;
- Y = 0;
- return * this;
- }
-
- struct tagComplex & operator = (const tagComplex & c)
- {
- X = c.X;
- Y = c.Y;
- return * this;
- }
-
- struct tagComplex & operator += (const tagComplex & c)
- {
- X += c.X;
- Y += c.Y;
- return * this;
- }
-
- struct tagComplex & operator -= (const tagComplex & c)
- {
- X -= c.X;
- Y -= c.Y;
- return * this;
- }
-
- struct tagComplex & operator *= (const tagComplex & c)
- {
- COMPLEX t = * this;
- X = t.X * c.X - t.Y * c.Y;
- Y = t.X * c.Y + t.Y * c.X;
- return * this;
- }
-
- struct tagComplex & operator /= (double n)
- {
- X /= n;
- Y /= n;
- return * this;
- }
-
- void Mul(const tagComplex & a,const tagComplex & b)
- {
- * this = a;
- * this *= b;
- }
-
- operator double()
- {
- return sqrt(X*X + Y*Y);
- }
- operator long()
- {
- return (long)(X + 0.4);
- }
- friend struct tagComplex operator + (const struct tagComplex & a, const struct tagComplex & b)
- {
- struct tagComplex c = a;
- c += b;
- return c;
- }
- friend struct tagComplex operator - (const struct tagComplex & a, const struct tagComplex & b)
- {
- struct tagComplex c = a;
- c -= b;
- return c;
- }
- friend struct tagComplex operator * (const struct tagComplex & a, const struct tagComplex & b)
- {
- struct tagComplex c = a;
- c *= b;
- return c;
- }
- }COMPLEX, *LPCOMPLEX;
- #endif
复制代码 |
|