#include "msobject.ch"
class ComplexNumber
public data a
public data b
public method new()
public method create(parm1, parm2)
public OPERATOR Add (parm1)
public OPERATOR Sub(parm1)
public OPERATOR Mult(parm1)
public OPERATOR Div(parm1)
public OPERATOR Compare(parm1)
public OPERATOR ToString()
endclass
method new() class ComplexNumber
return self
method create(parm1, parm2) class ComplexNumber
self:a := parm1
self:b := parm2
return self
//esse será o método invocado ao executar a soma de um objeto do tipo ComplexNumber com qualquer outro tipo
operator Add(parm1) class ComplexNumber
local ret := ComplexNumber():New()
ret:a := self:a + parm1:a
ret:b := self:b + parm1:b
return ret
operator Sub(parm1) class ComplexNumber
local ret := ComplexNumber():New()
ret:a := self:a - parm1:a
ret:b := self:b - parm1:b
return ret
operator Mult(parm1) class ComplexNumber
local ret := ComplexNumber():New()
ret:a := self:a * parm1:a - (self:b - parm1:b)
ret:b := (self:a * parm1:b) + (self:b * parm1:a)
return ret
operator Div(parm1) class ComplexNumber
local conj := ComplexNumber():New()
local ret := ComplexNumber():New()
// Conjugado do denominador
conj:a := parm1:a
conj:b := - parm1:b
// Multipica numerador e denominador pelo conjugado
ret := self * conj
conj := parm1 * conj
// Divide o resultado da multiplicacao do numerador pela parte real do denominador
ret:a := ret:a / conj:a
ret:b := ret:b / conj:a
return ret
operator Compare(parm1) class ComplexNumber
//retorna -1 se for menor, 0 se for igual e 1 se for maior
if (self:a > parm1:a)
return 1
endif
if (self:a < parm1:a)
return -1
endif
if (self:b > parm1:b)
return 1
endif
if (self:b < parm1:b)
return -1
endif
return 0
operator ToString() class ComplexNumber
return cValtoChar(self:a) + " + " + cValToChar(self:b) + "i"
user function exemploComplex()
local obj := ComplexNumber():Create(1, 2)
local obj2 := ComplexNumber():Create(3, 4)
local objRet
objRet := obj + obj2
conout(objRet)
objRet := obj - obj2
conout(objRet)
objRet := obj * obj2
conout(objRet)
objRet := obj / obj2
conout(objRet)
if (obj < obj2)
conout("é menor")
endif
return
|