私は最近、スタンフォード大学の iPhone 開発に関するオンライン コースを iTunes U で受講し始めました。
私は今、最初の数回の講義のために宿題をやろうとしています。基本的な電卓を作成するチュートリアルをたどりましたが、今は最初の割り当てを試みていますが、うまくいかないようです。いくつかの問題があります:
これらを実装しようとしています:
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 と入力して 3 と入力すると inf が表示されます /
- また、sin、cos、および sprt のコードが正しいかどうかもわかりませんか?