컴퓨터 프로그래밍에서 꼭 알아두어야 할 주요 진법은 당연히 10진법, 2진법, 8진법, 16진법이다.
다음은 0 에서 15 까지의 정수를 10진법, 2진법, 8진법, 16진법의 표로 만들어 보여주는 Common Lisp 소스 코드이다. 진법 변환에 필요한 함수
(convertItoA number radix)
를 Common Lisp 코드로 자체 작성하여 사용하였다.
(아래의 소스는 CLisp으로 실행된다.)
- ;; Filename: makeRadixTable.lsp
- ;; Show the radix table with 10-, 2-, 8-, 16-radices.
- ;;
- ;; Execute: clisp makeRadixTable.lsp
- ;;
- ;; Date: 2013. 9. 5.
- (setf *BASE36* "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ")
- (defun println(s)
- (if (equal s nil)
- (format t "~%")
- (format t "~A~%" s) ))
- (defun printUsage()
- (println "Usage: clisp makeRadixTable.lsp")
- (println "Show the radix table with 10-, 2-, 8-, 16-radices.") )
- (defun join-string-list (string-list)
- ; (format nil "~{~A~^ ~}" string-list))
- (format nil "~{~A~^~}" string-list))
- (defun to-right-align(s)
- (let* ((n (length s))
- (a (get-token s))
- (k (length a)))
- (if (= n 0) s)
- (concatenate 'string (substring s k n) a) ))
- (defun get-token (s)
- (let* ((i (if (find (elt s 0) "+-") 1 0))
- (d (position-if 'nonwhite-char-p s :start i))
- (f 0))
- (if (equal d nil) s
- (progn
- (setf f (position-if 'white-char-p s :start (1+ d)))
- (if (equal f nil) s
- (progn
- (substring s d f) )) )) ))
- (defun nonwhite-char-p (c)
- (> (char-code c) (char-code #\Space)))
- (defun white-char-p (c)
- (<= (char-code c) (char-code #\Space)))
- (defun convertItoA(num radix)
- (let* ((isNegative nil)
- (a 0)
- (arr (list))
- (q 0)
- (r 0))
- (if (equal radix nil) (setf radix 10))
- (if (minusp num) (progn
- (setf isNegative t)
- (setf inum (- num)) ))
- (setf q num)
- (loop while (>= q radix) do
- (setf r (mod q radix))
- (setf q (floor (/ q radix)))
- (setf arr (nconc arr (list (substring *BASE36* r (1+ r)))))
- )
- (setf arr (nconc arr (list (substring *BASE36* q (1+ q)))))
- (if isNegative (nconc (ncons arr (list "-"))) )
- ; (format t "~A~%" arr)
- (setf arr (reverse arr))
- (join-string-list arr)
- )
- )
- (defun makeTable()
- (let* ((sbuf "")
- (abuf "")
- (tbuf ""))
- (loop for i from 0 below 4 do
- (setf sbuf (concatenate 'string sbuf "+-------")) )
- (setf sbuf (concatenate 'string sbuf "+"))
- (format t "~A~%" sbuf)
- (setf sbuf "| Dec")
- (setf sbuf (concatenate 'string sbuf (format nil " | Bin")))
- (setf sbuf (concatenate 'string sbuf (format nil " | Oct")))
- (setf sbuf (concatenate 'string sbuf (format nil " | Hex |")))
- (format t "~A~%" sbuf)
- (setf sbuf "")
- (loop for i from 0 below 4 do
- (setf sbuf (concatenate 'string sbuf "+-------")) )
- (setf sbuf (concatenate 'string sbuf "+"))
- (format t "~A~%" sbuf)
- (loop for i from 0 below 16 do
- (setf sbuf (format nil "| ~2D" i))
- (setf abuf (convertItoA i 2))
- (setf tbuf (format nil " | ~A" (to-right-align (format nil "~4A" abuf))))
- (setf sbuf (concatenate 'string sbuf tbuf))
- (setf abuf (convertItoA i 8))
- (setf tbuf (format nil " | ~A" (to-right-align (format nil "~2A" abuf))))
- (setf sbuf (concatenate 'string sbuf tbuf))
- (setf abuf (convertItoA i 16))
- (setf tbuf (format nil " | ~A |" (to-right-align (format nil "~2A" abuf))))
- (setf sbuf (concatenate 'string sbuf tbuf))
- (format t "~A~%" sbuf)
- )
- (setf sbuf "")
- (loop for i from 0 below 4 do
- (setf sbuf (concatenate 'string sbuf "+-------")) )
- (setf sbuf (concatenate 'string sbuf "+"))
- (format t "~A~%" sbuf)
- ))
- (if (and (> (length ext:*args*) 0) (equal (nth 0 ext:*args*) "-h")) (progn
- (printUsage)
- (quit) ))
- (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 | +-------+-------+-------+-------+
'프로그래밍 > Common Lisp' 카테고리의 다른 글
삼각형 출력 예제를 통한 여러 가지 소스 비교 with Common Lisp (0) | 2013.09.05 |
---|---|
7비트 ASCII 코드표 만들기 예제 with Common Lisp (0) | 2013.09.05 |
대화형 모드의 진법(radix) 변환 예제 with Common Lisp (0) | 2013.09.02 |
황금비율(golden ratio) 구하기 with Common Lisp (0) | 2013.09.01 |
현재 시각 알아내기 for Common Lisp (0) | 2013.08.31 |