home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / tolkit45.zip / os2tk45 / samples / rexx / complex.cmd < prev    next >
OS/2 REXX Batch file  |  1999-05-11  |  10KB  |  130 lines

  1. /******************************************************************************/
  2. /*  complex                  Object REXX Samples                              */
  3. /*                                                                            */
  4. /*  A complex number class.                                                   */
  5. /*                                                                            */
  6. /*  This program demonstrates how to create a complex number class using the  */
  7. /*  ::class and ::method directives.  Also shown is an example of subclassing */
  8. /*  the complex number class (the vector subclass).  Finally, the stringlike  */
  9. /*  class demonstrates the use of a mixin to provide some string behavior to  */
  10. /*  the complex number class.                                                 */
  11. /******************************************************************************/
  12.  
  13.                                             /* complex number data class      */
  14. ::class complex public inherit stringlike
  15.  
  16. ::method init                               /* initialize a complex number    */
  17. expose real imaginary                       /* expose the state data          */
  18. use arg first, second                       /* access the two numbers         */
  19. real = first + 0                            /* force rounding                 */
  20. if arg(2,'o') then                          /* no second argument?            */
  21.   imaginary = 0                             /* imaginary part is zero         */
  22. else                                                                               
  23.   imaginary = second + 0                    /* force rounding on the second   */
  24.                                                                                    
  25. ::method '[]' class                         /* create a new complex number    */
  26. use arg first, second                       /* access the two numbers         */
  27. if arg(2,'o') then                          /* only one argument?             */
  28.   return self~new(first)                    /* just create as a real number   */
  29. else                                                                               
  30.   return self~new(first, second)            /* create both parts              */
  31.                                                                                    
  32. ::method real                               /* return real part of a complex  */
  33. expose real                                 /* access the state information   */
  34. return real                                 /* return that value              */
  35.                                                                                    
  36. ::method imaginary                          /* return imaginary part          */
  37. expose imaginary                            /* access the state information   */
  38. return imaginary                            /* return the value               */
  39.                                                                                    
  40. ::method '+'                                /* addition method                */
  41. expose real imaginary                       /* access the state values        */
  42. use arg adder                               /* get the operand                */
  43. if arg(1,'o') then                          /* prefix plus operation?         */
  44.   return self                               /* don't do anything with this    */
  45. tempreal = real + adder~real                /* add the real parts             */
  46.                                             /* add the imaginary parts        */
  47. tempimaginary = imaginary + adder~imaginary
  48.                                             /* return a new item of same class*/
  49. return self~class~new(tempreal, tempimaginary)
  50.  
  51. ::method '-'                                /* subtraction method             */
  52. expose real imaginary                       /* access the state values        */
  53. use arg adder                               /* get the operand                */
  54. if arg(1,'o') then                          /* prefix minus operation?        */
  55.                                             /* negate the number              */
  56.   return self~class~new(-real, -imaginary)
  57. tempreal = real - adder~real                /* subtract the real part         */
  58.                                             /* subtract the imaginary part    */
  59. tempimaginary = imaginary - adder~imaginary
  60.                                             /* return a new item              */
  61. return self~class~new(tempreal, tempimaginary)
  62.  
  63. ::method '*'                                /* multiplication method          */
  64. expose real imaginary                       /* access the state values        */
  65. use arg multiplier                          /* get the operand                */
  66.                                             /* calculate the real part        */
  67. tempreal = (real * multiplier~real) - (imaginary * multiplier~imaginary)
  68.                                             /* calculate the imaginary part   */
  69. tempimaginary = (real * multiplier~imaginary) + (imaginary * multiplier~real)
  70.                                             /* return a new item              */
  71. return self~class~new(tempreal, tempimaginary)
  72.  
  73. ::method '/'                                /* division method                */ 
  74. expose real imaginary                       /* access the state values        */ 
  75. use arg divisor                             /* get the operand                */ 
  76. a=real                                      /* get real and imaginaries for   */                             
  77. b=imaginary                                 /* both numbers                   */                             
  78. c=divisor~real                                                           
  79. d=divisor~imaginary                                                      
  80. qr=((b*d)+(a*c))/(c**2+d**2)                /* generate the new result values */                             
  81. qi=((b*c)-(a*d))/(c**2+d**2)                                             
  82. return self~class~new(qr,qi)                /* return the new value           */
  83.  
  84. ::method '%'                                /* integer division method        */ 
  85. expose real imaginary                       /* access the state values        */ 
  86. use arg divisor                             /* get the operand                */ 
  87. a=real                                      /* get real and imaginaries for   */                             
  88. b=imaginary                                 /* both numbers                   */                             
  89. c=divisor~real                                                           
  90. d=divisor~imaginary                                                      
  91. qr=((b*d)+(a*c))%(c**2+d**2)                /* generate the new result values */                             
  92. qi=((b*c)-(a*d))%(c**2+d**2)                                             
  93. return self~class~new(qr,qi)                /* return the new value           */
  94.  
  95. ::method '//'                               /* remainder method               */ 
  96. expose real imaginary                       /* access the state values        */ 
  97. use arg divisor                             /* get the operand                */ 
  98. a=real                                      /* get real and imaginaries for   */                             
  99. b=imaginary                                 /* both numbers                   */                             
  100. c=divisor~real                                                           
  101. d=divisor~imaginary                                                      
  102. qr=((b*d)+(a*c))//(c**2+d**2)               /* generate the new result values */                             
  103. qi=((b*c)-(a*d))//(c**2+d**2)                                             
  104. return self~class~new(qr,qi)                /* return the new value           */
  105.  
  106. ::method string                             /* format as a string value       */
  107. expose real imaginary                       /* get the state info             */
  108. return real'+'imaginary'i'                  /* format as real+imaginaryi      */
  109.  
  110. ::class vector subclass complex public      /* vector subclass of complex     */
  111.  
  112. ::method '[]' class                         /* quick creation of a vector item*/ 
  113. use arg first, second                       /* get the arguments              */ 
  114. return self~new(first, second)              /* create a new vector item       */ 
  115.                                                                                     
  116. ::method string                             /* format as a string value       */ 
  117. return '('self~real','self~imaginary')'     /* format as '(a,b)'              */ 
  118.                                                                                     
  119.                                             /* class for adding generalized   */ 
  120.                                             /* string support to an object    */ 
  121. ::CLASS stringlike PUBLIC MIXINCLASS object
  122.  
  123. ::METHOD unknown UNGUARDED                  /* create an unknown method       */
  124.   use arg msgname, args                     /* get the message and arguments  */
  125.                                             /* forward to the string value    */
  126.   return .message~new(self~string, msgname, 'a', args)~send
  127.  
  128. ::METHOD makestring                         /* add MAKESTRING capability      */
  129.   return self~string                        /* return the string value        */
  130.