计算器app实现方法的十进制

时间:2015-07-04 16:28:11

标签: ios objective-c

我正在通过向我的标准计算器添加一个非常需要的小数选项来添加到我当前正在运行的应用程序。我一直在努力寻找一种方法来做到这一点。我终于遇到了今天实现小数的方法。问题是我收到错误所以我无法构建它以查看它是否有效。有人可以告诉我如何解决我遇到的错误吗?或者更好的方法来实现小数?

这是我的代码:

.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 UILabel *calculatorScreen;
@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"

NSUInteger decimalPlacesLimit = 2;

@interface ViewController ()

@end

@implementation ViewController

@synthesize ad1, calculatorScreen;

-(IBAction)buttonDigitPressed:(id)sender {

currentNumber = currentNumber *10 + (float) [sender tag];
calculatorScreen.text = [NSString stringWithFormat:@"%1.2f",currentNumber];
NSRange range = [self.calculatorScreen.text rangeOfString:@"."];

BOOL canUpdateScreen = YES;
if(range.location != NSNotFound) {
    if([sender tag] == 99) {
        // Already got dot, cannot show another dot
        canUpdateScreen = NO;
    } else {
        NSArray *explodedString = (NSArray*)[self.calculatorScreen.text     componentsSeparatedByString:@"."];
        if(explodedString[1].length >= decimalPlacesLimit) {
            canUpdateScreen = NO;
        }
    }
}

if(canUpdateScreen) {
    if([sender tag] == 99) {
        self.calculatorScreen.text = [NSString stringWithFormat:@"%@%@", self.calculatorScreen.text, @"."];
    } else {
        self.calculatorScreen.text = [NSString stringWithFormat:@"%@%ld", self.calculatorScreen.text, (long)[sender tag]];
    }
}
}

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

我收到错误消息:

Error Message I'm Recieving

有人可以告诉我如何解决错误吗?或者,如果有更好的方法来实现小数?我真的需要帮助让小数点工作。谢谢!

2 个答案:

答案 0 :(得分:1)

<?php error_reporting(E_ALL); ini_set('display_errors',1); $conn = mysqli_connect('host name','user name','password','database name') or die(mysqli_connect_error()); $propertyname = mysqli_real_escape_string($conn,$_POST['locationname']); $loadCP = mysqli_query($conn,"INSERT INTO properties (propertyName) VALUES ('$propertyname')") or die(mysqli_error($conn)); if($loadCP){ header("Location: locationbuilder.php"); }else{ echo "some error occur.Please try again"; } ?> explodedStringNSArray *将对象存储为NSArray s,它们基本上只是通用对象。虽然您和我知道id的内容是explodedString s,但编译器却没有。它需要将数组中所有对象保持为NSString *的安全路径。

总结一下: 编译器不知道id是一个字符串。

修正:明确地将explodedString[1]投射到explodedString[1]

NSString *

答案 1 :(得分:0)

NSArray中的对象的类型为id(匿名对象。)您必须将它们转换为所需的类型,或使用括号表示法。

您可以将违规行重写为

    if (((NSString*)explodedString[1]).length >= decimalPlacesLimit) 

    if ([explodedString[1] length] >= decimalPlacesLimit)