0

我正在开发一个计算器应用程序(下面的代码),它通过 UITextField 获取值,当按下操作数按钮时将它们存储到数组(varArray)中。按下操作数按钮还会将该操作数存储在另一个数组 (opArray) 中并清除 varTextField。按下相等按钮(尚未完全实现,仍在尝试获取值和在我继续之前计算的操作数)将执行计算并将其显示在标签 (equationLabel) 中。所有这些都是为了提供多变量功能。最后,我希望能够输入如下内容:var1、op1、var2、op2、var3...等,并将整个方程式/结果显示在 equationLabel 中。

我还将操作数等同于开关的 int 值。

我现在被卡住了,因为组合变量和操作数的开关似乎根本没有运行。结果项被发送到 equationLabel,但只有默认值 0。

我对编码比较陌生,对objective-c 更是如此,所以我可能只是在这里做一些愚蠢的事情。任何有关开关问题的帮助将不胜感激。

我还应该提到,在每个 switch 案例中,我从 xCode 收到以下警告:“'result' 的本地声明隐藏了实例变量。

更新后的 code2 工作得很好,除了应用程序似乎没有写入 opButtonPush 中的数组,直到第一次推送 equalButton。数组值在该 UI 事件之前返回为 null。

- (IBAction)opButtonPush:(id)sender    //if +, -, /, or x buttons are pushed, add value in varTextField to varArray and button label text to setOpArray
{    
     UIButton *tmpButton = (UIButton *)sender;

     float textValue=0;

     //add operand to opArray to be accessed by displayEquation and calculator then added to equationLabel and calcFunction
    if ([[[tmpButton titleLabel] text] isEqualToString:@"+"])
    {
        textValue = [[varTextField text] floatValue];
        [opArray addObject:[NSNumber numberWithInt:0]];
        [varOpArray addObject:[NSNumber numberWithFloat:textValue]];
        [varOpArray addObject:@"+"];
        [varArray addObject:[NSNumber numberWithFloat:textValue]];
        varTextField.text = @"";
    }
    else if ([[[tmpButton titleLabel] text] isEqualToString:@"-"])
    {
        textValue = [[varTextField text] floatValue];
        [opArray addObject:[NSNumber numberWithInt:1]];
        [varOpArray addObject:[NSNumber numberWithFloat:textValue]];
        [varOpArray addObject:@"-"];
        [varArray addObject:[NSNumber numberWithFloat:textValue]];
        varTextField.text = @"";
    }
    else if ([[[tmpButton titleLabel] text] isEqualToString:@"x"])
    {
        textValue = [[varTextField text] floatValue];
        [opArray addObject:[NSNumber numberWithInt:2]];
        [varOpArray addObject:[NSNumber numberWithFloat:textValue]];
        [varOpArray addObject:@"x"];
        [varArray addObject:[NSNumber numberWithFloat:textValue]];
        varTextField.text = @"";
    }
    else if ([[[tmpButton titleLabel] text] isEqualToString:@"/"])
    {
        textValue = [[varTextField text] floatValue];
        [opArray addObject:[NSNumber numberWithInt:3]];
        [varOpArray addObject:[NSNumber numberWithFloat:textValue]];
        [varOpArray addObject:@"/"];
        [varArray addObject:[NSNumber numberWithFloat:textValue]];
        varTextField.text = @"";
    }
    else if ([[[tmpButton titleLabel] text] isEqualToString:@"+/-"])
    {
        [varTextField setText:@"-"];
    }

    [equationLabel setText:[varOpArray description]];   //send vars and ops, as entered, to equationLabel

    //NSLog(@"%@", varOpArray);
}

- (IBAction)calculator:(id)sender
{
    UIButton *tmpButton = (UIButton *)sender;

    int opCount=0;

    float finalTextValue=0;
    float result = [[varArray objectAtIndex:0] floatValue];

    if ([[[tmpButton titleLabel] text] isEqualToString:@"="]) {
        finalTextValue = [[varTextField text] floatValue];
        [varArray addObject:[varTextField text]];
        [varOpArray addObject:[varTextField text]];
        [varOpArray addObject:@"="];
        [equationLabel setText:[varOpArray description]];

        for (int i=1; i<[varArray count]; i++) {     //use opArray[i] to eval the result and new var
                                                  //then write it to an array
            switch ([[opArray objectAtIndex:opCount] intValue]) {
                case 0:
                    result = result + [[varArray objectAtIndex:(i)] floatValue];
                    [resultArray addObject:[NSNumber numberWithFloat:result]];
                    [equationLabel setText:[varOpArray description]];   //send vars and ops, as entered, to equationLabel
                    break;
                case 1:
                    result = result - [[varArray objectAtIndex:(i)] floatValue];
                    [resultArray addObject:[NSNumber numberWithFloat:result]];
                    [equationLabel setText:[varOpArray description]];   //send vars and ops, as entered, to equationLabel
                    break;
                case 2:
                    result = result * [[varArray objectAtIndex:(i)] floatValue];
                    [resultArray addObject:[NSNumber numberWithFloat:result]];
                    [equationLabel setText:[varOpArray description]];   //send vars and ops, as entered, to equationLabel
                    break;
                case 3:
                    result = result / [[varArray objectAtIndex:(i)] floatValue];
                    [resultArray addObject:[NSNumber numberWithFloat:result]];
                    [equationLabel setText:[varOpArray description]];   //send vars and ops, as entered, to equationLabel
                    break;
                default:
                    NSLog(@"default");
                    break;
            }
            opCount++;
        }
        [varOpArray addObject:[NSNumber numberWithFloat:result]];
        [equationLabel setText:[varOpArray description]];

        [varTextField setText:@""];

        //reset arrays for next function 
        varOpArray = [NSMutableArray new];
        varArray = [NSMutableArray new];
        opArray = [NSMutableArray new];
    }
}

更新代码1:

- (IBAction)opButtonPush:(id)sender    //if +, -, /, or x buttons are pushed, add value in varTextField to varArray and button label text to setOpArray
{    
    UIButton *tmpButton = (UIButton *)sender;

    float textValue=0;

    //add operand to opArray to be accessed by displayEquation and calculator then added to equationLabel and calcFunction
    if ([[[tmpButton titleLabel] text] isEqualToString:@"+"])
    {
        textValue = [[varTextField text] floatValue];
        [opArray addObject:[NSNumber numberWithInt:0]];
        [varOpArray addObject:[NSNumber numberWithFloat:textValue]];
        [varOpArray addObject:@"+"];
        [varArray addObject:[NSNumber numberWithFloat:textValue]];
    }
    else if ([[[tmpButton titleLabel] text] isEqualToString:@"-"])
    {
        textValue = [[varTextField text] floatValue];
        [opArray addObject:[NSNumber numberWithInt:1]];
        [varOpArray addObject:[NSNumber numberWithFloat:textValue]];
        [varOpArray addObject:@"-"];
        [varArray addObject:[NSNumber numberWithFloat:textValue]];
    }
    else if ([[[tmpButton titleLabel] text] isEqualToString:@"x"])
    {
        textValue = [[varTextField text] floatValue];
        [opArray addObject:[NSNumber numberWithInt:2]];
        [varOpArray addObject:[NSNumber numberWithFloat:textValue]];
        [varOpArray addObject:@"x"];
        [varArray addObject:[NSNumber numberWithFloat:textValue]];
    }
    else if ([[[tmpButton titleLabel] text] isEqualToString:@"/"])
    {
        textValue = [[varTextField text] floatValue];
        [opArray addObject:[NSNumber numberWithInt:3]];
        [varOpArray addObject:[NSNumber numberWithFloat:textValue]];
        [varOpArray addObject:@"/"];
        [varArray addObject:[NSNumber numberWithFloat:textValue]];
    }

    //add value of float var to arrays to be accessed by calculator 

    [equationLabel setText:[varOpArray description]];   //send vars and ops, as entered, to equationLabel

    varTextField.text = @"";
    NSLog(@"%@", varOpArray);

}

- (IBAction)calculator:(id)sender
{
    UIButton *tmpButton = (UIButton *)sender;

    int opCount=0;

    float finalTextValue=0;
    float result=0.0;

    if ([[[tmpButton titleLabel] text] isEqualToString:@"="]) {
        finalTextValue = [[varTextField text] floatValue];
        //[opArray addObject:[NSNumber numberWithInt:4]];
        [varOpArray addObject:[NSNumber numberWithFloat:finalTextValue]];
        [varOpArray addObject:@"="];
        [varArray addObject:[NSNumber numberWithFloat:finalTextValue]];

        for (int i=0; i<[varArray count]; i+=2) {            
            switch ([[opArray objectAtIndex:opCount] intValue]) {
                case 0:
                    result = [[varArray objectAtIndex:i] floatValue] + [[varArray objectAtIndex:(i+1)] floatValue];
                    [varOpArray addObject:[NSNumber numberWithFloat:result]];
                    varTextField.text = @"";
                    break;
                case 1:
                    result = [[varArray objectAtIndex:i] floatValue] - [[varArray objectAtIndex:(i+1)] floatValue];
                    [varOpArray addObject:[NSNumber numberWithFloat:result]];
                    varTextField.text = @"";
                    break;
                case 2:
                    result = [[varArray objectAtIndex:i] floatValue] * [[varArray objectAtIndex:(i+1)] floatValue];
                    [varOpArray addObject:[NSNumber numberWithFloat:result]];
                    varTextField.text = @"";
                    break;
                case 3:
                    result = [[varArray objectAtIndex:i] floatValue] / [[varArray objectAtIndex:(i+1)] floatValue];
                    [varOpArray addObject:[NSNumber numberWithFloat:result]];
                    varTextField.text = @"";
                    break;
                default:
                    NSLog(@"default");
                    break;
            }

            opCount++;
        }
        varOpArray = [NSMutableArray new];
        varArray = [NSMutableArray new];
        opArray = [NSMutableArray new];
        [varTextField resignFirstResponder];
    }

}

- (IBAction)textFieldClear:(id)sender
{
    if ([varTextField isFirstResponder])
    {

    }
    else
    {
        [varTextField becomeFirstResponder];
        varTextField.text = @"";
    }
}

原始代码

- (IBAction)opButtonPush:(id)sender    //if +, -, /, or * buttons are pushed, add value in varTextField to varArray and button label text to setOpArray
{
    UIButton *tmpButton = (UIButton *)sender;

    //add value of float var to varArray to be accessed by displayEquation and calculator then added to equationLabel and calcFunction
    if ([[tmpButton titleLabel] text] != @"=" || [[tmpButton titleLabel] text] != @"+/-") {
        varArray = [[NSMutableArray alloc] initWithObjects:varTextField.text, nil];
    }

    //add operand to opArray to be accessed by displayEquation and calculator then added to equationLabel and calcFunction

    if ([[[tmpButton titleLabel] text] isEqualToString:@"+"])
    {
        opArray = [[NSMutableArray alloc] initWithObjects:@"+", nil];
    }

    else if ([[[tmpButton titleLabel] text] isEqualToString:@"-"])
    {
        opArray = [[NSMutableArray alloc] initWithObjects:@"-", nil];
    }

    else if ([[[tmpButton titleLabel] text] isEqualToString:@"x"])
    {
        opArray = [[NSMutableArray alloc] initWithObjects:@"*", nil];
    }

    else if ([[[tmpButton titleLabel] text] isEqualToString:@"/"])
    {
        opArray = [[NSMutableArray alloc] initWithObjects:@"/", nil];
    }
    varTextField.text = @"";     //clear varTextField
}

- (IBAction)calculator:(id)sender
{
    [equationLabel setText:@"x + y = z"];

    NSString *tmpVar;
    NSString *tmpOp;
    float var;

    int varCount = [varArray count];
    int opCount = [opArray count];

    UIButton *tmpButton = (UIButton *)sender;

    if ([[[tmpButton titleLabel] text] isEqualToString:@"="]) {

        //for each instance of varArray and opArray, add it to the string "equation" and equation label
        for (int i=0; i<[varArray count]; i++) {
            tmpVar = [varArray objectAtIndex:i];
            var = ([tmpVar floatValue]);
            for (int j=0; j<[opArray count]; j++) {
                tmpOp = [opArray objectAtIndex:j];
                //equationLabel = 
            }
        }
    }
    //result =
}
4

2 回答 2

1

不要!=用于比较此行中的字符串:

if ([[tmpButton titleLabel] text] != @"=" || [[tmpButton titleLabel] text] != @"+/-") {

改为使用isEqualToString

if (![[[tmpButton titleLabel] text] isEqualToString:@"="] || ![[[tmpButton titleLabel] text] isEqualToString:@"+/-"]) {

opArray包含类型的对象NSString。要将该值分配给一个 int ,您需要向NSString. intValue您还想使用objectAtIndexopArray.

由于您将其用作数组的索引,因此您需要做两件事:

1)设置opCount为循环0 之前for

opCount = 0;

2) 在您的switch陈述中,执行以下操作:

switch([[opArray objectAtIndex:opCount] intValue]) {

我仍然看到几个问题:

1)你确定你没有点击 switch 语句吗?NSLog在你的开关之前放一个。

2)在一个等式中,1+2*3/4-5=每个操作数实际上只有一个操作,第二个操作数是前一个结果。一旦=被检测到,我将设置result为 varArray 中的第一个值,然后我将从for循环中的第二个操作数开始(开始您的索引i,每次循环1仅增加1。使用该变量作为您的第一个操作数你的计算。iresult

3)请在上面的问题中留下您的代码的每个版本,否则答案对后来出现并阅读它们的人来说没有意义。

4)只有在计算出最终结果之后,您才想做(即在您的for循环之后)。

[varOpArray addObject:[NSNumber numberWithFloat:result]];

varOpArray在清除之前不要忘记显示。

5)显然,您的类中有一个实例变量(或属性),result这就是您收到错误消息的原因。摆脱它,只使用您定义的本地结果。

此修改后的代码有效:

- (IBAction)opButtonPush:(id)sender {
    UIButton *tmpButton = (UIButton *)sender;

    float textValue=0;

    //add operand to opArray to be accessed by displayEquation and calculator then added to equationLabel and calcFunction
    if ([[[tmpButton titleLabel] text] isEqualToString:@"+"])
    {
        textValue = [[varTextField text] floatValue];
        [opArray addObject:[NSNumber numberWithInt:0]];
        [varOpArray addObject:[NSNumber numberWithFloat:textValue]];
        [varOpArray addObject:@"+"];
        [varArray addObject:[NSNumber numberWithFloat:textValue]];
    }
    else if ([[[tmpButton titleLabel] text] isEqualToString:@"-"])
    {
        textValue = [[varTextField text] floatValue];
        [opArray addObject:[NSNumber numberWithInt:1]];
        [varOpArray addObject:[NSNumber numberWithFloat:textValue]];
        [varOpArray addObject:@"-"];
        [varArray addObject:[NSNumber numberWithFloat:textValue]];
    }
    else if ([[[tmpButton titleLabel] text] isEqualToString:@"x"])
    {
        textValue = [[varTextField text] floatValue];
        [opArray addObject:[NSNumber numberWithInt:2]];
        [varOpArray addObject:[NSNumber numberWithFloat:textValue]];
        [varOpArray addObject:@"x"];
        [varArray addObject:[NSNumber numberWithFloat:textValue]];
    }
    else if ([[[tmpButton titleLabel] text] isEqualToString:@"/"])
    {
        textValue = [[varTextField text] floatValue];
        [opArray addObject:[NSNumber numberWithInt:3]];
        [varOpArray addObject:[NSNumber numberWithFloat:textValue]];
        [varOpArray addObject:@"/"];
        [varArray addObject:[NSNumber numberWithFloat:textValue]];
    }

    //add value of float var to arrays to be accessed by calculator 

    [equationLabel setText:[varOpArray description]];   //send vars and ops, as entered, to equationLabel

    varTextField.text = @"";
    NSLog(@"%@", varOpArray);

}

- (IBAction)calculator:(id)sender {
    UIButton *tmpButton = (UIButton *)sender;

    int opCount=0;

    float finalTextValue=0;
    float result=0.0;

    if ([[[tmpButton titleLabel] text] isEqualToString:@"="]) {
        finalTextValue = [[varTextField text] floatValue];
        //[opArray addObject:[NSNumber numberWithInt:4]];
        [varOpArray addObject:[NSNumber numberWithFloat:finalTextValue]];
        [varOpArray addObject:@"="];
        [varArray addObject:[NSNumber numberWithFloat:finalTextValue]];

        result = [[varArray objectAtIndex:0] floatValue];

        for (int i=1; i<[varArray count]; i++) { 
            NSLog(@"switch hit");
            switch ([[opArray objectAtIndex:opCount] intValue]) {
                case 0:
                    result = result + [[varArray objectAtIndex:i] floatValue];
                    varTextField.text = @"";
                    break;
                case 1:
                    result = result - [[varArray objectAtIndex:i] floatValue];
                    varTextField.text = @"";
                    break;
                case 2:
                    result = result * [[varArray objectAtIndex:i] floatValue];
                    varTextField.text = @"";
                    break;
                case 3:
                    result = result / [[varArray objectAtIndex:i] floatValue];
                    varTextField.text = @"";
                    break;
                default:
                    NSLog(@"default");
                    break;
            }

            opCount++;
        }
        [varOpArray addObject:[NSNumber numberWithFloat:result]];
        [equationLabel setText:[varOpArray description]];      

        varOpArray = [NSMutableArray new];
        varArray = [NSMutableArray new];
        opArray = [NSMutableArray new];
        [varTextField resignFirstResponder];
    }

}
于 2012-09-29T22:25:59.787 回答
0

我试图了解你的项目。我可能错了,但似乎:在for循环中,opCount应该是objectAtIndex;

结果应该是=,而不是+=

于 2012-09-29T20:33:40.513 回答