将 Decimal 功能实现到预先存在的功能计算器中

Implementing Decimal feature into pre-existing functional Calculator

我创建了一个计算器应用程序。它功能齐全并且可以正常工作。但是标准计算器中没有小数功能。我一直在进行更新以添加一个小数点按钮,该按钮允许用户输入小数点,以便他们可以解决涉及小数点的问题,这是一个非常需要的功能。我尝试过的每一种方法,以及我在 youtube 上找到的每一个教程都与我当前的计算器不兼容。我需要帮助找到一种方法来添加小数功能,而无需完全重写我的计算器,因为我的截止日期快到了。这是我的代码:

.h 文件:

//
//  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 文件:

//
//  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.
}

我需要帮助找到一种方法来添加小数功能,而无需完全重写我的计算器。用户需要能够使用我的计算器来解决小数问题!非常感谢任何想法或建议!谢谢!!!

这是使用 NSDecimalNumber 的好地方。对于您键入的每个数字,请像您正在做的那样。一旦你点击“。”然后从 exponent 中减去。 NSDecimalNumber 1234 作为 mantissa-2 作为 exponent 转换为 12.34

在界面中,添加:

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

当用户按下小数点按钮时,在buttonDigitPressed:方法中设置decimalPressed = YES;

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

对于操作,您将需要使用 NSDecimalNumber 操作,例如 decimalNumberByAdding: 查看 headers for NSDecimalNumber 以了解如何使用它们。