C++标准复数类的实现

本文展示了如何在C++中实现一个自定义的复数类,包括复数的加减乘除、指数、对数、三角函数等操作,并提供了测试用例进行验证。通过示例输出展示了各种运算的结果。
摘要由CSDN通过智能技术生成

root@ubuntu:/home/cpptest# g++ -w -D USE_STL -o complex complex.cpp
root@ubuntu:/home/cpptest# ./complex
a+b = (5,7)
a-b = (-3,-3)
a*b = (-6,13)
a/b = (0.341463,0.0731707)
log(3,2)= (1.28247,0.588003)
sin(1,2.1)= (3.48732,2.17302)
cos(1,2.1)= (2.23918,-3.38428)
exp(1,2.1)= -1.37231+2.34645i
norm(1,2.1)= 5.41
arg(1,2.1)= 1.12638
conj(1,2.1)= (1,-2.1)
abs(1,2.1)= 2.32594
tan(1,2.1)= 0.0276093+1.01218i
tan(1,2.1)= (0.0276093,1.01218)
atan(0.0276093,1.01218)= (1,2.1)
y = (2,2.1)
i=(0,1)
a=(-1,0)
b=(0,-1)
z=(5,7)
w=sin(z)=(-525.795,155.537)
z=(0.75,0.25)
w=cos(z)=(0.754673,-0.17219)
z=(0.25,0.25)
w=tan(z)=(0.23909,0.259871)
p=conj(i)=(0,-1)
q=real(i)=(0,0)
r=imag(i)=(1,0)
z=(3,2)
w=ln(z)=(1.28247,0.588003)
z=(55.6,68.2)
zabs=abs(z)=87.992
z=(16,81)
r1=4
w=z^r1=(3.30346e+07,-3.26851e+07)
c=sqrt(i)=(0.707107,0.707107)
d=sin(i)=(0,1.1752)
e=i^0.25=(0.92388,0.382683)
z=(1,1)
z1=(1,1)
w=z^z1=(0.273957,0.583701)
root@ubuntu:/home/cpptest# g++ -w -o complex complex.cpp
root@ubuntu:/home/cpptest# ./complex
a+b = (5,7)
a-b = (-3,-3)
a*b = (-6,13)
a/b = (0.341463,0.0731707)
log(3,2)= (1.28247,0.588003)
sin(1,2.1)= (3.48732,2.17302)
cos(1,2.1)= (2.23918,-3.38428)
exp(1,2.1)= -1.37231+2.34645i
norm(1,2.1)= 5.41
arg(1,2.1)= 1.12638
conj(1,2.1)= (1,-2.1)
abs(1,2.1)= 2.32594
tan(1,2.1)= 0.0276093+1.01218i
tan(1,2.1)= (0.0276093,1.01218)
atan(0.0276093,1.01218)= (1,2.1)
y = (2,2.1)
i=(0,1)
a=(-1,0)
b=(0,-1)
z=(5,7)
w=sin(z)=(-525.795,155.537)
z=(0.75,0.25)
w=cos(z)=(0.754673,-0.17219)
z=(0.25,0.25)
w=tan(z)=(0.23909,0.259871)
p=conj(i)=(0,-1)
q=real(i)=(0,0)
r=imag(i)=(1,0)
z=(3,2)
w=ln(z)=(1.28247,0.588003)
z=(55.6,68.2)
zabs=abs(z)=87.992
z=(16,81)
r1=4
w=z^r1=(3.30346e+07,-3.26851e+07)
c=sqrt(i)=(0.707107,0.707107)
d=sin(i)=(0,1.1752)
e=i^0.25=(0.92388,0.382683)
z=(1,1)
z1=(1,1)
w=z^z1=(0.273957,0.583701)

#ifdef USE_STL
#include <complex>
#else
    
#endif
#include<cstdio>
#include<cmath>
#include<iostream>
using namespace std;

#ifndef USE_STL

// 模板类友元函数的前置声明,<>的作用是告诉编译器不是普通函数的声明,是友元函数的声明
template<class T> class complex;
template<class T> ostream& operator<<(ostream& os,const complex<T>& c);
//template<class T> complex<T> operator/(complex<T> &, T);
template<class T> complex<T> div(T r, complex<T> & b);
template<class T> complex<T> cos(complex<T> &);
template<class T> complex<T> sin(complex<T> &);      
template<class T> T real(complex<T> &);   // the real part
template<class T> T imag(complex<T> &);   // the imaginary part
template<class T> complex<T> conj(complex<T> &);  // the complex conjugate
template<class T> T abs(complex<T> &); //求模    
template<class T> complex<T> log(complex<T> &);
template<class T> complex<T> tan(complex<T> &);
template<class T> complex<T> exp(complex<T> &);
template<class T> complex<T> pow(complex<T> & __base, T __expon);
template<class T> complex<T> pow(complex<T> & cpxz,complex<T> & cpxn,int n);
template<class T> complex<T> pow(complex<T> & cpxz,complex<T> & cpxn);
template<class T> T norm(complex<T> &);   // the square of the magnitude
template<class T> T arg(complex<T> &);    // the angle in the plane
template<class T> complex<T

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值