Author Topic: how can i make it work ?  (Read 3363 times)

0 Members and 1 Guest are viewing this topic.

Andrea

  • Water Moccasin
  • Posts: 2372
how can i make it work ?
« on: October 14, 2005, 03:41:30 PM »
Hi all..

I have this..

Code: [Select]
(setq b1 2)
(setq val1 (getstring "Enter the value : "))

so if I answer this:

command: Endter the value : (* b1 3)

it doesn't work..

why ?
Keep smile...

Murphy

  • Guest
Re: how can i make it work ?
« Reply #1 on: October 14, 2005, 04:05:30 PM »
Because you are getting a string. You need to use getreal.

CarlB

  • Guest
Re: how can i make it work ?
« Reply #2 on: October 14, 2005, 04:16:13 PM »
Because you can't reenter lisp.  You should have got that message when you tried it.  You are suppyling a lisp expression to a lisp function that is expecting a string.

Andrea

  • Water Moccasin
  • Posts: 2372
Re: how can i make it work ?
« Reply #3 on: October 14, 2005, 04:16:31 PM »
I know I can make this..


(setq b1 2)
(setq val1 (getreal "Enter the value : "))

(setq result (* b1 val1))


But I need to allow user to put his own calculation..

see ?

so he can put (/ (+ b1 2) (* b1 4.3) 1.44)

see ?
Keep smile...

Andrea

  • Water Moccasin
  • Posts: 2372
Re: how can i make it work ?
« Reply #4 on: October 14, 2005, 04:21:45 PM »
ok ...i found it..

(setq b1 2
        x (getstring "enter your code:")
       y (eval (read x))
)
Keep smile...

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: how can i make it work ?
« Reply #5 on: October 14, 2005, 04:39:31 PM »
Suggestion --

Code: [Select]
(defun evaluate ( expression / result )
    (setq result
        (vl-catch-all-apply
           '(lambda ( )
                (eval
                    (if (eq 'str (type expression))
                        (read expression)
                        expression
                    )
                )
            )
        )
    )   
    result
)

Then your program can intelligently branch if your user provides bozo input:

Code: [Select]
(defun c:Test ( / expression result texteval )
    (setq texteval (getvar "texteval"))
    (setvar "texteval" 0)
    (if     
        (vl-catch-all-error-p
            (setq result
                (evaluate
                    (setq expression
                        (getstring
                            "Enter an expression: "
                            t
                        )
                    )   
                )
            )
        )
        (princ
            (strcat
                "Error: "
                expression
                " => "
                (vl-catch-all-error-message result)
            )
        )
        (princ
            (strcat
                expression
                " => "
                (vl-prin1-to-string result)
            )
        )
    )
    (setvar "texteval" texteval)
    (princ)
)

Test drive:

Code: [Select]
Command: TEST
Enter an expression: (/ 1 0)
Error: (/ 1 0) => divide by zero

Command:
Command: TEST
Enter an expression: (/ 1 2.0)
(/ 1 2.0) => 0.5

Unless of course a pie in the face is acceptable behavior.

:)
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

LE

  • Guest
Re: how can i make it work ?
« Reply #6 on: October 14, 2005, 07:51:07 PM »
Here is a mickey mouse approach, I wrote some years ago..... the request was about on how to add fractional numbers i.e.
12' 5-1/2" + 6' 3-5/8" = 18' 9-1/8"

MMcal = Mickey Mouse Calculator....

Code: [Select]
(defun C:MMcal (/ key fun input inputA data r)
  (initget "+ - Div")
  (if (setq key (getKword "\nOperation [+/-/Div]: "))
    (progn
      (setq fun (eval (read key)))
      (cond
((= fun +)
(while (setq input (getDist "\nInput: "))
   (if input
     (setq data (cons input data))))
(setq r (apply 'fun data)))
((= fun -)
(setq inputA (getDist "\nSubtract base: "))
(while (setq input (getDist "\nFollowing number: "))
   (if input
     (setq data (cons input data))))
(foreach i  data
   (setq inputA (fun inputA i)))
(setq r inputA))
((= key "Div")
(setq inputA (getDist "\nDivide base: "))
(while (setq input (getDist "\nFollowing number: "))
   (if input
     (setq data (cons input data))))
(foreach i  data
   (setq inputA (/ inputA i)))
(setq r inputA)))
      (princ "\nResult: ")
      (princ (rtos r (getvar "lunits") (getvar "luprec")))))
  (princ))