2012-02-28 4 views
0

私はクラスを取っています。私たちは電卓プログラムで作業しています。私の背景はC++です。私は3のsqrtを入力し、sqrt(3)として私のdescriptionOfProgramメソッドでそれを表示する必要がありますRPN電卓のエントリを取って、新しい、以下の関連プロパティを含む。ここまではクラスです。私の問題を見つけるために "xcode"を検索してください。何か案は?私は基本的な目的のクラスにはあまりよくはありませんが、私は学びたいと思っています。ここに概要があります:目的C初心者

  1. 私のブール値について不平を言います。なぜ私は分からない。私は別のクラスでこれを行い、それはうまくいった。
  2. それは{私はそれを見ていない
  3. 私のキーの使用が嫌いです。私が問題と思うキーの内容をどのように取得するかについては不明です。
  4. は@end

あなたが役立つことを願っで}それは期待

  • スキップなぜ
  • それは望んでいる]が、私は見ていませんよ!ありがとう!

    // 
    // CalculatorBrain.m 
    // Calculator 
    // 
    // Created by Michele Cleary on 2/25/12. 
    // Copyright (c) 2012 __MyCompanyName__. All rights reserved. 
    // 
    
    #import "CalculatorBrain.h" 
    
    @interface CalculatorBrain() 
    @property (nonatomic, strong) NSMutableArray *programStack; 
    @property (nonatomic, strong) NSDictionary *testVariable; 
    @property (nonatomic) BOOL numberHandledNextOperation; 
    - (double) convertRadianToDegree: (double) radian; 
    @end 
    
    @implementation CalculatorBrain 
    
    @synthesize programStack = _programStack; 
    @synthesize testVariable = _testVariable; 
    @synthesize numberHandledNextOperation = _numberHandledNextOperation; 
    
    
    - (NSMutableArray *)programStack 
    { 
        if (_programStack == nil) _programStack = [[NSMutableArray alloc] init]; 
        return _programStack; 
    } 
    //- (void)setOperandStack:(NSMutableArray *)operandStack 
    //{ 
    // _operandStack = operandStack; 
    //} 
    
    
    
    - (void)pushOperand:(double)operand 
    { 
        [self.programStack addObject:[NSNumber numberWithDouble:operand]]; 
    } 
    
    - (double)performOperation:(NSString *)operation 
    { 
        [self.programStack addObject:operation]; 
        return[CalculatorBrain runProgram:self.program]; 
    
    } 
    
    - (id)program 
    { 
        return [self.programStack copy]; 
    } 
    
    + (NSString *)descriptionOfProgram:(id)program 
    { 
        self.numberHandledNextOperation = NO; //1. this is a problem with xcode: member reference type struct objc_class * is a pointer; maybe you meant to use -> 
    
        NSMutableSet * displayDescrip = [[NSMutableSet alloc] init]; 
    
        for(id foundItemKey in program) 
        { 
         if ([foundItemKey isKindOfClass:[NSString class]]) 
          //operator or variable 
         { 
          if ([foundItemKey isEqualToString:@"sin"]&&(!self.numberHandledNextOperation)) 
          { //2. xcode says To match this {. 
           NSObject *nextObj = [program objectForKey:(foundItemKey+1); //3. xcode doesn't like this: arithmetic on pointer to interface id which is not a constant size in non-fragile ABI 
           //[displayDescrip addObject:foundItemKey]; 
          } 
          else if ([foundItemKey isEqualToString:@"cos"]) 
          { 
           //[displayDescrip addObject:foundItemKey]; 
          } 
          else if ([foundItemKey isEqualToString:@"sqrt"]) 
          { 
           //[displayDescrip addObject:foundItemKey]; 
          } 
          else if ([foundItemKey isEqualToString:@"Ï€"]) 
          { 
           //[displayDescrip addObject:foundItemKey]; 
          } 
          else if (![CalculatorBrain isOperationName:foundItemKey]) 
          { 
          //variable 
    
           //[displayDescrip addObject:foundItemkey]; 
          } 
          else if (foundItemKey isKindOfClass:[NSNumber class]) //4. xcode expected ] 
          { 
           //number 
           //if next object is operation 
           if(isOperation([program objectForKey:(foundItemKey+1))) 
           { 
            numberHandledNextOperation = YES; 
            if(isOperationSpecial([program objectForKey:(foundItemKey+1))) 
            { //sin or cos or sqrt need parentheses 
             //[displayDescrip addObject:(foundItemKey+1)]; 
             //[displayDescrip addObject:@"("]; 
             //[displayDescrip addObject:foundItemKey]; 
             //[displayDescrip addObject:@")"]; 
            } 
            else 
            { //regular operation + -/* 
            //[displayDescrip addObject:(foundItemKey+1)]; 
            //[displayDescrip addObject:(foundItemKey)]; 
            } 
            numberHandledNextOperation = YES; 
           } //if 
          } //else if 
    
         } //if 
        } //for 
        //not sure if I need this next thing 
        //NSSet * returnedVarNames = [varNames copy]; 
        //return returnedVarNames; 
        return @"implement this in Assignment 2"; 
    } 
    
    + (double)runProgram:(id)program 
    { 
        NSMutableArray *stack; 
        if ([program isKindOfClass:[NSArray class]]) { 
         stack = [program mutableCopy]; 
        } 
        return [self popOperandOffStack:stack]; 
    } 
    
    + (double)runProgram:(id)program usingVariableValues:(NSDictionary *)variableValues 
    { 
        NSMutableArray *stack; 
        if ([program isKindOfClass:[NSArray class]]) { 
         stack = [program mutableCopy]; 
        } 
    
        if(variableValues) 
        { 
         int numItemsDisplayed = [stack count]; 
         for (int count = 0; count < numItemsDisplayed; count++) 
         { 
          id foundItem = [stack objectAtIndex:count]; 
          if ([foundItem isKindOfClass:[NSString class]]) 
          { 
           NSString * var = [variableValues objectForKey:foundItem]; 
           if(var) 
           { 
            [stack replaceObjectAtIndex:count withObject:[NSNumber numberWithDouble:[var doubleValue]]]; 
           } 
          } 
         } 
        } 
        return [self popOperandOffStack:stack]; 
    } 
    
    + (double)popOperandOffStack:(NSMutableArray *)stack 
    { 
        double result = 0; 
    
        id topOfStack = [stack lastObject]; 
        if (topOfStack) [stack removeLastObject]; 
    
        if([topOfStack isKindOfClass:[NSNumber class]]){ //number 
         result = [topOfStack doubleValue]; 
        } 
        else if ([topOfStack isKindOfClass:[NSString class]]){ //string operation 
         NSString *operation = topOfStack; 
         if ([operation isEqualToString:@"+"]) { 
          result = [self popOperandOffStack:stack] + [self popOperandOffStack:stack]; 
         }else if ([operation isEqualToString:@"*"]) { 
          result = [self popOperandOffStack:stack] * [self popOperandOffStack:stack];  
         }else if ([operation isEqualToString:@"/"]) { 
          double divisor = [self popOperandOffStack:stack]; 
          if (divisor) 
           result = [self popOperandOffStack:stack]/divisor;  
         }else if ([operation isEqualToString:@"-"]) { 
          double subtrahend = [self popOperandOffStack:stack]; 
          result = [self popOperandOffStack:stack] - subtrahend;  
         }else if ([operation isEqualToString:@"sin"]) { 
          result = result = (sin([self popOperandOffStack:stack])); //(sin([self convertRadianToDegree:[self popOperandOffStack:stack]]));  
         }else if ([operation isEqualToString:@"cos"]) { 
          result = (cos([self popOperandOffStack:stack]));  
         }else if ([operation isEqualToString:@"sqrt"]) { 
          result = (sqrt([self popOperandOffStack:stack]));  
         }else if ([operation isEqualToString:@"π"]) { 
          result = M_PI;  
         }else{ 
          result = 0; 
         } 
    
    
        } 
    
        return result; 
    } 
    
    + (NSSet *)variablesUsedInProgram:(id)program 
    { 
        NSMutableSet * varNames = [[NSMutableSet alloc] init]; 
    
        for(id foundItem in program) 
        { 
         if ([foundItem isKindOfClass:[NSString class]]) 
         { 
          if (![CalculatorBrain isOperationName:foundItem]) 
          { 
           [varNames addObject:foundItem]; 
          } 
         } 
        } 
        NSSet * returnedVarNames = [varNames copy]; 
        return returnedVarNames; 
    } 
    
    + (BOOL)isOperationName:(NSString *)foundItem 
    { 
        NSSet *myOperationSet = [NSSet setWithObjects:@"sqrt", @"sin", @"cos", @"π", @"+", @"-", @"*", @"/", nil]; 
        return([myOperationSet containsObject:(foundItem)]); 
    } 
    
    - (NSString *)description 
    { 
        return [NSString stringWithFormat:@"stack = %@", self.programStack]; 
    } 
    
    -(double) convertRadianToDegree: (double) radian; 
    { 
        return M_PI*2*radian/360; 
    } 
    
    @end //6. xcode expected } 
    
  • +0

    ようこそスタックオーバーフロー。メンバーは、特定の回答を持つ完全な質問を投稿するときに最も効果的です。表示されているエラーまたは警告についてスタックオーバーフローを検索しようとしましたか? – Justin

    答えて

    3
    + (NSString *)descriptionOfProgram:(id)program 
    

    あなたが実際にクラス+方法descriptionOfProgramをしたいですか?はいの場合は、C++の静的メソッドのようになります。クラスの特定のインスタンスには属しません。現在のインスタンスへの定数ポインタの隠されたパラメータは渡されません。

    +3

    実際には、クラスメソッドで 'self' _is_ allowedを使用すると、クラスのインスタンスを指すのではなく、クラス自体を指しています。 – omz

    +0

    @omzありがとうございました。 – Mahesh