-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculator.lsp
executable file
·72 lines (67 loc) · 2.23 KB
/
calculator.lsp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
;; Function to split input
(defun split-sequence (str)
;; Splits the string str up into three parts
;;
(let ((op-pos (position-if (lambda (c) (member c '(#\+ #\- #\* #\/))) str)))
;; checks if the char c is one of the operators
(if op-pos
;; if true, extract the operands and operator
(list
(subseq str 0 op-pos)
(string (char str op-pos))
(subseq str (1+ op-pos))
)
;; if false, signals an error
(error "No operator found")
)
)
)
;; Basic arithmetic functions
(defun add (a b) (+ a b)) ;;addition
(defun subtract (a b) (- a b)) ;;subtraction
(defun multiply (a b) (* a b)) ;;multiplication
(defun divide (a b) (/ a b)) ;;division
;; Calculator function to dispatch operations
(defun calculate (operation a b)
(cond ;; determines which arithmetic function to use
((equal operation "+") (add a b))
((equal operation "-") (subtract a b))
((equal operation "*") (multiply a b))
((equal operation "/") (divide a b))
(t (error "Unknown operation"))
)
)
;; Function to parse the input
(defun parse-expression (expr)
;; Parsing the string expression
(let ((parts (split-sequence expr))) ; Corrected call to split-sequence
;; parts split as: 1st operand, operator, 2nd operand
(list
(second parts) ; operator (string)
(parse-integer (first parts)) ; 1st operand (integer)
(parse-integer (third parts)) ; 2nd operand (integer)
)
)
)
;; Main function to handle command line arguments
(defun main ()
;; Checks if there are arguments and calculates the result
;; checks if there is at least one argument
(if (> (length *args*) 0)
;; process the input
(let* (
(expr (first *args*))
(parsed-expr (parse-expression expr)) ;; bind the result of parse-expression to parsed-expr
(operation (first parsed-expr)) ;; extract the operation and operands
(operand-a (second parsed-expr))
(operand-b (third parsed-expr))
)
;; calculate and print the result
(format t "~A~%" (calculate operation operand-a operand-b))
)
;; if the condition is false, print usage information
(format t "Usage: clisp calculator.lisp '<expression>'~%")
)
)
;; Call main if there are arguments
(main)