Re: returning math answers given a string
Re: returning math answers given a string
- Subject: Re: returning math answers given a string
- From: Andrew Oliver <email@hidden>
- Date: Sat, 25 Jan 2003 13:42:45 -0800
This is what I think is the easiest way to do this.
The idea is that you loop through the input string filtering out characters
that you don't want (i.e. "w", "h", "a", "t", etc.) which leaves you with
the ones you do want: " 3 + 3" which AppleScript can then parse itself.
--define valid characters
property validChars : {"1", "2", "3", "4", "5", "6", "7", "8", "9", "0",
"+", "/", "-", "*", " ", "(", ")", "."}
-- get the question (watch for line wraps)
set inputStr to text returned of (display dialog "Ask me a math question:"
default answer "What is 3 + 3?" buttons {"OK"})
-- start with a blank string
set filteredStr to ""
-- iterate through each character in the string
repeat with aChar in inputStr
-- is this character one we want to keep?
if aChar is in validChars then
-- if so, keep it
set filteredStr to filteredStr & aChar
end if
end repeat
-- by now our filteredStr will contain " 3 + 3" (assuming the default answer
was used).
-- AppleScript can run this as a script.
do script filteredStr
This approach has the advantage that it can handle arbitrarily large numbers
and complex math questions such as "What is 4.025 * (345/16) + (11*14.6)".
Answer: 247.3890625.
Andrew
:)
On 1/25/03 10:41 AM, "Michael Glasser" <email@hidden> wrote:
>
Is there any way to have AppleScipt read a text message such as "what
>
is 3 + 3" and have it calculate the math so it can return the answer
>
(6, I believe. :) )
>
>
I would also like it to be able to do other math operations, and
>
recognize other terms like "plus" instead of "+".
>
>
Anyone ever do this before? Is there a semi-simple / efficient way?
>
>
If I need to have it parse the text in some clever way that is fine, I
>
do not need it to do large numbers... single digit would be good,
>
double digit would be great, triple would be overkill.
>
>
Thanks in advance
>
_______________________________________________
>
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.
_______________________________________________
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.