• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: returning math answers given a string
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: returning math answers given a string


  • Subject: Re: returning math answers given a string
  • From: Michael Glasser <email@hidden>
  • Date: Sat, 25 Jan 2003 20:05:16 -0700

Thank you all who responded... I appreciate the help.

I have come up with the following script. It handles numbers up to 20 quite well, though if you use "twenty one" or something like that it gets rather confused. Any ideas on how I can improve it?

Any idea why it does not work with "/" unless I add the extra loop to convert "/" to " divided "... which only gets converted back! I do not understand that one at all...

-------------------------------------------------------

set theText to text returned of (display dialog "Ask me a math question:" default answer "What is 3 + 3?" buttons {"OK"})
set theAnswer to doMath(theText)
display dialog theAnswer

on doMath(theText)
set makeCalc to ""
set newCalc to ""

repeat with loop from 1 to count of characters in theText
set theChar to character loop of theText
if theChar = "/" then set theChar to " divided "
set newCalc to newCalc & theChar
end repeat

set validChars to "101121314151617181920+-*w().^/w"
try
set theWords to every word in newCalc
repeat with loop from 1 to count of words in newCalc
set theWord to word loop of newCalc
if theWord is "plus" then
set theWord to "+"
else if theWord is "minus" then
set theWord to "-"
else if theWord is "times" then
set theWord to "*"
else if theWord is "divided" then
set theWord to "/"
else if theWord is "power" then
set theWord to "^"
else if theWord contains "square" then
set theWord to " ^ 2"
else if theWord is "point" then
set theWord to "."
else if theWord is "one" then
set theWord to "1"
else if theWord is "two" then
set theWord to "2"
else if theWord is "three" then
set theWord to "3"
else if theWord is "four" then
set theWord to "4"
else if theWord is "five" then
set theWord to "5"
else if theWord is "six" then
set theWord to "6"
else if theWord is "seven" then
set theWord to "7"
else if theWord is "eight" then
set theWord to "8"
else if theWord is "nine" then
set theWord to "9"
else if theWord is "ten" then
set theWord to "10"
else if theWord is "eleven" then
set theWord to "11"
else if theWord is "twelve" then
set theWord to "12"
else if theWord is "thirteen" then
set theWord to "13"
else if theWord is "fourteen" then
set theWord to "14"
else if theWord is "fifteen" then
set theWord to "15"
else if theWord is "sixteen" then
set theWord to "16"
else if theWord is "seventeen" then
set theWord to "17"
else if theWord is "eightteen" then
set theWord to "18"
else if theWord is "nineteen" then
set theWord to "19"
else if theWord is "twenty" then
set theWord to "20"
else if theWord is in validChars then
set theWord to theWord
else
set theWord to ""
end if
set makeCalc to makeCalc & theWord
end repeat
set theCalc to makeCalc & "=" & (run script makeCalc)
on error
set theCalc to theText
end try
if theCalc = "=" then set theCalc to theText
return theCalc
end doMath
_______________________________________________
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.

References: 
 >Re: returning math answers given a string (From: John Stewart <email@hidden>)

  • Prev by Date: Re: returning math answers given a string
  • Next by Date: Re: returning math answers given a string
  • Previous by thread: Re: returning math answers given a string
  • Next by thread: Re: returning math answers given a string
  • Index(es):
    • Date
    • Thread