1

私は最近、スタンフォード大学の iPhone 開発に関するオンライン コースを iTunes U で受講し始めました。

私は今、最初の数回の講義のために宿題をやろうとしています。基本的な電卓を作成するチュートリアルをたどりましたが、今は最初の割り当てを試みていますが、うまくいかないようです。いくつかの問題があります:

これらを実装しようとしています:

Add the following 4 operation buttons:
• sin : calculates the sine of the top operand on the stack.
• cos : calculates the cosine of the top operand on the stack.
• sqrt : calculates the square root of the top operand on the stack.
• π: calculates (well, conjures up) the value of π. Examples: 3 π * should put
three times the value of π into the display on your calculator, so should 3 Enter π *, 
so should π 3 *. Perhaps unexpectedly, π Enter 3 * + would result in 4 times π being 
shown. You should understand why this is the case. NOTE: This required task is to add π as 
an operation (an operation which takes no arguments off of the operand stack), not a new
way of entering an operand into the display.

performOperation の私のコードは次のとおりです。

-(double)performOperation:(NSString *)operation
{
    double result = 0;
    double result1 = 0;
    if ([operation isEqualToString:@"+"]){
        result = [self popOperand] + [self popOperand];
    }else if ([@"*" isEqualToString:operation]){
        result = [self popOperand] * [self popOperand];
    }
    else if ([@"/" isEqualToString:operation]){
        result = [self popOperand] / [self popOperand];
    }
    else if ([@"-" isEqualToString:operation]){
        result = [self popOperand] - [self popOperand];
    }
    else if ([@"C" isEqualToString:operation])
    {
        [self.operandStack removeAllObjects];
        result = 0;
    }
    else if ([@"sin" isEqualToString:operation])
    {
       result1 = [self popOperand];
        result = sin(result1); 
    }
    else if ([@"cos" isEqualToString:operation])
    {
        result1 = [self popOperand];
        result = cos(result1);
    }
    else if ([@"sqrt" isEqualToString:operation])
    {
        result1 = [self popOperand];
        result = sqrt(result1);
    }

    [self pushOperand:result];
    return result;
}

次のようないくつかの問題に直面しています。

  1. 5 と入力して 3 と入力すると inf が表示されます /
  2. また、sin、cos、および sprt のコードが正しいかどうかもわかりませんか?
4

3 に答える 3

3

私もiTunesUでクラスを受講しているので、これが役立つかもしれません。

まず、アルノーが言ったことは真実です。

ポップする最初のオペランドは実際には分母にある必要があるため、最初に脇に保管する必要があります。(また、分母であるため、= to 0 でないことを確認する必要があります。)また、減算を調べて、演算の順序が正しいことを確認する必要があります。

get inf from 5 enter 3 / は、ウォークスルーで 1 つのことを見逃したことを示しています。In operationPressed: CalculatorViewController で、数字の入力中に enterPressed を送信していますか?

- (IBAction)operationPressed:(UIButton *)sender {
    if (self.userIsInTheMiddleOfEnteringANumber) {
        [self enterPressed];
    }
    double result = [self.brain performOperation:sender.currentTitle];
    NSString *resultString = [NSString stringWithFormat:@"%g",result];
    self.display.text = resultString;
    self.history.text = [CalculatorBrain descriptionOfProgram:self.brain.program];
}
于 2012-02-05T05:36:22.543 に答える
2

ここであなたが見逃しているのはこの点だと思います:

「この必須タスクは、操作として π を追加することです」

次のケースを考えてみましょう - 3π* は答えとして 9.42 を生成するはずです。performOperation: によると、「*」が検出された場合、オペランド スタックは次のようになります。

  • 3.14
  • 3

必要なタスクは、操作として π を追加することです。したがって、その π 操作で必要なことは、適切な値をスタックにプッシュすることだけです。他の関数については、関数電卓で試してみて、正しく実装されているかどうかを確認してください。

于 2012-02-04T05:27:16.380 に答える
2

あなたの部門には 1 つの誤りがあります。

ここで「2 enter 4 enter /」と入力すると、答えとして 2 (4/2) が返されます。0.5 (2/4) にする必要があります。

たぶん、このヒントが役立ちます。

関数を「result = sin([self popOperand]);」に短縮できます。例えば。

とにかく行き詰まったときは、NSLog() を使用して、コンソールに興味深い値を出力してみてください。デバッグ時に非常に便利です。

于 2012-02-04T08:27:12.910 に答える