lakshmen
lakshmen

Reputation: 29094

CS193p-Adding backspace to a calculator

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:

Implement a “backspace” button for the user to press if they hit the wrong digit button. This is not intended to be “undo,” so if they hit the wrong operation button, they are out of luck! It’s up to you to decided how to handle the case where they backspace away the entire number they are in the middle of entering, but having the display go completely blank is probably not very user-friendly.

I followed this: Creating backspace on iOS calculator

So the code is

-(IBAction)backspacePressed:(UIButton *)sender {

    NSMutableString *string = (NSMutableString*)[display text];

    int length = [string length];

    NSString *temp = [string substringToIndex:length-1];

    [display setText:[NSString stringWithFormat:@"%@",temp]];

}

My question is shouldn't I check whether my last is a digit or operand? If operand, no execution and if digit, remove it...

Upvotes: 1

Views: 1374

Answers (4)

Bugs
Bugs

Reputation: 21

I have just started on the course myself, this post is quite old now but my solution might help others, food for thought if nothing else:

- (IBAction)deletePressed:(id)sender 
{
    NSString *displayText = [display text];
    int length = [displayText length];
    if (length != 1) {
        NSString *newDisplayText = [displayText substringToIndex:length-1];
        [display setText:[NSString stringWithFormat:@"%@",newDisplayText]];
    } else {
        userIsInTheMiddleOfEnteringANumber = NO;
        NSString *newDisplayText = @"0";
        [display setText:[NSString stringWithFormat:@"%@",newDisplayText]];
    }
}

Upvotes: 1

aleene
aleene

Reputation: 334

I used the approach taken by Joe_Schmoe, which is straightforward. (just remove characters in the dispaly until you reach the end).

If the user continues pressing 'Clear Error', I removed an item from the stack as well.

Upvotes: 1

Joe_Schmoe
Joe_Schmoe

Reputation: 1494

I'm also going through the fall 2011 class on iTunes U.

The walk through gives us an instance variable userIsInTheMiddleOfEnteringANumber so I just checked to see if that is YES.

- (IBAction)backspacePressed {
    if (self.userIsInTheMiddleOfEnteringANumber) {
        if ([self.display.text length] > 1) {
            self.display.text = [self.display.text substringToIndex:[self.display.text length] - 1];
        } else {
            self.display.text = @"0";
            self.userIsInTheMiddleOfEnteringANumber = NO;
        }
    }
}

Upvotes: 1

Andrew R.
Andrew R.

Reputation: 943

First of all, there are several unnecessary steps in that code... And to answer your question, yes, you should check for an operand. Here is how I would write that method with a check:

NSString *text = [display text];
int length = [text length];
unichar c = [text characterAtIndex:length];
NSCharacterSet *digits = [NSCharacterSet decimalCharacterSet];
if ([digits characterIsMember:c] || c == '.') {
    NSString *temp = [[display text] substringToIndex:length-1];
    [display setText:temp];
}

Upvotes: 1

Related Questions