컴퓨터 프로그래밍에서 꼭 알아두어야 할 주요 진법은 당연히 10진법, 2진법, 8진법, 16진법이다.
다음은  0 에서 15 까지의 정수를 10진법, 2진법, 8진법, 16진법의 표로 만들어 보여주는 Common Lisp 소스 코드이다. 진법 변환에 필요한 함수

        (convertItoA number  radix)

를 Common Lisp 코드로 자체 작성하여 사용하였다.

(아래의 소스는 CLisp으로  실행된다.)


  1. ;;  Filename: makeRadixTable.lsp
  2. ;;            Show the radix table with 10-, 2-, 8-, 16-radices.
  3. ;;
  4. ;;  Execute: clisp makeRadixTable.lsp
  5. ;;
  6. ;;      Date:  2013. 9. 5.
  7. (setf *BASE36* "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ")
  8. (defun println(s)
  9.     (if (equal s nil)
  10.         (format t "~%")
  11.         (format t "~A~%" s) ))
  12. (defun printUsage()
  13.     (println "Usage: clisp makeRadixTable.lsp")
  14.     (println "Show the radix table with 10-, 2-, 8-, 16-radices.") )
  15. (defun join-string-list (string-list)
  16.     ; (format nil "~{~A~^ ~}" string-list))
  17.     (format nil "~{~A~^~}" string-list))
  18. (defun to-right-align(s)
  19.     (let* ((n (length s))
  20.            (a (get-token s))
  21.            (k (length a)))
  22.          (if (= n 0) s)
  23.          (concatenate 'string (substring s k n) a) ))
  24. (defun get-token (s)
  25.   (let* ((i (if (find (elt s 0) "+-") 1 0))
  26.          (d (position-if 'nonwhite-char-p s :start i))
  27.          (f 0))
  28.     (if (equal d nil) s
  29.            (progn
  30.                (setf f (position-if 'white-char-p s :start (1+ d)))
  31.                (if (equal f nil) s
  32.                    (progn
  33.                         (substring s d f) )) )) ))
  34. (defun nonwhite-char-p (c)
  35.     (> (char-code c) (char-code #\Space)))
  36. (defun white-char-p (c)
  37.     (<= (char-code c) (char-code #\Space)))
  38. (defun convertItoA(num radix)
  39.     (let* ((isNegative nil)
  40.            (a 0)
  41.            (arr (list))
  42.            (q 0)
  43.            (r 0))
  44.         (if (equal radix nil) (setf radix 10))
  45.         (if (minusp num) (progn
  46.             (setf isNegative t)
  47.             (setf inum (- num)) ))
  48.         (setf q num)
  49.         (loop while (>= q radix) do
  50.             (setf r (mod q radix))
  51.             (setf q (floor (/ q radix)))
  52.             (setf arr (nconc arr (list (substring *BASE36* r (1+ r)))))
  53.         )
  54.        (setf arr (nconc arr (list (substring *BASE36* q (1+ q)))))
  55.        (if isNegative (nconc (ncons arr (list "-"))) )
  56.        ; (format t "~A~%" arr)
  57.        (setf arr (reverse arr))
  58.        (join-string-list arr)
  59.     )
  60. )
  61. (defun makeTable()
  62.     (let* ((sbuf "")
  63.            (abuf "")
  64.            (tbuf ""))
  65.         (loop for i from 0 below 4 do
  66.             (setf sbuf (concatenate 'string sbuf "+-------")) )
  67.         (setf sbuf (concatenate 'string sbuf "+"))
  68.         (format t "~A~%" sbuf)
  69.         (setf sbuf "|  Dec")
  70.         (setf sbuf (concatenate 'string sbuf (format nil "  |  Bin")))
  71.         (setf sbuf (concatenate 'string sbuf (format nil "  |  Oct")))
  72.         (setf sbuf (concatenate 'string sbuf (format nil "  |  Hex  |")))
  73.         (format t "~A~%" sbuf)
  74.         (setf sbuf "")
  75.         (loop for i from 0 below 4 do
  76.             (setf sbuf (concatenate 'string sbuf "+-------")) )
  77.         (setf sbuf (concatenate 'string sbuf "+"))
  78.         (format t "~A~%" sbuf)
  79.         (loop for i from 0 below 16 do
  80.             (setf sbuf (format nil "|   ~2D" i))
  81.             (setf abuf (convertItoA i 2))
  82.             (setf tbuf (format nil "  |  ~A" (to-right-align (format nil "~4A" abuf))))
  83.             (setf sbuf (concatenate 'string sbuf tbuf))
  84.             (setf abuf (convertItoA i 8))
  85.             (setf tbuf (format nil " |   ~A" (to-right-align (format nil "~2A" abuf))))
  86.             (setf sbuf (concatenate 'string sbuf tbuf))
  87.             (setf abuf (convertItoA i 16))
  88.             (setf tbuf (format nil "  |   ~A  |" (to-right-align (format nil "~2A" abuf))))
  89.             (setf sbuf (concatenate 'string sbuf tbuf))
  90.             (format t "~A~%" sbuf)
  91.         )
  92.         (setf sbuf "")
  93.         (loop for i from 0 below 4 do
  94.             (setf sbuf (concatenate 'string sbuf "+-------")) )
  95.         (setf sbuf (concatenate 'string sbuf "+"))
  96.         (format t "~A~%" sbuf)
  97. ))
  98. (if (and (> (length ext:*args*) 0) (equal (nth 0 ext:*args*) "-h")) (progn
  99.     (printUsage)
  100.     (quit) ))
  101. (makeTable)



실행> clisp makeRadixTable.lsp

+-------+-------+-------+-------+
|  Dec  |   Bin |  Oct  |  Hex  |
+-------+-------+-------+-------+
|    0  |     0 |    0  |    0  |
|    1  |     1 |    1  |    1  |
|    2  |    10 |    2  |    2  |
|    3  |    11 |    3  |    3  |
|    4  |   100 |    4  |    4  |
|    5  |   101 |    5  |    5  |
|    6  |   110 |    6  |    6  |
|    7  |   111 |    7  |    7  |
|    8  |  1000 |   10  |    8  |
|    9  |  1001 |   11  |    9  |
|   10  |  1010 |   12  |    A  |
|   11  |  1011 |   13  |    B  |
|   12  |  1100 |   14  |    C  |
|   13  |  1101 |   15  |    D  |
|   14  |  1110 |   16  |    E  |
|   15  |  1111 |   17  |    F  |
+-------+-------+-------+-------+




 

Posted by Scripter
,