Implementing Decimal feature into pre-existing functional Calculator

| | August 11, 2015

I have created a calculator app. It is fully functional and works. But there is not a decimal feature in the standard calculator. I have been working on an update to add a decimal button that would allow users to input decimals so that they could solve problems involving decimals, a much needed feature. Every way I have tried, and every tutorial that I have found on youtube haven’t been compatible with my current calculator. I need help finding a way to add a decimal feature without completely rewriting my calculator, as my deadline is coming up. Here is my code:

.h file:

//
//  ViewController.h
//  CalculatorPlus
//
//  Created by William Harris on 12/20/14.
//  Copyright (c) 2014 William Harris. All rights reserved.
//

#import <UIKit/UIKit.h>
#import <iAd/iAd.h>
#import <StoreKit/StoreKit.h>
#import "Calculator.h"
#import "ViewController2.h"
#import "ViewController3.h"
#import "ViewController4.h"

@class ViewController;

@interface ViewController : UIViewController <UIActionSheetDelegate,        ADBannerViewDelegate> {

float result;
IBOutlet UILabel *calculatorScreen;
NSInteger currentOperation;
float currentNumber;

}

@property (strong, nonatomic) IBOutlet ADBannerView *ad1;
-(IBAction)buttonDigitPressed:(id)sender;
-(IBAction)buttonOperationPressed:(id)sender;
-(IBAction)cancelInput:(id)sender;
-(IBAction)cancelOperation:(id)sender;
-(IBAction)changeCalculatorTapped:(id)sender;

@end

.m file:

//
//  ViewController.m
//  CalculatorPlus
//
//  Created by William Harris on 12/20/14.
//  Copyright (c) 2014 William Harris. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

@synthesize ad1;

-(IBAction)buttonDigitPressed:(id)sender {
    currentNumber = currentNumber *10 + (float) [sender tag];
    calculatorScreen.text = [NSString stringWithFormat:@"%1.2f",currentNumber];
}

-(IBAction)buttonOperationPressed:(id)sender {
    if (currentOperation == 0) result = currentNumber;
    else {
        switch (currentOperation) {
            case 1:
                result = result + currentNumber;
                break;
            case 2:
                result = result - currentNumber;
                break;
            case 3:
                result = result * currentNumber;
                break;
            case 4:
                result = result / currentNumber;
                break;
            case 5:
                currentOperation = 0;
                break;
        }
    }
    currentNumber = 0;
    calculatorScreen.text = [NSString stringWithFormat:@"%1.2f",result];
    if ([sender tag] == 0) result = 0;
    currentOperation = [sender tag];
}

-(IBAction)cancelInput:(id)sender {
    currentNumber = 0;
    calculatorScreen.text = @"0";
} 

-(IBAction)cancelOperation:(id)sender {
    currentNumber = 0;
    calculatorScreen.text = @"0";
    currentOperation = 0;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    calculatorScreen.adjustsFontSizeToFitWidth = true;
}

-(void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

I need help finding a way to add a decimal feature without completely rewriting my calculator. Users need to be able to use my calculator to solve problems with decimals! Any Ideas or Suggestions are much appreciated! Thank You!!!

One Response to “Implementing Decimal feature into pre-existing functional Calculator”

  1. This is a great place to use NSDecimalNumber. For each number you type, do like you are doing. Once you hit the ‘.’ then you subtract from the exponent. An NSDecimalNumber with 1234 as the mantissa and -2 as the exponent translates into 12.34

    In the interface, add:

    NSUInteger mantissa;
    NSInteger exponent;
    NSDecimalNumber *currentNumber;
    BOOL decimalPressed;
    

    When the user presses the decimal button, set decimalPressed = YES; In the buttonDigitPressed: method:

    mantissa = mantissa *10 + [sender tag];
    if (decimalPressed) {
       exponent--;
    }
    NSDecimalNumber *currentNumber = [NSDecimalNumber decimalNumberWithMantissa:mantissa exponent:exponent isNegative:NO];
    

    For the operations, you will need to use the NSDecimalNumber operations, such as decimalNumberByAdding: look at the headers for NSDecimalNumber to see how to use them.

Leave a Reply