• 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
calculator design (was Re: running an external app)
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

calculator design (was Re: running an external app)


  • Subject: calculator design (was Re: running an external app)
  • From: Graham Cox <email@hidden>
  • Date: Tue, 27 May 2008 14:41:16 +1000


On 27 May 2008, at 9:19 am, Nathan wrote:

I'm also having another problem.... how do I append a single digit to an int like a calculator does? Again, I checked the documentation and online but no luck. Am I missing some secret to searching the documentation? Thanks!


To append a digit to a number, you multiply the number by 10 and add the digit. That's basic maths - I believe I learned that when I was about 6yo! (YMMV ;-) There isn't a function for doing this because it's too trivial to be necessary (and actually not that commonly done).

e.g.

int myNumber;

myNumber = (myNumber * 10) + appendedDigit;


However, you don't want to design your calculator to work like this. Trust me. What about the user keying 1, +, 1, enter? OK, not too hard, you notice they keyed the + operator so you can divert that to a separate variable, and so on.. but that rapidly gets hard to scale up. Consider:


1.2345 * (( sqrt(2) + 42 )/3 ) + cos(3.1415)

(you have to imagine the user keying that in one character at a time from left to right). The classic method of handling this is to use a tokenizer that can break up the input into tokens - so each number is a separate token, as is each operator, including brackets. Tokens are pushed onto a stack until an operator is input that can actually operate on the stacked tokens. The operands are pulled off the stack and the operation performed, then the result is then pushed back on the stack. The final operator (= or enter) just pops the final result off the stack. Congratulations, you just wrote a classic L-R descent parser!

(n.b. NSScanner can be used for doing tokenization, and NSMutableArray can be used as a stack).

But if you don't fancy that as an intellectual exercise and would prefer to concentrate on learning Cocoa, the parser I linked in my previous message will do all this for you.

In that case, your calculator becomes an exercise in interface design and implementing a controller - instead of appending each input key to a number, append it instead to a *string*. (You still might want to separate out each number and display it on the calculator "screen" or just display the string so far). When you hit enter, just evaluate the whole entry in one go (internally it works as described above).

Hope this info is useful,

Graham


_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


References: 
 >running an external app (From: Nathan <email@hidden>)
 >Re: running an external app (From: Jens Alfke <email@hidden>)
 >Re: running an external app (From: Nathan <email@hidden>)
 >Re: running an external app (From: Nathan Kinsinger <email@hidden>)
 >Re: running an external app (From: Nathan <email@hidden>)

  • Prev by Date: Re: Cocoa-dev Digest, Vol 5, Issue 919
  • Next by Date: Re: running an external app
  • Previous by thread: Re: running an external app
  • Next by thread: Re: running an external app
  • Index(es):
    • Date
    • Thread