在计算器中追加或添加小数点功能 [英] Append or Add the decimal point functionality in calculator

查看:615
本文介绍了在计算器中追加或添加小数点功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将点功能添加到计算器中,但是我一直在努力使其工作。

I'm trying to add the dot functionality to my calculator but I have been struggling getting it to work.

到目前为止,它的作用是添加小数点,但是当我按另一个数字时,无论按什么数字,它的作用都是加0。

So far what it does is that adds the decimal point but then when I press another number what it does is add 0 regardless the number you pressed..

示例:用户输入9(点运算/十进制),然后输入5 =到9.0

Example: user input 9 (dot operation/decimal) and then 5 = to 9.0

这当然是完全不正确的。

Which of course is totally incorrect.

最后,我要专门发布点函数以及产生答案的数字9。

Finally, I'm posting specifically the dot function and the number 9 which are the ones that produce that answer.

头文件。

int Method;
float SelectNumber;
float RunningTotal;
bool DecimalActived;

@interface ViewController : UIViewController{
    IBOutlet UILabel *Screen;
}

-(IBAction)Number9:(UIButton *)sender;
-(IBAction)Dot:(UIButton *)sender;  

@end

实施文件。(已更新)

-(IBAction)Number9:(UIButton *)sender{

    [self appendDigit:@"9"];

}

- (IBAction)Dot:(UIButton *)sender {

    NSString *currentText = Screen.text;
    if ([currentText rangeOfString:@"." options:NSBackwardsSearch].length == 0) {
        [self appendDigit:@"."];
    }

}

- (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->Screen.text isEqualToString:@"0"] && ![digit isEqual:@"."]) {
        self->Screen.text = digit;
    } else {
        self->Screen.text = [Screen.text stringByAppendingString:digit];
    }
}

计算(已更新)

- (IBAction)Percent:(UIButton *)sender {

    [self MySwitch];

    Method = 5;
    SelectNumber = 0;
    DecimalActived = FALSE;
    Screen.text = [NSString stringWithFormat:@"%.2g", RunningTotal];

}


- (IBAction)PositiveOrNegative:(UIButton *)sender {

    [self MySwitch];

    Method = 6;
    SelectNumber = 0;
    DecimalActived = FALSE;
    Screen.text = [NSString stringWithFormat:@"%g", RunningTotal];

}


-(IBAction)Equals:(UIButton *)sender{

    [self MySwitch];

    Method = 0;
    SelectNumber = 0;
    DecimalActived = FALSE;
    Screen.text = [NSString stringWithFormat:@"%g", RunningTotal];

}


-(IBAction)AllClear:(UIButton *)sender{

    Method = 0;
    RunningTotal = 0;
    SelectNumber = 0;

    Screen.text = [NSString stringWithFormat:@"0"];

}

- (double) MySwitch {

     SelectNumber = [[NSNumberFormatter alloc] init];
     [SelectNumber setNumberStyle:NSNumberFormatterDecimalStyle];
     RunningTotal = [SelectNumber numberFromString:self->Screen.text];

     if (RunningTotal == 0) {
         RunningTotal = SelectNumber;
     } else{
        switch (Method) {
            case 1:
                RunningTotal = RunningTotal * SelectNumber;
                break;
            case 2:
                RunningTotal = RunningTotal / SelectNumber;
                break;
            case 3:
                RunningTotal = RunningTotal - SelectNumber;
                break;
            case 4:
                RunningTotal = RunningTotal + SelectNumber;
                break;
            case 5:
                RunningTotal = RunningTotal / 100;
                break;
            case 6:
                if(RunningTotal > 0){
                    RunningTotal = - RunningTotal;
                } else{
                    RunningTotal = + RunningTotal;
                }
                break;
            default:
                break;
        }
     }

    return RunningTotal;
}

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

If you guys need more reference to the code please let me know and will supply it or any question to help in this regard let me know too and I will provide the information :)

推荐答案

问题可能是意识到必须在进行计算之前输入的数值才重要,从而简化了此过程。您可以让按钮和文本字段只关心文本行为,然后在需要时提取值。例如,

The problem can be simplified by realizing that the numerical value of the input doesn't matter until you must do a calculation. You can let the buttons and the text field just concern themselves with textual behavior, then extract a value any time you need it. 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];

然后,只有稍后,当您需要进行计算时,才可以得到这样的数值:

Then, only later, when you need to do a calculation, you can get the numerical value like this:

NSNumberFormatter *f = [[NSNumberFormatter alloc] init];
[f setNumberStyle:NSNumberFormatterDecimalStyle];
NSNumber *number = [f numberFromString:self.myTextField.text];

最后,另一个不错的表示模式是将数值状态保持为NSNumbers,当数学为时转换为标量类型需要...

Finally, another nice representational pattern is to keep numerical state as NSNumbers, converting to scalar types when math is needed...

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];

这篇关于在计算器中追加或添加小数点功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆