0

私はプログラミングが初めてで、アプリを初めて試すときは、基本的な電卓を書いて​​います。今のところ、4つの演算子(+、-、*、/)をすべて使用して計算させることができますが、2回目にequalsを押すとクラッシュします。計算を再度実行するにはどうすればよいですか?たとえば、「2 + 2 = 4」と入力した場合、2回目に等しいを押して「6」を生成し、3回目に押すと「8」を生成します。

これが私がこれまでに持っているものです。switchステートメントを使用しています。

-(void)equalsButton:(id)sender

{
    second = [display.text integerValue];

    int result;

    NSArray* components;


    switch (operator)
    {
        case 0:
            components = [display.text componentsSeparatedByString:@"+"];
            first = [(NSString*)[components objectAtIndex:0] integerValue];
            second = [(NSString*)[components objectAtIndex:1] integerValue];
            result = first + second;
            break;
        case 1:
            components = [display.text componentsSeparatedByString:@"-"];
            first = [(NSString*)[components objectAtIndex:0] integerValue];
            second = [(NSString*)[components objectAtIndex:1] integerValue];
            result = first - second;
            break;
        case 2:
            components = [display.text componentsSeparatedByString:@"*"];
            first = [(NSString*)[components objectAtIndex:0] integerValue];
            second = [(NSString*)[components objectAtIndex:1] integerValue];
            result = first * second;
            break;
        case 3:
            components = [display.text componentsSeparatedByString:@"/"];
            first = [(NSString*)[components objectAtIndex:0] integerValue];
            second = [(NSString*)[components objectAtIndex:1] integerValue];
            result = first / second;
            break;
            }

    NSString * result1 = [NSString stringWithFormat:@"%i",result];
    display.text = result1;


}
4

1 に答える 1

0

私が収集しているものから、あなたはその表示フィールドを入力と出力の両方として使用しています。したがって、最初に計算すると、結果のみが得られ、結果に再適用しようとしている演算子と 2 番目のオペランドが失われます。

したがって、あまり多くの変更を加えたくない場合にできることの 1 つは、結果を計算するたびに演算子と 2 番目のオペランドをインスタンス変数に格納することです。そうすれば、演算子を特定できない場合は、最後の演算子を呼び戻したいということになります。したがって、基本的には、switch ステートメントに次の新しいケースを追加することになります。

  • display.text最初のオペランドとして使用
  • 最後に保存されたオペレーターをオペレーターとして使用する
  • 最後に保存した値を 2 番目の値として使用する

その点に到達したら、おそらく冗長なコードをevaluate:(int)operator a:(int)a b:(int)bメソッドまたはそれらの線に沿った何かにマージして、その新しい case ブロック内のすべての演算子に対してすべての eval コードを繰り返す必要がないようにする必要があります。

アップデート

職場のコンピューターに XCode がないため、試すことができません。さらに、残りのコードもありませんが、これを試してください。+で動作するはずです。存在する場合は、他のオペレーターに複製します。

-(void)equalsButton:(id)sender

{
int result;

NSArray* components;


switch (operator)
{
    case 0:
        components = [display.text componentsSeparatedByString:@"+"];
        first = [(NSString*)[components objectAtIndex:0] integerValue];
        if([components count] > 1) {
            second = [(NSString*)[components objectAtIndex:1] integerValue];
        }
        result = first + second;
        break;
    case 1:
        components = [display.text componentsSeparatedByString:@"-"];
        first = [(NSString*)[components objectAtIndex:0] integerValue];
        second = [(NSString*)[components objectAtIndex:1] integerValue];
        result = first - second;
        break;
    case 2:
        components = [display.text componentsSeparatedByString:@"*"];
        first = [(NSString*)[components objectAtIndex:0] integerValue];
        second = [(NSString*)[components objectAtIndex:1] integerValue];
        result = first * second;
        break;
    case 3:
        components = [display.text componentsSeparatedByString:@"/"];
        first = [(NSString*)[components objectAtIndex:0] integerValue];
        second = [(NSString*)[components objectAtIndex:1] integerValue];
        result = first / second;
        break;
        }

NSString * result1 = [NSString stringWithFormat:@"%i",result];
display.text = result1;


}
于 2012-08-07T19:37:03.447 に答える