home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / mskscripts / product.kds < prev    next >
Text File  |  2020-01-01  |  5KB  |  102 lines

  1. ; Editor's note: This script was written before MS-DOS Kermit had its own
  2. ; arithmetic capabilities -- it stands as a testament to human ingenuity.
  3. ;------------------------------
  4. ;
  5. ; Arithmetic for MS-DOS-Kermit!
  6. ; Gisbert W. Selke, Aug 1991.
  7. ; Share and enjoy.
  8. ;
  9. ; This collection of macro definitions for MS-DOS Kermit 3.10 (or later)
  10. ; was prompted by Chris Gianone's remark:
  11. ; "Meanwhile, creative Kermit users will no doubt find their own uses for
  12. ; [the PRODUCT macro]". (Using MS-DOS Kermit, 2nd ed., p 182)
  13. ;
  14. ; Having done some math, I think I *know* what a product is...
  15. ; Given that Joe D. has made Kermit with its script language a universal
  16. ; Turing machine...  There you are.
  17. ;
  18. ; TAKE this file from the MS-Kermit> prompt; then, you can do calculations:
  19. ; tadd  <number1> <number2> <...> : show sum of numbers
  20. ;                                   ex: tadd  15 17    yields 32
  21. ;                                   ex: tadd  15 17 19 yields 51
  22. ; tmult <number1> <number2> <...> : show product of numbers
  23. ;                                   ex: tmult 11 13    yields 143
  24. ;                                   ex: tmult 2 3 4 5  yields 120
  25. ; tfact <number>                  : show factorial of number
  26. ;                                   ex: tfact 5        yields 120
  27. ; Macros used internally are explained below.
  28. ;
  29. ; More importantly, when you're in CONNECT mode and your host sends a
  30. ; sequence like "ESC [ 15;7 ~", the product of the two numbers will
  31. ; appear on your screen.
  32. ;
  33. ; Remark: Multiplication can be implemented more efficiently. This is
  34. ;         left as an exercise for the reader. So are FFT and primality
  35. ;         testing for large integers.
  36. ;
  37. ; Uses variables \%a..\%e as arithmetic registers and for passing results.
  38. ;
  39. ; Elementary operations:
  40. ; Increment one-digit number (\%1) by 1; result in \%r, overflow in \%o:
  41. def inc1  def \%o 0,if = \%1 0 def \%r 1,if = \%1 1 def \%r 2,-
  42. if = \%1 2 def \%r 3,if = \%1 3 def \%r 4,if = \%1 4 def \%r 5,-
  43. if > \%1 4 inc1b \%1
  44. ; internal macro for inc1:
  45. def inc1b if = \%1 5 def \%r 6,if = \%1 6 def \%r 7,if = \%1 7 def \%r 8,-
  46. if = \%1 8 def \%r 9,if = \%1 9 def \%r 0,if = \%1 9 def \%o 1
  47. ; Increment the number in registers \%a..\%e by 1:
  48. def inc5  inc1 \%e, ass \%e \%r,if = \%o 0 go e,inc1 \%d, ass \%d \%r,-
  49. if = \%o 0 go e,inc1 \%c, ass \%c \%r, if = \%o 0 go e,inc1 \%b,-
  50. ass \%b \%r, if = \%o 0 go e,inc1 \%a, ass \%a \%r,:e
  51.  
  52. ; Split multi-digit number into digits, result in \%a..\%e:
  53. def split def \%a 0,def \%b 0,def \%c 0,def \%d 0,def \%e 0,-
  54. if = \%1 0 go e,set cou \%1,:l,inc5 \%a \%b \%c \%d \%e,if cou go l,:e
  55.  
  56. ; Add \%1 to number in \%a..\%e:
  57. def add1  if = \%1 0 go e,set cou \%1,:l,inc5 \%a \%b \%c \%d \%e,-
  58. if cou go l,:e
  59. ; Add two numbers; result in \%a..\%e:
  60. def add0  split 0,if > \v(argc) 1 split \%1,if > \v(argc) 2 add1 \%2,-
  61. if > \v(argc) 3 add1 \%3,if > \v(argc) 4 add1 \%4,-
  62. if > \v(argc) 5 add1 \%5,if > \v(argc) 6 add1 \%6,-
  63. if > \v(argc) 7 add1 \%7,if > \v(argc) 8 add1 \%8,-
  64. if > \v(argc) 9 add1 \%9
  65.  
  66. ; Multiply number in \%a..\%e by \%1; result in \%a..\%e:
  67. def mult1 set cou \%1,ass \%9 \%a\%b\%c\%d\%e,if > \%1 0 go s,split 0,-
  68. go e,:l,add1 \%9,:s,if cou go l,:e
  69. ; Multiply two numbers; result in \%a..\%e:
  70. def mult  split 1,if > \v(argc) 1 split \%1,if > \v(argc) 2 mult1 \%2,-
  71. if > \v(argc) 3 mult1 \%3,if > \v(argc) 4 mult1 \%4,-
  72. if > \v(argc) 5 mult1 \%5,if > \v(argc) 6 mult1 \%6,-
  73. if > \v(argc) 7 mult1 \%7,if > \v(argc) 7 mult1 \%8,-
  74. if > \v(argc) 9 mult1 \%9
  75.  
  76. ; Time-honoured practice: a factorial routine:
  77. def fact  split 1,if = \%1 0 go e,set count \%1,:l,mult1 \v(count),-
  78. if cou go l,:e
  79.  
  80. ; user interface macros: calls for macros above, plus display of result:
  81. def fatal echo Error: \%1\13, def \%1, stop      ; error handler
  82. def tinc1  inc1 \%1,echo \%o\%r                  ; ex: tinc1  5
  83. def tinc5b inc5b \%1 \%2 \%3 \%4 \%5,echo \%a\%b\%c\%d\%e
  84.                                                  ; ex: tinc5b 1 2 3 9 9
  85. def tinc5  split \%1,inc5 \%a \%b \%c \%d \%e,echo \%a\%b\%c\%d\%e
  86.                                                  ; ex: tinc5 99
  87. def tsplit split \%1,echo \%a\%b\%c\%d\%e        ; ex: tsplit 12399
  88. def tadd   add0 \%1 \%2 \%3 \%4 \%5 \%6 \%7 \%8 \%9,echo \%a\%b\%c\%d\%e
  89. def tadd1  add1 \%1,echo \%a\%b\%c\%d\%e         ; ex: split 17, tadd1 15
  90. def tmult1 mult1 \%1,echo \%a\%b\%c\%d\%e        ; ex: split 13, tmult1 7
  91. def tmult  mult \%1 \%2 \%3 \%4 \%5 \%6 \%7 \%8 \%9,echo \%a\%b\%c\%d\%e
  92. def tfact  if not = \v(argc) 2 fatal {TFact takes exactly 1 numeric -
  93. argument},fact \%1,echo \%a\%b\%c\%d\%e
  94. ; Multiplication per PRODUCT macro:
  95. def product   mult \%1 \%2 \%3 \%4 \%5 \%6 \%7 \%8 \%9,-
  96. askq \%9 Result is \%a\%b\%c\%d\%e\59 hit RETURN:,connect
  97. ; Factorial:
  98. def factorial if not = \v(argc) 2 fatal {Factorial takes exactly 1 -
  99. numeric argument},fact \%1,askq \%9 Result is \%a\%b\%c\%d\%e\59 -
  100. hit RETURN:,connect
  101. ; End of MS-DOS-Kermit arithmetic routines.
  102.