-1

你好! 在我的计算器程序中,不使用减法/乘法/除法方法,而是使用加法方法。

加法:

-(IBAction) clickPlus
{
    [self processOp: '+'];
}

减法:

-(IBAction) clickMinus
{
    [self processOp: '-'];
}

处理方法:

-(void) processOp: (char) theOp
{
    NSString *opStr;

    op = theOp;

    switch (theOp) {
        case '+':
            opStr = @" + ";
            break;
        case '-':
            opStr = @" – ";
            break;
        case '*':
            opStr = @" * ";
            break;
        case '/':
            opStr = @" ÷ ";
            break;
    }

    [self storeFracPart];
    firstOperand = NO;
    isNumerator = YES;

    [displayString appendString: opStr];
    display.text = displayString;
}

在该方法中使用“op”变量:“ clickEquals

-(IBAction) clickEquals
{
    if ( firstOperand == NO )
    {
        [self storeFracPart];
        [myCalculator performOperation: op];

        [displayString appendString: @" = "];
        [displayString appendString: [myCalculator.accumulator
                                      convertToString]];
        display.text = displayString;

        currentNumber = 0;
        isNumerator = YES;
        firstOperand = YES;
        [displayString setString: @""];
    }
}

你为什么认为'op'会收集垃圾?

如果您需要更多代码,请告诉我。

4

1 回答 1

0

What is op in clickEquals? Is it supposed to be a class ivar? If so processOp is wrong as it's using a local variable called op and is therefore never assigning the ivar.

You should learn to debug this kind of problems yourself. It's not too hard. If you step through the code execution line by line in the debugger and watch what changes when you should be able to find this sort of problems.

于 2012-08-11T09:22:01.580 回答