将十进制特征实现为预先存在的功能计算器

时间:2015-07-04 18:20:35

标签: ios objective-c cocoa-touch decimal 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.
}

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

1 个答案:

答案 0 :(得分:1)

这是使用NSDecimalNumber的好地方。对于您键入的每个号码,请执行您喜欢的操作。一旦你点击'。'然后从exponent中减去。 NSDecimalNumber 1234 mantissa-2 exponent转换为12.34

在界面中,添加:

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

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

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

对于操作,您将需要使用NSDecimalNumber操作,例如decimalNumberByAdding:查看NSDecimalNumber的标头以查看如何使用它们。