Jump to content

Recommended Posts

Posted (edited)

Greetings everyone.

Recently I've stumbled upon Lee Mac's TextCalculator Lisp which does wonders and I have a project that this lisp really helps me with,the only problem is that I need it to do something extra,like showing the whole operation as the result.For example, if I have the numbers 2*3 I would like it to return as result  2*3=6 and not just plain 6. Thanks in advance!

TextCalcV1-0.lsp

Edited by Panos
Forgot to upload said Lisp
Posted (edited)

What are you my 7th grade math teacher asking me to show my work?!

lightly tested seems to work. use at your own risk.

TextCalcV1-01.lsp

Edited by mhupp
  • Funny 2
Posted (edited)

Hey @mhupp sorry to bother you again,but I noticed that the lisp shows only the last caclulation,is there a way of it to show all the calculations used in the operation?For example 1+2*3 Thanks again in advance!

Edited by Panos
Posted

Guys, do you have string split that splits on multiple delimitators?

 

(stringsplit_m "1 + 2 x 3" (list " + " " - " " x " "/")) 

should ideally return

(list

(list "1" "2" "3") 

(list 0 2)  ;; index of the delimitators found

)

 

@Panos

What do you want  "1 + 2 x 3" to result in? 

- Read left to right = 9

- Read by the order of operators = 7

(The latter is harder to program)

Posted

@Emmanuel Delay It would be better to do the the operation by selection,for example on 1+2x3 it would be 9 cause (1+2)x3.So the first calculation should be with the first 2 numbers picked then the 3rd.

Posted
13 hours ago, Panos said:

I noticed that the lisp shows only the last calculation, is there a way of it to show all the calculations used in the operation? For example 1+2*3 Thanks again in advance!

 

I only changed two lines of code since it was already outputting basically what you wanted. might need to use another method to get what you want.

Maybe input a formula with variables. ask to define each one. then output the answer with updated Values and answers? but I don't know how to do this.

 

(calc "x + y * z")

Define X: "asks user to pick text"

Define Y:

Define Z:

 

it would ouput something like.

1 + (2 *3) = 7

 

;Original code from: http://www.lispology.com/show?JIH  by: johnsondavies
;Infix notation Converter and Calculator
; Supports: + - * / ^ ( )
; _Examples_
;
;(str2prefix "B*8-5/2^(2 PI)")                                  ->      (- (* B 8) (/ 5 (EXPT 2 (* 2 PI))))
;(setq B 4.6)
;(calculate "B*8-5/2^(2 PI)")                                   ->      36.7358
;(eval (infix-prefix '(10 - 3 - 2 - 1)))        ->  4
;(infix-prefix '(10 - 3 - 2 - 1))                       ->      (- (- (- 10 3) 2) 1)
;It also handles unary - and +, and implicit multiplication:
; (infix-prefix '(- 2 a + b))                                   ->      (+ (* (- 2) A) B)

(defun str2prefix (str / i cache)
  (setq i 1 cache "(")
  (repeat (strlen str)
    (setq char (substr str i 1))
    (if (member char '( "+" "-" "*" "/" "(" ")" "^" "%"))
      (setq cache (strcat cache " " char " "))
      (setq cache (strcat cache char))
    )
    (setq i (1+ i))
  )
  (setq cache (strcat cache ")"))
  (infix-prefix (read cache))
)

(defun calculate (str) (eval (str2prefix str)))
(setq *binary-operators* '((+ 1 +) (- 1 -) (* 2 *) (/ 2 /) (^ 3 expt)))  ;removed (x 2 *) to avoid accidents
(setq *unary-operators* '((+ 4 +) (- 4 -)))
(defun weight (c) (cadr (assoc c *binary-operators*)))
(defun binary-opcode (c) (caddr (assoc c *binary-operators*)))
(defun unary-opcode (c) (caddr (assoc c *unary-operators*)))
(defun infix-prefix (ae)
  (cond
    ((atom ae) ae)
    (t (inf-aux ae nil nil))
  )
)

(defun inf-aux (ae operators operands)
  (cond
  ;; Unary operator
    ((and (atom (car ae)) (assoc (car ae) *unary-operators*))
          (inf-iter (cddr ae) operators (cons
                                          (list
                                            (unary-opcode (car ae))
                                            (infix-prefix (cadr ae))
                                          )
                      operands
                    )
          )
    )
    (t (inf-iter (cdr ae) operators (cons (infix-prefix (car ae)) operands)))
  )
)

(defun inf-iter (ae operators operands)
  (cond
    ((and (null ae) (null operators)) (car operands))
    ;; Implicit multiplication
    ((and ae
          (or (listp (car ae))
              (null (weight (car ae)))
          )
     )
      (inf-iter (cons '* ae) operators operands)
    )
    ((and ae
          (or (null operators)
              (> (weight (car ae)) (weight (car operators)))
          )
     )
      (inf-aux (cdr ae) (cons (car ae) operators) operands)
    )
    (t
      (inf-iter ae (cdr operators)
                (cons (list (binary-opcode (car operators))
                            (cadr operands) (car operands)
                      )
                  (cddr operands)
                )
      )
    )
  )
)

 

Posted

Hey @mhupp,thanks again for your responce,since my Lisp level of understanding seems low,how can I actually start this Lisp,what's the command?

Posted

My last post is more of a proof of concept. hoping someone smarter then me could figure out.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...