; From: Dat Thuc Nguyen
; Date: 27 May 1999
; Subject: Incoming script: Complex Number in C-Kermit
; URL: http://www.smalltickle.com
;
; COMPLEX NUMBER IS USUALLY NOT A BUILT-IN TYPE OF MANY PROGRAMMING
; LANGUAGES, HERE OOP COMES TO RESCUE WITH THE USER-DEFINED TYPE.
; THE FOLLOWING SCRIPT DEFINES A COMPLEX NUMBER CLASS IN C-KERMIT.
; THE COMPLEX NUMBER CLASS OFFERS THE FAMILIAR USAGE INTERFACE THAT
; C++ IMPLEMENTATION ALSO PROVIDES:
;
; C-Kermit 7.0 Beta.07 required
;
; complex instance_name re_val im_val
;    Create a new instance of complex
;         with re_val as the real value
;         and  im_val as the imagine value.
;
; instance_name show
;    Display the re_val and im_value in usual math notation, ex: 3 + 7i
;
; instance_name add other_instance_name
;    Add the complex number other_instance_name
;    to  the complex number instance_name.
;
; instance_name sub other_instance_name
;    Substract the complex number other_instance_name
;    from      the complex number instance_name.
;
; instance_name mult other_instance_name
;    Mulitply the complex number other_instance_name
;    with     the complex number instance_name
;    and      store the result into the complex number instance_name.
;
; instance_name copy other_instance_name
;    Copy the complex number other_instance_name
;    into the complex number instance_name
;
; instance_name real
;    Return the real value of the complex number instance_name.
;
; instance_name imagine
;    Return the imagine value of the complex number instance_name.
;
; instance_name destroy
;    Erase the complex number instance_name and its components from
;    the memory.

set eval new ; Use new EVALUATE command format (C-Kermit 7.0 Beta.07)

define complex {
; \%1 instance_name
; \%2 real value
; \%3 imagine value
     if not define \%1 END 1 ... missing instance_name
     if not define \%2 assign \%2 0     ; default real value
     if not define \%3 assign \%3 0     ; default imagine value
     _assign complex::\%1.re \%2
     _assign complex::\%1.im \%3

     _define \%1 {
          if define \m(complex::\%1) {
               complex::\%1 \v(macro) \%2 \%3 \%4
          } else {
               END -9999 doesNotUnderstand
          }
     }
}

define complex::show {
     echo \m(complex::\%1.re) + \m(complex::\%1.im)i
}

define complex::add {
     _eval complex::\%1.re \m(complex::\%1.re) + \m(complex::\%2.re)
     _eval complex::\%1.im \m(complex::\%1.im) + \m(complex::\%2.im)
}

define complex::sub {
     _eval complex::\%1.re \m(complex::\%1.re) - \m(complex::\%2.re)
     _eval complex::\%1.im \m(complex::\%1.im) - \m(complex::\%2.im)
}

define complex::mult {
     local \%r \%i
     .\%r ::= \m(complex::\%1.re) * \m(complex::\%2.re)-
            - \m(complex::\%1.im) * \m(complex::\%2.im)
     .\%i ::= \m(complex::\%1.re) * \m(complex::\%2.im)-
            + \m(complex::\%1.im) * \m(complex::\%2.re)
     _assign complex::\%1.re \%r
     _assign complex::\%1.im \%i
}

define complex::real {
     if define \%2 _assign complex::\%1.re \%2
     return \m(complex::\%1.re)
}

define complex::imagine {
     if define \%2 _assign complex::\%1.im \%2
     return \m(complex::\%1.im)
}

define complex::copy {
     _assign complex::\%1.re \m(complex::\%2.re)
     _assign complex::\%1.im \m(complex::\%2.im)
}

define complex::destroy {
     _define complex::\%1.re
     _define complex::\%1.im
     _define \%1
}

;
; USAGE SAMPLES
;
complex c1 1 3                ; create the complex number c1: 1 + 3i
complex c2 2 5                ; create the complex number c2: 2 + 5i
c1 show                       ; => 1 + 3i
c2 show                       ; => 2 + 5i
c1 add c2
c1 show                       ; => 3 + 8i
c1 sub c2                     ;
c1 show                       ; => 1 + 3i
complex c3                    ; create the complex number c3: 0 + 0i
c3 copy c2
c3 show                       ; => 2 + 5i
complex c4 1 3                ; create the complex number c4: 1 + 3i
complex c5 2 4                ; create the complex number c5: 2 + 4i
c4 mult c5
c4 show                       ; => -10 + 11i
c1 destroy
c2 destroy
c3 destroy
c4 destroy
c5 destroy

exit
