home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / tipi / anybase.tpi next >
Text File  |  1993-10-25  |  1KB  |  60 lines

  1. # ANYBASE.TPI
  2. # by Kent Peterson 10/24/93
  3.  
  4. defstr digit$
  5. "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  6. digit$ store
  7.  
  8. defvar frombase
  9. defvar tobase
  10. defstr numin$
  11.  
  12. define ** ( x y -- x**y )
  13. # raises x to the y power 
  14. # Note: only works with non-negative
  15. # y values
  16.  dup
  17.  case 0 of drop drop 1 endof
  18.       1 of drop endof
  19.  default
  20.       over swap
  21.       1 - do over * loop
  22.      swap drop
  23.  endcase
  24. enddef
  25.  
  26. begin
  27.  "Convert number" print$
  28.  get$ ucase$ dup$ numin$ store
  29.  "" =$ if bye endif
  30.  "from base" print$
  31.  getnum frombase store
  32.  "to base" print$
  33.  getnum tobase store
  34.  
  35. # Convert the number to base 10
  36.  0
  37.  numin$ fetch len drop$
  38.  do
  39.   digit$ fetch 
  40.   index 1 numin$ fetch mid$
  41.   instr 1 -
  42.   drop$ drop$
  43.   numin$ fetch len drop$
  44.   index - frombase fetch swap **
  45.   dup 0 = if drop 1 endif
  46.   * +
  47.  loop
  48.  
  49. # Convert the number from base 10
  50. # to the target base
  51.  ""
  52.  begin
  53.   dup tobase fetch mod
  54.   1 + digit$ fetch 1 mid$ swap$ +$
  55.   tobase fetch / dup not
  56.  until drop
  57.  
  58.  print$ cr
  59. 0 until
  60.