home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / ckscripts / complex < prev    next >
Text File  |  2020-01-01  |  4KB  |  140 lines

  1. ; From: Dat Thuc Nguyen
  2. ; Date: 27 May 1999
  3. ; Subject: Incoming script: Complex Number in C-Kermit
  4. ; URL: http://www.smalltickle.com
  5. ;
  6. ; COMPLEX NUMBER IS USUALLY NOT A BUILT-IN TYPE OF MANY PROGRAMMING
  7. ; LANGUAGES, HERE OOP COMES TO RESCUE WITH THE USER-DEFINED TYPE.
  8. ; THE FOLLOWING SCRIPT DEFINES A COMPLEX NUMBER CLASS IN C-KERMIT.
  9. ; THE COMPLEX NUMBER CLASS OFFERS THE FAMILIAR USAGE INTERFACE THAT
  10. ; C++ IMPLEMENTATION ALSO PROVIDES:
  11. ;
  12. ; C-Kermit 7.0 Beta.07 required
  13. ;
  14. ; complex instance_name re_val im_val
  15. ;    Create a new instance of complex
  16. ;         with re_val as the real value
  17. ;         and  im_val as the imagine value.
  18. ;
  19. ; instance_name show
  20. ;    Display the re_val and im_value in usual math notation, ex: 3 + 7i
  21. ;
  22. ; instance_name add other_instance_name
  23. ;    Add the complex number other_instance_name
  24. ;    to  the complex number instance_name.
  25. ;
  26. ; instance_name sub other_instance_name
  27. ;    Substract the complex number other_instance_name
  28. ;    from      the complex number instance_name.
  29. ;
  30. ; instance_name mult other_instance_name
  31. ;    Mulitply the complex number other_instance_name
  32. ;    with     the complex number instance_name
  33. ;    and      store the result into the complex number instance_name.
  34. ;
  35. ; instance_name copy other_instance_name
  36. ;    Copy the complex number other_instance_name
  37. ;    into the complex number instance_name
  38. ;
  39. ; instance_name real
  40. ;    Return the real value of the complex number instance_name.
  41. ;
  42. ; instance_name imagine
  43. ;    Return the imagine value of the complex number instance_name.
  44. ;
  45. ; instance_name destroy
  46. ;    Erase the complex number instance_name and its components from
  47. ;    the memory.
  48.  
  49. set eval new ; Use new EVALUATE command format (C-Kermit 7.0 Beta.07)
  50.  
  51. define complex {
  52. ; \%1 instance_name
  53. ; \%2 real value
  54. ; \%3 imagine value
  55.      if not define \%1 END 1 ... missing instance_name
  56.      if not define \%2 assign \%2 0     ; default real value
  57.      if not define \%3 assign \%3 0     ; default imagine value
  58.      _assign complex::\%1.re \%2
  59.      _assign complex::\%1.im \%3
  60.  
  61.      _define \%1 {
  62.           if define \m(complex::\%1) {
  63.                complex::\%1 \v(macro) \%2 \%3 \%4
  64.           } else {
  65.                END -9999 doesNotUnderstand
  66.           }
  67.      }
  68. }
  69.  
  70. define complex::show {
  71.      echo \m(complex::\%1.re) + \m(complex::\%1.im)i
  72. }
  73.  
  74. define complex::add {
  75.      _eval complex::\%1.re \m(complex::\%1.re) + \m(complex::\%2.re)
  76.      _eval complex::\%1.im \m(complex::\%1.im) + \m(complex::\%2.im)
  77. }
  78.  
  79. define complex::sub {
  80.      _eval complex::\%1.re \m(complex::\%1.re) - \m(complex::\%2.re)
  81.      _eval complex::\%1.im \m(complex::\%1.im) - \m(complex::\%2.im)
  82. }
  83.  
  84. define complex::mult {
  85.      local \%r \%i
  86.      .\%r ::= \m(complex::\%1.re) * \m(complex::\%2.re)-
  87.             - \m(complex::\%1.im) * \m(complex::\%2.im)
  88.      .\%i ::= \m(complex::\%1.re) * \m(complex::\%2.im)-
  89.             + \m(complex::\%1.im) * \m(complex::\%2.re)
  90.      _assign complex::\%1.re \%r
  91.      _assign complex::\%1.im \%i
  92. }
  93.  
  94. define complex::real {
  95.      if define \%2 _assign complex::\%1.re \%2
  96.      return \m(complex::\%1.re)
  97. }
  98.  
  99. define complex::imagine {
  100.      if define \%2 _assign complex::\%1.im \%2
  101.      return \m(complex::\%1.im)
  102. }
  103.  
  104. define complex::copy {
  105.      _assign complex::\%1.re \m(complex::\%2.re)
  106.      _assign complex::\%1.im \m(complex::\%2.im)
  107. }
  108.  
  109. define complex::destroy {
  110.      _define complex::\%1.re
  111.      _define complex::\%1.im
  112.      _define \%1
  113. }
  114.  
  115. ;
  116. ; USAGE SAMPLES
  117. ;
  118. complex c1 1 3                ; create the complex number c1: 1 + 3i
  119. complex c2 2 5                ; create the complex number c2: 2 + 5i
  120. c1 show                       ; => 1 + 3i
  121. c2 show                       ; => 2 + 5i
  122. c1 add c2
  123. c1 show                       ; => 3 + 8i
  124. c1 sub c2                     ;
  125. c1 show                       ; => 1 + 3i
  126. complex c3                    ; create the complex number c3: 0 + 0i
  127. c3 copy c2
  128. c3 show                       ; => 2 + 5i
  129. complex c4 1 3                ; create the complex number c4: 1 + 3i
  130. complex c5 2 4                ; create the complex number c5: 2 + 4i
  131. c4 mult c5
  132. c4 show                       ; => -10 + 11i
  133. c1 destroy
  134. c2 destroy
  135. c3 destroy
  136. c4 destroy
  137. c5 destroy
  138.  
  139. exit
  140.