首页 > 编程语言 >C++面对对象:实现complex类

C++面对对象:实现complex类

时间:2023-02-21 11:24:26浏览次数:39  
标签:real return imag C++ complex inline const 面对

  1 #ifndef __MYCOMPLEX_H__
  2 #define __MYCOMPLEX_H__
  3 
  4 class complex;
  5 complex& __doapl(complex*, const complex&); //友元可以在类外声明
  6 complex& __doami(complex*, const complex&);
  7 complex& __doaml(complex*, const complex&);
  8 
  9 
 10 class complex{
 11 public:
 12     complex(double r = 0, double i = 0) :re(r), im(i) {}
 13     complex(const complex& x) { re = x.real(); im = x.imag(); }
 14 
 15     complex& operator += (const complex&);
 16     complex& operator -= (const complex&);
 17     complex& operator *= (const complex&);
 18 
 19     double real() const { return re; }
 20     double imag() const { return im; }
 21 
 22 private:
 23     double re, im;
 24 
 25     friend complex& __doapl(complex*, const complex&);//TODO plus
 26     friend complex& __doami(complex*, const complex&);//TODO minus
 27     friend complex& __doaml(complex*, const complex&);//TODO multiply
 28 };
 29 
 30 //友元函数不是类的成员函数, 没有class
 31 inline complex& __doapl(complex* ths, const complex& r){
 32     ths->re += r.re;
 33     ths->im += r.im;
 34     return *ths;
 35 }
 36 
 37 inline complex& __doami(complex* ths, const complex& r){
 38     ths->re -= r.re;
 39     ths->im -= r.im;
 40     return *ths;
 41 }
 42 
 43 //(a+bi)(c+di) = (ac-bd)+(ad+bc)i
 44 inline complex& __doaml(complex* ths, const complex& r){
 45     double f = ths->re * r.re - ths->im * r.im;
 46     ths->im = ths->re * r.im + ths->im * r.re;
 47     ths->re = f;
 48     return *ths;
 49 }
 50 
 51 inline complex& complex::operator *= (const complex& r){
 52     return __doaml(this, r);
 53 }
 54 
 55 inline complex& complex::operator += (const complex& r){
 56     return __doapl(this, r);
 57 }
 58 
 59 inline complex& complex::operator -= (const complex& r){
 60     return __doami(this, r);
 61 }
 62 
 63 /* ------------------ */
 64 
 65 inline double imag(const complex& x){
 66     return x.imag();
 67 }
 68 
 69 inline double real(const complex& x){
 70     return x.real();
 71 }
 72 
 73 inline complex operator + (const complex& x, const complex& y){ //区分+=, 不用写在public里, 要考虑多种情况
 74     return complex(real(x) + real(y), imag(x) + imag(y));
 75 }
 76 
 77 inline complex operator + (const complex& x, double y){
 78     return complex(real(x) + y, imag(x));
 79 }
 80 
 81 inline complex operator + (double x, const complex& y){
 82     return complex(x + real(y), imag(y));
 83 }
 84 
 85 inline complex operator - (const complex& x, const complex& y){
 86     return complex(real(x) - real(y), imag(x) - imag(y));
 87 }
 88 
 89 inline complex operator - (const complex& x, double y){
 90     return complex(real(x) - y, imag(x));
 91 }
 92 
 93 inline complex operator - (double x, const complex& y){
 94     return complex(x - real(y), -imag(y));
 95 }
 96 
 97 //(a+bi)(c+di) = (ac-bd)+(ad+bc)i
 98 inline complex operator * (const complex& x, const complex& y){
 99     return complex(real(x) * real(y) - imag(x) * imag(y),
100         real(x) * imag(y) + imag(x) * real(y));
101 }
102 
103 inline complex operator * (const complex& x, double y){
104     return complex(real(x) * y, imag(x) * y);
105 }
106 
107 inline complex operator * (double x, const complex& y){
108     return complex(x * real(y), x * imag(y));
109 }
110 
111 inline complex operator / (const complex& x, double y){
112     return complex(real(x) / y, imag(x) / y);
113 }
114 
115 //取正
116 inline complex operator + (const complex& x){
117     return x;
118 }
119 
120 inline complex operator - (const complex& x){
121     return complex(-real(x), -imag(x));
122 }
123 
124 inline bool operator == (const complex& x, const complex& y){
125     return real(x) == real(y) && imag(x) == imag(y);
126 }
127 
128 inline bool operator == (const complex& x, double y){
129     return real(x) == y && imag(x) == 0;
130 }
131 
132 inline bool operator == (double x, const complex& y){
133     return x == real(y) && imag(y) == 0;
134 }
135 
136 inline bool operator != (const complex& x, const complex& y){
137     return real(x) != real(y) || imag(x) != imag(y);
138 }
139 
140 inline bool operator != (const complex& x, double y){
141     return real(x) != y || imag(x) != 0;
142 }
143 
144 inline bool operator != (double x, const complex& y){
145     return x != real(y) || imag(y) != 0;
146 }
147 
148 /* ------------------ */
149 
150 #include <cmath>
151 
152 //复数的极坐标定义
153 inline complex polar(double r, double t){
154     return complex(r * cos(t), r * sin(t));
155 }
156 
157 //共轭复数
158 inline complex conj(const complex& x){
159     return complex(real(x), -imag(x));
160 }
161 
162 //复数的向量的模, 模值平方
163 inline double norm(const complex& x){
164     return real(x) * real(x) + imag(x) * imag(x);
165 }
166 
167 #endif // !__MYCOMPLEX_H__
 1 #include<iostream>
 2 #include"complex.h"
 3 
 4 using namespace std;
 5 
 6 ostream& operator << (ostream& os, const complex& x){
 7     return os << "(" << real(x) << ", " << imag(x) << "i)";
 8 }
 9 
10 int main(){
11     complex c1(3, 2);
12     complex c2(5, 0);
13     cout << c1 << endl;
14     cout << c2 << endl;
15 
16     complex c3;
17     complex c4(2);
18     complex c31(c3);
19     complex c41(c4);
20     cout << c3 << " " << c4 << endl;
21     cout << c31 << " " << c41 << endl;
22 
23     
24     complex c5(7, 3);
25     cout << c1 + c3 << endl;
26 
27     complex c6 = c1 * c5;
28     cout << c5 << " " << c6 << " " << -c6 << endl;
29     
30     cout << c6 - 2 << endl;
31     cout << c6 - c1 << endl;
32     cout << c6 * 9 << endl;
33     cout << c6 / 2 << endl;
34     
35     cout << conj(c6) << endl;
36     cout << norm(c6) << endl;
37     cout << polar(10, 4) << endl;
38 
39     cout << (c1 += c2) << endl;
40 
41     cout << (c1 == c2) << endl;
42     cout << (c1 != c2) << endl;
43     cout << +c2 << endl;
44     cout << -c2 << endl;
45 
46     cout << (c2 - 2) << endl;
47     cout << (5 + c2) << endl;
48 
49     return 0;
50 }

 

标签:real,return,imag,C++,complex,inline,const,面对
From: https://www.cnblogs.com/karinto/p/17140266.html

相关文章

  • C++ json库jsoncpp 吐槽
    Explain   最近在做游戏接入SDK时用到C++的json库jsoncpp,jsoncpp 是一款优秀的json库,但恶心的一点是它采用Assert作为错误处理方法,而assert在linux下通过调用abort......
  • Jni中C++和Java的参数传递
    如何使用JNI的一些基本方法和过程在网上多如牛毛,如果你对Jni不甚了解,不知道Jni是做什么的,如何建立一个基本的jni程序,或许可以参考下面下面这些文章:利用VC++6.0实现JNI的最......
  • C++输出文件名、函数名、行号
    11std::cout<<"filepath=%s"<<__FILE__;//源文件名22std::cout<<"functionname=%s"<<__FUNCTION__;//函数名称33std::c......
  • C++数组
    C++一维数组C++数组的定义方式数据类型数组名[数组长度];例子:intarr[3];arr[0]=1;arr[1]=2;arr[2]=3;数据类型数组名[数组长度]=intarr[3]=......
  • c++学习
      c++字符串转化为整数浮点数。   string和char直接转换============31m代表字体为红色,0m代表关闭所有属性。常用的ANSI控制码如下(有些不支持):\033[0m关闭所......
  • 树状数组板子C++
    1intn;2inta[1005],c[1005];//对应原数组和树状数组34intlowbit(intx){5returnx&(-x);6}78voidupdata(inti,intk){//在i位置加......
  • 【数组与链表算法】矩阵算法在程序中常见的简单应用 | C++
    第二十三章矩阵算法:::hljs-center目录第二十三章矩阵算法●前言●矩阵算法与深度学习●一、矩阵相加●二、矩阵相乘●三、矩阵转置●四、稀疏矩阵●......
  • C++学习-const
    1,定义常量​ A,const与#define的区别:​ a,const常量具有类型,编译器可以进行安全检查,#define没有类型,只是简单替换字符串​ b,const只能定义整数或枚举的常量2,const......
  • 从C到C++(三)
    目录一、引用二、const引用三、引用传递四、引用作为函数返回值五、引用与指针的区别一、引用1、引用是给一个变量起别名,没有自己独立的空间,要与它所引用的变量共享空间......
  • From C++ to Python and a little Java
    原创不意味着能得到“知识产权”。FromC++toPythonandalittleJava从C++到Python以及对Java的小观点OutputPython:printf'\n'C++:std::coutprintformat......