home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / mskermit / msiocs.ini < prev    next >
Text File  |  2020-01-01  |  5KB  |  111 lines

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