1

我正在尝试编写一个带有加法、减法等功能的简单计算器。

我的问题是获取用户输入。如何将数值字符串转换为向量?还有什么是编写程序的更好方法?

(ns scalc.core)

(defn add
  [numbers]
  (println (apply + numbers)))

(defn numchoose
  []
  (println "What numbers?: ")
  (let [numbers (read-line)] numbers))

(defn opchoose
  []
  (println "What operation would you like to do?: ")
  (let [operation (read-line)]

    (if (= operation "add")
      (do
        (println "You chose to add.")
        (let [numvect (numchoose)]
              (add [numvect]))))))


(defn -main
  [& args]

  (opchoose)
  (numchoose))

这是错误:

 ~/clj/scalc 1/7 % lein trampoline run -m scalc.core
What operation would you like to do?: 
add
You chose to add.
What numbers?: 
5 7
Exception in thread "main" java.lang.ClassCastException: Cannot cast java.lang.String to java.lang.Number
        at java.lang.Class.cast(Class.java:3005)
        at clojure.core$cast.invoke(core.clj:318)
        at clojure.core$_PLUS_.invoke(core.clj:927)
        at clojure.lang.AFn.applyToHelper(AFn.java:161)
        at clojure.lang.RestFn.applyTo(RestFn.java:132)
        at clojure.core$apply.invoke(core.clj:601)
        at scalc.core$add.invoke(core.clj:5)
        at scalc.core$opchoose.invoke(core.clj:21)
        at scalc.core$_main.doInvoke(core.clj:27)
        at clojure.lang.RestFn.invoke(RestFn.java:397)
        at clojure.lang.Var.invoke(Var.java:411)
        at user$eval15.invoke(NO_SOURCE_FILE:1)
        at clojure.lang.Compiler.eval(Compiler.java:6511)
        at clojure.lang.Compiler.eval(Compiler.java:6501)
        at clojure.lang.Compiler.eval(Compiler.java:6477)
        at clojure.core$eval.invoke(core.clj:2797)
        at clojure.main$eval_opt.invoke(main.clj:297)
        at clojure.main$initialize.invoke(main.clj:316)
        at clojure.main$null_opt.invoke(main.clj:349)
        at clojure.main$main.doInvoke(main.clj:427)
        at clojure.lang.RestFn.invoke(RestFn.java:421)
        at clojure.lang.Var.invoke(Var.java:419)
        at clojure.lang.AFn.applyToHelper(AFn.java:163)
        at clojure.lang.Var.applyTo(Var.java:532)
        at clojure.main.main(main.java:37)

编辑:解决的程序现在看起来像这样:

(ns scalc.core)

(defn add [numbers]
  (reduce + numbers))

(defn numchoose []
  (let [nums (re-seq #"\d+" (read-line))]
    (map #(Integer/parseInt %) nums)))

(defn delegate []
  (println "What operation would you like to do?: ")
  (let [operation (read-line)]

    (when (= operation "add")
      (println "You chose to add.")
      (println "What numbers? ")
      (add (numchoose)))))

(defn -main
  [& args]

  (delegate))
4

1 回答 1

3

要获取数字,您可以使用re-seq

(re-seq #"\d+" "123 456 789") => ("123" "456" 789")

不过,您仍然只有字符串而不是数字。您可以使用 read-string 来获取数字(read-string 很方便,但并非在所有情况下都安全。这里我们确保这些字符串中确实只有数字,所以没关系)。

(read-string "5") => 5

而不是(apply + numbers)你可以使用reduce : (reduce + numbers),你的 add 函数也不应该打印任何东西(你应该尽可能地将函数函数与副作用函数分开)。

(let [numbers (read-line)] numbers)等于(read-line)。不要把事情复杂化!

代替

(if (= operation "add")
      (do ... ))

你可以写

(when (= operation "add")
     ...)

when只是一个宏,当您不需要 ifs 中的 else 情况时很有用(它在 do 中包装条件之后的所有内容,并在条件评估为 false 时评估为 nil)。

于 2012-11-03T20:19:22.433 回答