带小数点的ios 7计算器应用程序

时间:2014-11-13 03:24:16

标签: ios uitextfield calculator

好这是我的代码

它现在将用户输入限制为小数点后的两个字符,并检查小数,但现在它不再允许我添加或减去.... /叹息

//
//  CalculatorViewController.h
//  
//


#import "ViewController.h"

int Method;
long int SelectNumber;
float RunningTotal;


@interface CalculatorViewController : ViewController <UITextFieldDelegate>

@property (strong, nonatomic) IBOutlet UILabel *screen;

- (IBAction)backToMainViewFromCalculatorViewButton:(id)sender;


@end

和我的.m

//
//  CalculatorViewController.m
//  
//


#import "CalculatorViewController.h"
BOOL isDecimal;
float resultNumber;
float displayNumber;
int operation;

NSUInteger decimalPlacesLimit = 2;


@interface CalculatorViewController ()

@end

@implementation CalculatorViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

//    self.screen.text = @"";
    isDecimal = false;
    resultNumber = 0;
    self.screen.adjustsFontSizeToFitWidth = TRUE;
}

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

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (IBAction)backToMainViewFromCalculatorViewButton:(id)sender {

    // Dismiss the VIEW
    // DO NOT SEGUE back
    // DO THIS
    //Going back using segue just stacks views and then soon you run out of memory and APP crashes
    [self dismissViewControllerAnimated:YES completion:NULL];
}

-(void)setResultWithNumber:(int)theNumber{

    if(!isDecimal){
        displayNumber *= 10;
        displayNumber += theNumber;
        self.screen.text = [NSString stringWithFormat:@"%.0f", displayNumber];
    }
    else
    {
        self.screen.text = [self.screen.text stringByAppendingString:[NSString stringWithFormat:@"%d", theNumber]];
    }
    displayNumber = [self.screen.text floatValue];
}

-(void)operationWithNumber:(int)theNumber{

    isDecimal = false;

    if(resultNumber == 0){
        resultNumber = displayNumber;
    }
    else{
        self.screen.text = [NSString stringWithFormat:@"%.2f",resultNumber];
        switch (operation) {
            case 1:
                resultNumber += displayNumber;
                break;
            case 2:
                resultNumber -= displayNumber;
                break;
            case 3:
                resultNumber = displayNumber*resultNumber;
                break;
            case 4:
                resultNumber /= displayNumber;
                break;

            default:
                break;
        }
    }
    operation = theNumber;
    displayNumber = 0;

}
- (IBAction)AC:(id)sender {
    operation = 0;
    resultNumber = 0;
    displayNumber = 0;
    isDecimal = false;
    self.screen.text = [NSString stringWithFormat:@"%i",0];

}
/*
- (IBAction)plus_minus:(id)sender {

    displayNumber = 0 - displayNumber;
    if(isDecimal)
        result.text = [NSString stringWithFormat:@"%.2f", displayNumber];
    else
        result.text = [NSString stringWithFormat:@"%.0f", displayNumber];

}

- (IBAction)divide:(id)sender {
    if(resultNumber != 0){
        [self operationWithNumber:operation];
        result.text = [NSString stringWithFormat:@"%.2f",resultNumber];
        displayNumber = [result.text floatValue];
        resultNumber = 0;
    }
    [self operationWithNumber:4];


}
 */
- (IBAction)seven:(id)sender
{
       NSRange range = [self.screen.text rangeOfString:@"."];

    // Search for a Period
    // If not found append one to the text
    if (range.location == NSNotFound)
    {
        [self setResultWithNumber:7];
        isDecimal = false;
    }
    else
    {
        // If we allready ahve a character move on to test for .
        NSArray *explodedString = [self.screen.text componentsSeparatedByString:@"."];
        NSString *decimalPart = explodedString[1];

        if (decimalPart.length >= decimalPlacesLimit && ![self.screen.text isEqualToString:@""])
        {
            NSLog(@"textField already contains %lu decimal places", (unsigned long)decimalPlacesLimit);
            isDecimal = true;

        }
        else
        {
            isDecimal = false;
            [self setResultWithNumber:7];
        }
    }
}
- (IBAction)eight:(id)sender
{

    NSRange range = [self.screen.text rangeOfString:@"."];

    // Search for a Period
    // If not found append one to the text
    if (range.location == NSNotFound)
    {
        [self setResultWithNumber:8];
    }
    else
    {
        // If we allready ahve a character move on to test for .
        NSArray *explodedString = [self.screen.text componentsSeparatedByString:@"."];
        NSString *decimalPart = explodedString[1];

        if (decimalPart.length >= decimalPlacesLimit && ![self.screen.text isEqualToString:@""])
        {
            NSLog(@"textField already contains %lu decimal places", (unsigned long)decimalPlacesLimit);
             isDecimal = true;
        }
        else
        {
            [self setResultWithNumber:8];
        }
    }
}
- (IBAction)nine:(id)sender
{

    NSRange range = [self.screen.text rangeOfString:@"."];

    // Search for a Period
    // If not found append one to the text
    if (range.location == NSNotFound)
    {
        [self setResultWithNumber:9];
    }
    else
    {
        // If we allready ahve a character move on to test for .
        NSArray *explodedString = [self.screen.text componentsSeparatedByString:@"."];
        NSString *decimalPart = explodedString[1];

        if (decimalPart.length >= decimalPlacesLimit && ![self.screen.text isEqualToString:@""])
        {
            NSLog(@"textField already contains %lu decimal places", (unsigned long)decimalPlacesLimit);
              isDecimal = true;
        }
        else
        {
            [self setResultWithNumber:9];
        }
    }
}
/*
- (IBAction)multiply:(id)sender {
    if(resultNumber != 0){
        [self operationWithNumber:operation];
        result.text = [NSString stringWithFormat:@"%.2f",resultNumber];
        displayNumber = [result.text floatValue];
        resultNumber = 0;
    }
    [self operationWithNumber:3];
}
 */
- (IBAction)six:(id)sender
{
       NSRange range = [self.screen.text rangeOfString:@"."];

    // Search for a Period
    // If not found append one to the text
    if (range.location == NSNotFound)
    {
        [self setResultWithNumber:6];
    }
    else
    {
        // If we allready ahve a character move on to test for .
        NSArray *explodedString = [self.screen.text componentsSeparatedByString:@"."];
        NSString *decimalPart = explodedString[1];

        if (decimalPart.length >= decimalPlacesLimit && ![self.screen.text isEqualToString:@""])
        {
            NSLog(@"textField already contains %lu decimal places", (unsigned long)decimalPlacesLimit);
            isDecimal = true;
        }
        else
        {
            [self setResultWithNumber:6];
        }
    }
}
- (IBAction)five:(id)sender
{

    NSRange range = [self.screen.text rangeOfString:@"."];

    // Search for a Period
    // If not found append one to the text
    if (range.location == NSNotFound)
    {
        [self setResultWithNumber:5];
    }
    else
    {
        // If we allready ahve a character move on to test for .
        NSArray *explodedString = [self.screen.text componentsSeparatedByString:@"."];
        NSString *decimalPart = explodedString[1];

        if (decimalPart.length >= decimalPlacesLimit && ![self.screen.text isEqualToString:@""])
        {
            NSLog(@"textField already contains %lu decimal places", (unsigned long)decimalPlacesLimit);
              isDecimal = true;
        }
        else
        {
            [self setResultWithNumber:5];
        }
    }
}
- (IBAction)four:(id)sender
{

    NSRange range = [self.screen.text rangeOfString:@"."];

    // Search for a Period
    // If not found append one to the text
    if (range.location == NSNotFound)
    {
        [self setResultWithNumber:4];
    }
    else
    {
        // If we allready ahve a character move on to test for .
        NSArray *explodedString = [self.screen.text componentsSeparatedByString:@"."];
        NSString *decimalPart = explodedString[1];

        if (decimalPart.length >= decimalPlacesLimit && ![self.screen.text isEqualToString:@""])
        {
            NSLog(@"textField already contains %lu decimal places", (unsigned long)decimalPlacesLimit);
              isDecimal = true;
        }
        else
        {
            [self setResultWithNumber:4];
        }
    }
}
- (IBAction)substract:(id)sender {
    if(resultNumber != 0){
        [self operationWithNumber:operation];
        self.screen.text = [NSString stringWithFormat:@"%.2f",resultNumber];
        displayNumber = [self.screen.text floatValue];
        resultNumber = 0;
    }
    [self operationWithNumber:2];

}
- (IBAction)three:(id)sender
{

    NSRange range = [self.screen.text rangeOfString:@"."];

    // Search for a Period
    // If not found append one to the text
    if (range.location == NSNotFound)
    {
        [self setResultWithNumber:3];
    }
    else
    {
        // If we allready ahve a character move on to test for .
        NSArray *explodedString = [self.screen.text componentsSeparatedByString:@"."];
        NSString *decimalPart = explodedString[1];

        if (decimalPart.length >= decimalPlacesLimit && ![self.screen.text isEqualToString:@""])
        {
            NSLog(@"textField already contains %lu decimal places", (unsigned long)decimalPlacesLimit);
              isDecimal = true;
        }
        else
        {
            [self setResultWithNumber:3];
        }
    }
}

- (IBAction)two:(id)sender
{

    NSRange range = [self.screen.text rangeOfString:@"."];

    // Search for a Period
    // If not found append one to the text
    if (range.location == NSNotFound)
    {
        [self setResultWithNumber:2];
    }
    else
    {
        // If we allready ahve a character move on to test for .
        NSArray *explodedString = [self.screen.text componentsSeparatedByString:@"."];
        NSString *decimalPart = explodedString[1];

        if (decimalPart.length >= decimalPlacesLimit && ![self.screen.text isEqualToString:@""])
        {
            NSLog(@"textField already contains %lu decimal places", (unsigned long)decimalPlacesLimit);
             isDecimal = true;
        }
        else
        {
            [self setResultWithNumber:2];
        }
    }
}
- (IBAction)one:(id)sender
{

    NSRange range = [self.screen.text rangeOfString:@"."];

    // Search for a Period
    if (range.location == NSNotFound)
    {
        // No period found
        isDecimal = false;
        // set the number
        [self setResultWithNumber:1];
    }
    else
    {
        // If we allready have a character move on to test for .
        NSArray *explodedString = [self.screen.text componentsSeparatedByString:@"."];
        NSString *decimalPart = explodedString[1];

        if (decimalPart.length >= decimalPlacesLimit && ![self.screen.text isEqualToString:@""])
        {
            NSLog(@"textField already contains %lu decimal places", (unsigned long)decimalPlacesLimit);
             isDecimal = true;
        }
        else
        {
            [self setResultWithNumber:1];
        }
    }}


- (IBAction)add:(id)sender {
    if(resultNumber != 0)
    {
        [self operationWithNumber:operation];
        self.screen.text = [NSString stringWithFormat:@"%.2f",resultNumber];
        displayNumber = [self.screen.text floatValue];
        resultNumber = 0;
    }
[self operationWithNumber:1];
}

- (IBAction)zero:(id)sender
{

    NSRange range = [self.screen.text rangeOfString:@"."];

    // Search for a Period
    // If not found append one to the text
    if (range.location == NSNotFound)
    {
       [self setResultWithNumber:0];
    }
    else
    {
     // If we allready ahve a character move on to test for .
     NSArray *explodedString = [self.screen.text componentsSeparatedByString:@"."];
     NSString *decimalPart = explodedString[1];

        if (decimalPart.length >= decimalPlacesLimit && ![self.screen.text isEqualToString:@""])
        {
            NSLog(@"textField already contains %lu decimal places", (unsigned long)decimalPlacesLimit);
             isDecimal = true;
        }
        else
        {
            [self setResultWithNumber:0];
        }
    }
}


- (IBAction)dot:(id)sender {

    isDecimal = true;
    NSRange range = [self.screen.text rangeOfString:@"."];

    // Search for a Period
    // If not found append one to the text
    if (range.location == NSNotFound)
    {
        self.screen.text = [self.screen.text stringByAppendingString:@"."];
    }
    // We must have a period so now lets test for how many places after the decimal
    // and limit it to two



 //   NSLog(@"text on the way: %@", string);


    /*
    NSUInteger decimalPlacesLimit = 2;

    NSRange rangeDot = [self.screen.text rangeOfString:@"." options:NSCaseInsensitiveSearch];
    NSRange rangeComma = [self.screen.text rangeOfString:@"," options:NSCaseInsensitiveSearch];
    if (rangeDot.length > 0 || rangeComma.length > 0)
    {
        if([self.screen.text isEqualToString:@"."])
        {
            NSLog(@"textField already contains a separator");
        }
        else
        {
            NSArray *explodedString = [self.screen.text componentsSeparatedByString:@"."];
            NSString *decimalPart = explodedString[1];

            if (decimalPart.length >= decimalPlacesLimit && ![self.screen.text isEqualToString:@""])
            {
                NSLog(@"textField already contains %lu decimal places", (unsigned long)decimalPlacesLimit);
            }
        }
    }
     */
}




- (IBAction)equals:(id)sender {
    [self operationWithNumber:operation];
    self.screen.text = [NSString stringWithFormat:@"%.2f",resultNumber];
    displayNumber = [self.screen.text floatValue];
    resultNumber = 0;

}



- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSLog(@"text on the way: %@", string);
    NSUInteger decimalPlacesLimit = 2;

    NSRange rangeDot = [textField.text rangeOfString:@"." options:NSCaseInsensitiveSearch];
    NSRange rangeComma = [textField.text rangeOfString:@"," options:NSCaseInsensitiveSearch];
    if (rangeDot.length > 0 || rangeComma.length > 0){
        if([string isEqualToString:@"."]) {
            NSLog(@"textField already contains a separator");
            return NO;
        } else {
            NSArray *explodedString = [textField.text componentsSeparatedByString:@"."];
            NSString *decimalPart = explodedString[1];
            if (decimalPart.length >= decimalPlacesLimit && ![string isEqualToString:@""]) {
                NSLog(@"textField already contains %lu decimal places", (unsigned long)decimalPlacesLimit);
                return NO;
            }
        }
    }

    return YES;
}

@end

我无法弄清楚如何确保检查小数,然后将条目限制为小数点后不超过两个位置且仍然能够计算

任何帮助将不胜感激

特拉维斯

3 个答案:

答案 0 :(得分:1)

我之前构建了一个计算器应用程序。我做的是:
使用标签识别不同的按钮,0表示按钮0,1表示按钮1 ... 99表示按钮点。所有这些按钮都触发了相同的IBAction功能,在此功能中只需检查按下的用户是否可以通过应用规则添加到Label(您可以在此使用UILabel而不是UITextField)上面创建的。 试试这段代码:

// Assume that, the number buttons have tags from 0 - 9 respectively, and dot button has tag 99
- (IBAction)onButtonPressed:(UIButton *)sender
{
    NSRange range = [self.screen.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 = [self.screen.text componentsSeparatedByString:@"."];
            if(explodedString[1].length >= decimalPlacesLimit) {
                canUpdateScreen = NO;
            }
        }
    }

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

答案 1 :(得分:1)

下面

-(IBAction)NUmber1:(id)sender{

    SelectNumber = SelectNumber * 10;
    SelectNumber = SelectNumber + 1;
    Screen.text = [NSString stringWithFormat:@"%i", SelectNumber];
}
you are erasing your current string with new INTEGER value. Try to change to

-(IBAction)NUmber1:(id)sender{
    Screen.text = [Screen.text stringByAppendingString:@"1"];
}

您还需要更改SelectNumber计算逻辑。我建议你在真正需要时从字符串中获取它

double SelectNumber = [Screen.text doubleValue];

答案 2 :(得分:0)

<强>问题

您发布的代码是 UITextFieldDelegate 方法实现。如果您通过代码更新 UITextField 文本,则不会触发此内容。

<强>解决方案

您可以使用以下任何解决方案。

为了简化任务,您可以使用0到9标记按钮0到9以及其他一些值“。”和“,”,如另一个答案中所述。

// 1. Call the method (in your code) on tapping the buttons (0 - 9 and “.”) and conditionally update the UITextField.text (yourTextField.text), like

    update = [self textField:yourTextField shouldChangeCharactersInRange:NSMakeRange(yourTextField.text.length, 1) replacementString:yourButtonLabel];

    if (update)
    {
        // Uodate yourTextField
    }
    else
    {
        // Don’t update yourTextField
    }

// 2. Before updating yourTextField.text, check there is any decimal point or comma already in yourTextField, using

    if([yourTextField.text rangeOfString:@"."].location != NSNotFound) // Update condition to check comma if the tapped button is so
    {
        // Don’t update yourTextField
    }
    else
    {
        // Update yourTextField
    }