I recently started following the online course on iPhone development from Stanford University on iTunes U.
I'm trying to do the homework assignments now for the first couple of lectures. I followed through the walkthrough where I built a basic calculator, but now I'm trying the first assignment and I can't seem to work it out. It's a follows:
Your calculator already works with floating point numbers (e.g. if you press 3 / 4 = it will properly show the resulting value of 0.75), however, there is no way for the user to enter a floating point number. Remedy this. Allow only legal floating point numbers to be entered (e.g. “192.168.0.1” is not a legal floating point number).
First of all, I'm not sure whether a floating point counts as digitPressed
or operationPressed
. I even tried with a new method called floatingPointPressed
but that didn't really work out. Could someone provide pointers on this?
When I saw it as digitPressed, I tried something like this:
if (hasFloatingPoint) {
NSRange range = [[display text] rangeOfString:@"."];
if (range.location == NSNotFound)
{
[display setText:[[display text] stringByAppendingFormat:digit]];
hasFloatingPoint = YES;
}
}
else {
[display setText:[[display text] stringByAppendingFormat:digit]];
}
But I'm missing the key concept here, I think.
I have also tried another solution which I have, sadly, undone already so I can't provide the code but what happend was this: I could press a number, say 5
and then a floating point and 3
so I would end up with 5.3
. I then managed to 'disable' the floating point for the remainder of the input.. But I guess I was a little too strict on it: I couldn't, for example, put in 5.3.2
but after pressing an operation button (+
, etc), it still wouldn't let me press the floating point button. I guess I should reset the bool I used for this?
I'm not looking for a completely written out solution here, but could someone be so kind to provide some basic advice on how to tackle this problem? Some kind of step-by-step overview of what I should do and think about, without actually providing a code solution.
Thanks in advance.