Re: returning math answers given a string
Re: returning math answers given a string
- Subject: Re: returning math answers given a string
- From: Kai <email@hidden>
- Date: Mon, 27 Jan 2003 11:41:08 +0000
on Mon, 27 Jan 2003 05:32:29 +0000, I wrote:
>
It also allows numbers up to "ninety nine" to be expressed as text:
This could imply that other values, if expressed numerically, would work -
which of course they generally wouldn't. You might therefore find this a
more flexible alternative:
==========================
property t : missing value
property o : missing value
property v : missing value
to setup()
if t is not missing value then return
set {p, m, d} to {{"one", "two", "three", "four", "five", [NO BREAK]
"six", "seven", "eight", "nine", "ten", "eleven", "twelve", [NO BREAK]
"thirteen", "fourteen", "fifteen", "sixteen", "seventeen", [NO BREAK]
"eighteen", "nineteen", "twenty", "thirty", "forty", [NO BREAK]
"fifty", "sixty", "seventy", "eighty", "ninety"}, [NO BREAK]
ASCII character 208, ASCII character 214}
set {t, o, v} to {{"plus", "minus", "-", "times", [NO BREAK]
"multiplied", "divided", "/", "power"}, [NO BREAK]
{"+", m, m, "*", "*", d, d, "^"}, "+*^" & m & d}
set x to p's items 1 thru 19
repeat with y from 20 to 27
set n to p's item y
set x's end to n
repeat with z from 1 to 9
set x's end to n & space & p's item z
end repeat
end repeat
set t to t & x's reverse & "zero"
repeat with i from 99 to 0 by -1
set o's end to i
end repeat
end setup
to getQuestion()
set {tid, q, m} to {text item delimiters, "", "Ask me a math question:"}
repeat
set q to (display dialog m default answer q with icon 1)'s text returned
set {k, a} to (getAnswer to q)
set m to k & " = " & a & return & return & "Ask me another math question:"
end repeat
set text item delimiters to tid
end getQuestion
to getAnswer to q
set {q, a, text item delimiters} to {convertTerms from q, {}, space}
repeat with c in q's words
tell c's contents to if it is in v then
set a's end to it
else
try
set a's end to it as number as string
end try
end if
end repeat
set k to a as string
set a to run script k
try
set a to a as integer
end try
{k, a}
end getAnswer
to convertTerms from q
repeat with n from 1 to count t
set i to t's item n
if i is in q then
set text item delimiters to i
set {q, text item delimiters} to {q's text items, o's item n as string}
set q to q as string
end if
end repeat
q
end convertTerms
setup()
getQuestion()
==========================
>
...I'll leave any further refinements to you...
I lied. ;-)
--
Kai
_______________________________________________
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.