【问题标题】:iOS - Stanford CS193P Fall 2011 course, Assignment 2 descriptionOfProgram issueiOS - Stanford CS193P Fall 2011 course, Assignment 2 descriptionOfProgram 问题
【发布时间】:2012-02-03 19:46:03
【问题描述】:

这项任务的一部分包括在显示屏上打印出当前要求解的方程,为此我使用以下方法:

+ (NSString *)descriptionOfTopOfStack:(NSMutableArray *)stack {
    NSMutableString *programFragment = [NSMutableString stringWithString:@""];

    id topOfStack = [stack lastObject];
    if (topOfStack) [stack removeLastObject];

    if ([topOfStack isKindOfClass:[NSNumber class]]) {
        [programFragment appendFormat:@"%g", [topOfStack doubleValue]];
    } else if ([topOfStack isKindOfClass:[NSString class]]) {
        NSString *operation = topOfStack;
        if ([self isDoubleOperandOperation:operation]) {
            [programFragment appendFormat:@"(%@ %@ %@)", [self descriptionOfTopOfStack:stack], operation, [self descriptionOfTopOfStack:stack]];
        } else if ([self isSingleOperandOperation:operation]) {
            [programFragment appendFormat:@"%@( %@ )", operation, [self descriptionOfTopOfStack:stack]];
        } else if ([ self isNoOperandOperation:operation]) {
            [programFragment appendFormat:@"%@", operation];
        } else if ([self isVariable:operation]) {
            [programFragment appendFormat:@"%@", operation];
        }
    }

    return programFragment;
}

+ (NSString *)descriptionOfProgram:(id)program {
    NSMutableArray *stack;
    if ([program isKindOfClass:[NSArray class]]) {
        stack = [program mutableCopy];
    }

    return [self descriptionOfTopOfStack:stack];
}

我的程序计算了结果,一切都很好,唯一的问题是当我输入变量、数字或单操作数运算时,显示器只显示最后一个条目,因为它不会继续迭代其余的数组中存在值,因为没有进行其他递归调用,知道如何让程序在整个堆栈中执行而不破坏输出吗?

【问题讨论】:

    标签: ios recursion nsstring nsmutablearray cs193p


    【解决方案1】:

    我不太清楚你的意思。递归应在变量、数字或单操作数操作处停止。虽然对于 sin(operand) 操作,它应该继续操作数。

    您是否考虑到您的堆栈可能未完全定义?

    假设你输入:3 输入 5 + 6 输入 7 * 9 sqrt

    这应该转化为:3+5, 6, sqrt(7*9)

    所以您的堆栈中仍有三个元素,但您的方法在 sqrt(7*9) 处停止。

    您需要在 处添加检查以查看堆栈中是否还有任何内容,并在必要时继续(并添加逗号)。

    【讨论】:

    • 是的,这是我的问题,我不知道如何继续,我的意思是如何继续连接值而不弄乱我的输出
    • Aleene 谢谢你的意见,这应该给我我需要的东西,忠告:如果你已经回答了,就不要再回答了,只要发表评论来扩展你的答案,我会很酷并全部投票,因为我只能接受一个。再次感谢。
    【解决方案2】:

    好的,然后是另一个提示(将在末尾添加):

    if ([stack count]) {    // did I finish the entire stack?
       [programFragment appendFormat:@"%@, %@", [self describeStack:stack], programFragment];
    }
    

    【讨论】:

    • 你的意思是把它加到我的 descriptionOfTopOfStack 方法的末尾吗?
    【解决方案3】:

    有趣的是,您使用了 NSMutableString,我使用 NSString 并使用了类方法 stringWithFormat。所以每次我的结果都是一个新字符串。我不知道哪种方法更好。

    【讨论】:

      【解决方案4】:

      aleene 已经回答了,但只是为了澄清。我在调用递归函数的方法中添加了 [stack count] 检查。

      + (NSString *)descriptionOfProgram:(id)program {
              NSMutableArray *stack;
              NSString *strDesc = @"";
      
              if ([program isKindOfClass:[NSArray class]]) {
                  // Make a consumable, mutable copy:
                  stack = [program mutableCopy];
              }
      
              while (stack.count) {
                  strDesc = [strDesc stringByAppendingString:[self descriptionOfTopOfStack:stack]];
                  if (stack.count) {
                      // More statements still on stack. We will loop again, but first, append comma separator:
                      strDesc = [strDesc stringByAppendingString:@", "];
                  }
              }
      
              return strDesc;
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-05-13
        • 2021-04-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-07-14
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多