Re: Unquoting a string
Re: Unquoting a string
- Subject: Re: Unquoting a string
- From: Stephen Swift <email@hidden>
- Date: Sun, 14 Jul 2002 23:39:07 -0400
At 7/14/02 7:32 PM, Robert Stretch (email@hidden) Wrote:
>
Then the result is a QUOTED equation. So my question is, after all
>
that, how do I make it so that AppleScript recognizes the equation for
>
what it is supposed to be, an unquoted equation, and calculate it?
Actually, a while back I created a script that would calculate equations
typed into a dialog box. It recognized * / - + and I think absolute value
||. However, in the end I decided I didn't need it and trashed the project.
So I never cleaned up the code or anything. Actually, the order of
operations doesn't work so well. It looks for * then / then + then -
instead of * & / then + & -. But you can take a look at the code. But
don't critique it because as I said, it was never finished.
---Begin---
set dlg to display dialog "Type your equation. Make sure to put a space
between each number." default answer ""
set finalText to " " & text returned of dlg & " "
set eq_list to {}
set OldDelims to AppleScript's text item delimiters
set AppleScript's text item delimiters to " "
set ftxtchars to text items of finalText
set AppleScript's text item delimiters to OldDelims
repeat with i from 1 to (count ftxtchars)
try
if item i of ftxtchars is "+" then
set next_val to "+"
else if item i of ftxtchars is "-" then
set next_val to "-"
else
set next_val to item i of ftxtchars as real
end if
on error
set next_val to item i of ftxtchars
end try
set eq_list to eq_list & next_val
end repeat
set final_val to ""
set mult_list to {}
set not_to_add to {}
repeat with i from 1 to (count eq_list)
if item i of eq_list is "*" then
set new_val to ((item (i - 1) of eq_list) * (item (i + 1) of
eq_list))
set item i of eq_list to new_val
set item (i + 1) of eq_list to new_val
set item (i - 1) of eq_list to new_val
set final_val to item i of eq_list
set not_to_add to not_to_add & (i + 1) & (i - 1)
end if
end repeat
repeat with i from 1 to (count eq_list)
if not_to_add does not contain i then
set mult_list to mult_list & item i of eq_list
end if
end repeat
set div_list to {}
set not_to_add to {}
repeat with i from 1 to (count mult_list)
if item i of mult_list is "/" then
set new_val to ((item (i - 1) of mult_list) / (item (i + 1) of
mult_list))
set item i of mult_list to new_val
set item (i + 1) of mult_list to new_val
set item (i - 1) of mult_list to new_val
set final_val to item i of mult_list
set not_to_add to not_to_add & (i + 1) & (i - 1)
end if
end repeat
repeat with i from 1 to (count mult_list)
if not_to_add does not contain i then
set div_list to div_list & item i of mult_list
end if
end repeat
set add_list to {}
set not_to_add to {}
repeat with i from 1 to (count div_list)
if item i of div_list is "+" then
set new_val to ((item (i - 1) of div_list) + (item (i + 1) of
div_list))
set item i of div_list to new_val
set item (i + 1) of div_list to new_val
set item (i - 1) of div_list to new_val
set final_val to item i of div_list
set not_to_add to not_to_add & (i + 1) & (i - 1)
end if
end repeat
repeat with i from 1 to (count div_list)
if not_to_add does not contain i then
set add_list to add_list & item i of div_list
end if
end repeat
repeat with i from 1 to (count add_list)
if item i of add_list is "-" then
set new_val to ((item (i - 1) of add_list) - (item (i + 1) of
add_list))
set item i of add_list to new_val
set item (i + 1) of add_list to new_val
set item (i - 1) of add_list to new_val
set final_val to item i of add_list
end if
end repeat
display dialog "The answer:" default answer final_val
on absval(num)
if num < 0 then
set num_string to text 2 thru -1 of (num as string)
set num to num_string as number
end if
return num
end absval
---End---
Stephen Swift
email@hidden
-----------------------------------------------
AppleScript Guru - The Mac Observer
http://www.macobserver.com/tips/applescript
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.