Skip to content

Commit

Permalink
Lisp Calculator
Browse files Browse the repository at this point in the history
  • Loading branch information
Haceau-Zoac committed Jul 30, 2020
1 parent 05cd133 commit 9e64d6c
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 0 deletions.
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
Lisp-Calculator
===

![Language](https://img.shields.io/badge/Language-Common Lisp-blue.svg?style=flat-square) ![Compiler](https://img.shields.io/badge/Compiler-CLISP 2.49-alice.svg?style=flat-square)

[![Gitee](https://img.shields.io/badge/Gitee-辰落火辉Haceau-red.svg?style=flat-square)](https://gitee.com/haceau/Lisp-Calculator)
[![Github](https://img.shields.io/badge/Github-HaceauZoac-skyblue.svg?style=flat-square)](https://github.com/Haceau-Zoac/Lisp-Calculator)

简介
---
该项目为练习Common Lisp写的计算器,输入格式同Common Lisp。当前版本为v1.0.0。开源许可证为WTFPL。

使用
---
加:(+ 12 23 -5) => 30

减:(- (+ 100 200) (- 300 400) 100) => 300

乘:(* (- (+ -1 +1) -100) 100) => -10000

除:(/ 10086 681) => 3362/227

数字:123456 => 123456

退出:(exit) 或 (quit) => 退出

* 大部分的结果都和CLISP 2.49的结果一样。

待办清单
---
|完成版本|内容|
|-------|---|
|1.0.x|长期维护|
70 changes: 70 additions & 0 deletions src/calculator.lisp
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
(defun calc (lst)
(if (atom lst)
(if (numberp lst)
lst
'Error.)
(if (eql (car lst) '+)
(add (cdr lst))
(if (eql (car lst) '-)
(sub (cdr lst))
(if (eql (car lst) '*)
(mul (cdr lst))
(if (eql (car lst) '/)
(div (cdr lst))
'Error.))))))

(defun add (lst)
(if (atom lst)
0
(if (numberp (car lst))
(+ (car lst) (add (cdr lst)))
(if (consp (car lst))
(if (not (null (cdr lst)))
(+ (calc (car lst)) (add (cdr lst)))
(calc (car lst)))
0))))

(defun sub (lst)
(if (atom lst)
0
(if (numberp (car lst))
(- (car lst) (sub (cdr lst)))
(if (consp (car lst))
(if (not (null (cdr lst)))
(+ (calc (car lst)) (sub (cdr lst)))
(calc (car lst)))
0))))

(defun mul (lst)
(if (atom lst)
1
(if (numberp (car lst))
(* (car lst) (mul (cdr lst)))
(if (consp (car lst))
(if (not (null (cdr lst)))
(* (calc (car lst)) (mul (cdr lst)))
(calc (car lst)))
1))))

(defun div (lst)
(if (atom lst)
1
(if (numberp (car lst))
(/ (car lst) (div (cdr lst)))
(if (consp (car lst))
(if (not (null (cdr lst)))
(/ (calc (car lst)) (div (cdr lst)))
(calc (car lst)))
1))))

(defun start ()
(format T "~% > ")
(let ((input (read)))
(if (or (equal input '(exit))
(equal input '(quit)))
0
(and (write (calc input))
(start)))))

(format T "Haceau-Calculator Lisp Editor v1.0.0")
(start)

0 comments on commit 9e64d6c

Please sign in to comment.