我最近开始在 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;
}
面临一些问题,例如:
- 当我输入 5 时我得到 inf 输入 3 /
- 也不确定我的 sin、cos 和 sprt 代码是否正确?