C語言設計---scheme表示

C語言設計 (第一章)

;; petite.exe --script lib.ss

(printf "hello, world!\n")                                          ; 打印第一個程序“hello,world!”

; ================================================================================================
; print fahr to celsius mapping                                     ; 溫度轉換(華氏到攝氏)
; from 0 ... 300

(define lower 0) ;lower bound of temp
(define upper 300) ;upper bound of temp
(define step 20)

(let trans ((fahr lower))
  (if (<= fahr upper)
      (let ((celsius (/ (* 5 (- fahr 32)) 9)))
        (printf "~5,1f ~6,1f\n" fahr celsius)
        (trans (+ fahr step)))))

(printf "newer version\n")

(let for ((fahr 0))
  (if (<= fahr 300)
      (begin
        (printf "~3d ~6,1f\n" fahr (* (/ 5.0 9.0) (- fahr 32)))
        (for (+ fahr 20)))))
; ================================================================================================
(printf "copy input to output\n")   

(let while ()                                                     ; 輸入輸出流字符拷貝
  (let ((c (get-char (current-input-port))))
    (if (not (eof-object? c))
        (begin (put-char (current-output-port) c)
          (while)))))

; =================================================================================================
(let while ((nl 1) (nw 0) (nc 0) (isPreWhite #t))                 ;統計行數,詞數,字符數
  (let ((c (get-char (current-input-port))))
    (if (eof-object? c)
        (printf "lines, words, charaters' count is ~d,~d,~d\n" nl nw nc)
        (let ((isCurWhite (char-whitespace? c)))
          (while (if (char=? c  #\newline) (+ nl 1) nl)
                 (if isCurWhite nw (if isPreWhite (+ nw 1) nw))
                 (+ nc 1)
                 isCurWhite)))))

; ===============================================================================================
(define dv (make-vector 10))
(vector-fill! dv 0)
(define (index c)
  (- (char->integer c)
     (char->integer #\0)))

(let tc ((nw 0) (no 0))                                    ; 統計數字,空格和其他字符數
  (let ((c (get-char (current-input-port))))
    (if (eof-object? c)
        (begin
          ; (printf "---print---\n")
          (let for ((i 0)) 
            (if (< i 10)
                (begin 
                  (printf " ~d" (vector-ref dv i))
                  (for (+ i 1)))))
          (printf ", white space = ~d, other = ~d" nw no))
        (begin
          (cond
            ((char-numeric? c) (vector-set! dv (index c) 
                                            (+ 1 (vector-ref dv (index c)))))
            ((char-whitespace? c) (set! nw (+ 1 nw)))
            (else (set! no (+ 1 no))))
          (tc nw no)))))
; ============================================================================================================
(define (power b n)                                        ; 計算b的n次冪
  (let for ((i 1) (p 1))
    (if (> i n)
        p
        (for (+ i 1) (* p b)))))

(let for ((i 0))
  (if (< i 10)
      (begin (printf "~d ~d ~d\n" i (power 2 i) (power -3 i))
        (for (+ i 1)))))


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章