【问题标题】:Assignment 1 of CS193pCS193p 的作业 1
【发布时间】:2012-02-26 15:30:22
【问题描述】:

我最近开始在 iTunes U 上关注斯坦福大学关于 iPhone 开发的在线课程。

我现在正在为前几节课做家庭作业。我完成了构建基本计算器的演练,但现在我正在尝试第一个作业,但似乎无法解决。有一些问题:

尝试实现这些:

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 时我得到 inf 输入 3 /
  2. 也不确定我的 sin、cos 和 sprt 代码是否正确?

【问题讨论】:

    标签: objective-c ios cs193p


    【解决方案1】:

    因为我也在 iTunesU 上上课,所以这可能会有所帮助。

    首先,Arnaud 说的是真的。

    您弹出的第一个操作数实际上应该在分母中,因此您必须先将其存储到一边。 (也因为它是分母,你应该确保它不是 = 到 0。) 您还应该查看减法以确保那里的操作顺序正确。

    从 5 输入 3 / 得到 inf 是在告诉我你错过了一件事。在 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];
    }
    

    【讨论】:

      【解决方案2】:

      你的部门有一个错误。

      如果您现在键入“2 enter 4 enter /”,您将得到 2 (4/2) 作为答案。它应该是 0.5 (2/4)。

      也许这个提示会有所帮助。

      您可以将您的函数缩短为 'result = sin([self popOperand]);'例如。

      无论如何,当您遇到困难时,请尝试使用 NSLog() 并在控制台中打印有趣的值。调试时真的很有用。

      【讨论】:

        【解决方案3】:

        我认为您在这里缺少的是这一点:

        “这个要求的任务是添加π作为一个操作”

        考虑以下情况 - 3π* 应该产生 9.42 作为答案。根据您的 performOperation: ,当遇到 '*' 时,操作数堆栈应为

        • 3.14
        • 3

        所需的任务是将 π 添加为操作。所以 π 操作需要做的就是将适当的值压入堆栈,仅此而已。至于其他功能,试一试,用科学计算器看看你实现的对不对。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-03-26
          • 2012-05-03
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多