2015-07-04 79 views
0

我創建了一個計算器應用程序。它功能齊全,功能齊全。但標準計算器中沒有小數點。我一直在進行更新以添加一個小數點按鈕,以便用戶輸入小數點,以便他們可以解決涉及小數的問題,這是一個非常需要的功能。我嘗試過的每一種方式,以及我在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

這是使用NSDecimalNumber的好地方。對於你輸入的每個號碼,都喜歡你正在做的事情。一旦你點擊'。'那麼你從exponent減去。與1234mantissa-2作爲exponent一個NSDecimalNumber轉化爲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的標題以瞭解如何使用它們。