复数与矢量的计算

这是我写一个与无线计算相关的小工具时而编写的代码,目的是为这完成矢量或复数的计算。同时,能完成矢量表达式:《模/相角》与《实数+i实数》这两种形式的自动转换。

 

代码使用delphi 7.0 完成。对于接口部分,是为了导出到其它环境中使用。方式可能不是最好,但目的我找不到更好的方式,权宜用着。

 

  unit my_complex; {$WARN SYMBOL_PLATFORM OFF} interface uses Windows, ActiveX, Classes, ComObj, my_typ_TLB, StdVcl; type TComplexObj=Class(TObject) protected Fre:Double; Fim:Double; Fmo:Double; Fan:Double; procedure SetRe(val:Double); procedure SetIm(val:Double); procedure SetMo(val:Double); procedure SetAn(val:Double); public property re:Double read Fre write SetRe; property im:Double read Fim write SetIm; property mo:Double read Fmo write SetMo; property an:Double read Fan write SetAn; End; type TComplex = class(TTypedComObject, IComplex) protected {Declare IComplex methods here} Function _Add(com1:TComplexObj;com2:TComplexObj):TComplexObj; Function _Sub(com1:TComplexObj;com2:TComplexObj):TComplexObj; Function _Mux(com1:TComplexObj;com2:TComplexObj):TComplexObj; Function _Div(com1:TComplexObj;com2:TComplexObj):TComplexObj; function CreateVector(modulus, angle: Double): OLE_HANDLE; stdcall; function CreateComplex(real, imaginary: Double): OLE_HANDLE; stdcall; function DestoryObject(aHandle: OLE_HANDLE): WordBool; stdcall; function Angle(aHandle: OLE_HANDLE): Double; stdcall; function Real(aHandle: OLE_HANDLE): Double; stdcall; function Modulus(aHandle: OLE_HANDLE): Double; stdcall; function Imaginary(aHandle: OLE_HANDLE): Double; stdcall; function Add(augend, addend: OLE_HANDLE): WordBool; stdcall; function Mux(multiplicand, multiplier_: OLE_HANDLE): WordBool; stdcall; function Sub(minuend, subtrahend: OLE_HANDLE): WordBool; stdcall; function Division(dividend, divisor: OLE_HANDLE): WordBool; stdcall; end; implementation uses ComServ,math; procedure TComplexObj.SetRe(val:Double); Begin Fre:=Val; Fmo:=sqrt(sqr(Fre)+sqr(Fim)); Fan:=RadToDeg(arctan( Fim/Fre )); End; procedure TComplexObj.SetIm(val:Double); Begin FIm:=Val; If (FRe=0) then Exit; Fmo:=sqrt(sqr(Fre)+sqr(Fim)); Fan:=RadToDeg(arctan( Fim/Fre )); End; procedure TComplexObj.SetMo(val:Double); Begin Fmo:=Val; Fre:=Fmo*cos(DegToRad(Fan)); Fim:=Fmo*sin(DegToRad(Fan)); End; procedure TComplexObj.SetAn(val:Double); Begin FAn:=Val; Fre:=Fmo*cos(DegToRad(Fan)); Fim:=Fmo*sin(DegToRad(Fan)); End; function TComplex._Add(com1, com2: TComplexObj): TComplexObj; begin result.re:=com1.re+com2.re; result.im:=com1.im+com2.im; end; function TComplex._Sub(com1, com2: TComplexObj): TComplexObj; begin result.re:=com1.re-com2.re; result.im:=com1.im-com2.im; end; function TComplex._Mux(com1, com2: TComplexObj): TComplexObj; begin result.re:=com1.mo*com2.mo*cos(DegToRad(com1.an+com2.an)); result.im:=com1.mo*com2.mo*sin(DegToRad(com1.an+com2.an)); end; function TComplex._Div(com1,com2:TComplexObj):TComplexObj; var com:TComplexObj; Begin com:=com2; com.im:=0-com.im; result:=self._Mux(com1,com); result.re:=result.re /( sqr(com2.re)+sqr(com2.im)); result.im:=result.im /( sqr(com2.re)+sqr(com2.im)); End; function TComplex.CreateVector(modulus, angle: Double): OLE_HANDLE; var ptr:TComplexObj; begin ptr:=TComplexObj.Create(); try ptr.SetMo(modulus); ptr.SetAn(angle); result:=OLE_HANDLE(ptr); except ptr:=nil; result:=0; end; end; function TComplex.CreateComplex(real, imaginary: Double): OLE_HANDLE; var ptr:TComplexObj; begin try ptr:=TComplexObj.Create(); ptr.SetRe(real); ptr.SetIm(imaginary); result:=OLE_HANDLE(ptr); except ptr:=nil; result:=0; end; end; function TComplex.DestoryObject(aHandle: OLE_HANDLE): WordBool; begin try TComplexObj(aHandle).Free; result:=true; except result:=false; end; end; function TComplex.Angle(aHandle: OLE_HANDLE): Double; begin result:=TComplexObj(aHandle).an; end; function TComplex.Real(aHandle: OLE_HANDLE): Double; begin result:=TComplexObj(aHandle).re; end; function TComplex.Modulus(aHandle: OLE_HANDLE): Double; begin result:=TComplexObj(aHandle).mo; end; function TComplex.Imaginary(aHandle: OLE_HANDLE): Double; begin result:=TComplexObj(aHandle).im; end; function TComplex.Add(augend, addend: OLE_HANDLE): WordBool; var aComplexObj:TComplexObj; begin try aComplexObj:=self._Add(TComplexObj(augend),TComplexObj(addend)); TComplexObj(augend):=aComplexObj; result:=true; except result:=false; end; end; function TComplex.Mux(multiplicand, multiplier_: OLE_HANDLE): WordBool; var aComplexObj:TComplexObj; begin try aComplexObj:=self._Mux(TComplexObj(multiplicand),TComplexObj(multiplier_)); TComplexObj(multiplicand):=aComplexObj; result:=true; except result:=false; end; end; function TComplex.Sub(minuend, subtrahend: OLE_HANDLE): WordBool; var aComplexObj:TComplexObj; begin try aComplexObj:=self._Sub(TComplexObj(minuend),TComplexObj(subtrahend)); TComplexObj(minuend):=aComplexObj; result:=true; except result:=false; end; end; function TComplex.Division(dividend, divisor: OLE_HANDLE): WordBool; var aComplexObj:TComplexObj; begin try aComplexObj:=self._Div(TComplexObj(dividend),TComplexObj(divisor)); TComplexObj(dividend):=aComplexObj; result:=true; except result:=false; end; end; initialization TTypedComObjectFactory.Create(ComServer, TComplex, Class_Complex, ciMultiInstance, tmApartment); end.

你可能感兴趣的:(delphi相关,程序开发)