Ios 在计算器中附加或添加小数点功能

Ios 在计算器中附加或添加小数点功能,ios,objective-c,calculator,Ios,Objective C,Calculator,我试图将点功能添加到我的计算器中,但我一直在努力让它工作 到目前为止,它所做的是添加小数点,但当我按下另一个数字时,它所做的是添加0,而不管您按下的数字是什么 示例:用户输入9点运算/小数,然后输入5=到9.0 这当然是完全错误的 最后,我特别发布了点函数和数字9,它们是产生这个答案的函数 头文件 实现文件..已更新 更新计算 如果你们需要更多的代码参考,请让我知道,并提供它或任何有助于这方面的问题,也请让我知道,我将提供信息:您应该让用户将数字输入NSMutableString,我们称之为bu

我试图将点功能添加到我的计算器中,但我一直在努力让它工作

到目前为止,它所做的是添加小数点,但当我按下另一个数字时,它所做的是添加0,而不管您按下的数字是什么

示例:用户输入9点运算/小数,然后输入5=到9.0

这当然是完全错误的

最后,我特别发布了点函数和数字9,它们是产生这个答案的函数

头文件

实现文件..已更新

更新计算


如果你们需要更多的代码参考,请让我知道,并提供它或任何有助于这方面的问题,也请让我知道,我将提供信息:

您应该让用户将数字输入NSMutableString,我们称之为buffer


要获得浮点值,可以在缓冲区上调用-floatValue。

通过认识到输入的数值在必须进行计算之前并不重要,可以简化问题。您可以让按钮和文本字段只关注文本行为,然后在需要时提取值。e、 g

// do this for digits and @"."
- (IBAction)button9:(id)sender {
    [self appendDigit:@"9"];
}

- (void)appendDigit:(NSString *)digit {
    // handle two special cases: append to only zero means just replace
    // but append decimal point to zero is a regular append
    if ([self.myTextField.text isEqualToString:@"0"] && ![digit isEqualToString:@"."]) {
        self.myTextField.text = digit;
    } else {
        self.myTextField.text = [myTextField.text stringByAppendingString:digit];
    }
}

// negative sign is special too...
self.myTextField.text = [@"-" stringByAppendingString:myTextField.text];
然后,只有在以后,当您需要进行计算时,才能得到如下数值:

NSNumberFormatter *f = [[NSNumberFormatter alloc] init];
[f setNumberStyle:NSNumberFormatterDecimalStyle];
NSNumber *number = [f numberFromString:self.myTextField.text];
最后,另一个很好的表示模式是将数值状态保持为NSNumbers,在需要数学时转换为标量类型

NSNumber *runningTotal;
NSNumber *currentValue;

// say the current operation is multiplication
float runningTotalFloat = [runningTotal floatValue];
float currentValueFloat = [currentValue floatValue];
float newRunningTotal = runningTotalFloat * currentValueFloat;

runningTotal = [NSNumber numberWithFloat: newRunningTotal];

那么,我应该在代码中更改NSMutableString的SelectNumber吗?然后执行刚才为我的点函数显示的示例?这就是我的建议。或者,如果字符串已经在textfield中,您可以在textfield中的字符串中添加一个字符;当我想起来的时候,这可能更干净了…是的,这比我的回答要简单得多我刚刚添加了我的计算功能。。我是否应该将RunningTotal更改为number?是的,您的计算器状态可以保持为数字运行total将是NSNumber,输入的最后一个操作可以是int,等等。重点是当前输入的数字的低级状态最好保持为文本字段的文本。@danh ok谢谢。我现在看到的问题是,在我按下一个数字后,零一直保持不变。。如果我按9是09,我明白你的意思。可以我更新以处理一些特殊情况,并将其重构为一个方法,这样就不必为这些情况到处添加源代码行。
- (void)appendCharacter:(char)c
{
    if(isdigit(c)) {
        [buffer appendFormat:@"%c", c];
    } else if(c == '.') {
        if([buffer rangeOfString:@"."].location != NSNotFound) {
            // user tried to add more than 1 decimal point
            BEEP();
        } else {
            [buffer appendString:@"."];
        }
    } else {
        // user writes some other character.
        // do whatever you like
    }
}
// do this for digits and @"."
- (IBAction)button9:(id)sender {
    [self appendDigit:@"9"];
}

- (void)appendDigit:(NSString *)digit {
    // handle two special cases: append to only zero means just replace
    // but append decimal point to zero is a regular append
    if ([self.myTextField.text isEqualToString:@"0"] && ![digit isEqualToString:@"."]) {
        self.myTextField.text = digit;
    } else {
        self.myTextField.text = [myTextField.text stringByAppendingString:digit];
    }
}

// negative sign is special too...
self.myTextField.text = [@"-" stringByAppendingString:myTextField.text];
NSNumberFormatter *f = [[NSNumberFormatter alloc] init];
[f setNumberStyle:NSNumberFormatterDecimalStyle];
NSNumber *number = [f numberFromString:self.myTextField.text];
NSNumber *runningTotal;
NSNumber *currentValue;

// say the current operation is multiplication
float runningTotalFloat = [runningTotal floatValue];
float currentValueFloat = [currentValue floatValue];
float newRunningTotal = runningTotalFloat * currentValueFloat;

runningTotal = [NSNumber numberWithFloat: newRunningTotal];