]]> ]]>

Квадратное уравнение в Lisp

Пример для версий Clojure 1.0.0, Clojure 1.1.0
(defn solve-quadratic [a b c]
  (if (= a 0)
    "Not a quadratic equation."
    (let [D (- (* b b) (* 4 a c))
          k1 (- 0 b)
          k2 (* 2 a)]
         (if (= D 0)
            (str "x = " (/ k1 k2))
            (if (> D 0)
                (let [k3 (Math/sqrt D)]
                     (str (str "x1 = " (/ (+ k1 k3) k2) (str "\nx2 = " (/ (- k1 k3) k2)))))
                (let [k3 (/ (Math/sqrt (- 0 D)) k2)]
                     (str (str (str (str "x1 = (" (/ k1 k2)) (str ", " k3)) ")\nx2 = (") (str (str (/ k1 k2) ", ") (str (- 0 k3) ")") ))
                ))))))

(import '(java.util Scanner))
(def scan (Scanner. *in*))
(def a (.nextInt scan))
(def b (.nextInt scan))
(def c (.nextInt scan))
(println (solve-quadratic a b c))

Комментарии

]]>

blog comments powered by Disqus

]]>

Работа программистам