-2

我正在尝试学习 iOS,并且正在关注斯坦福大学的视频,我正在尝试重新创建一个计算器示例。但由于某种原因,= 按钮不起作用或没有给出结果。我一步一步地练习,但根本不适合我。任何人都可以帮助我,我将非常感激。

//这是我的Calculator.h

#import <UIKit/UIKit.h>
#import "CalculatorBrain.h"

@interface ViewController : UIViewController {

    IBOutlet UILabel *display;
    CalculatorBrain *brain;
    BOOL userIsInTheMiddleOfTypingANumber;
}
-(IBAction) digitPressed:(UIButton *) sender;
-(IBAction) operationPressed: (UIButton *) sender;


@end
<code>





//This is my Calculator.m 

<pre>
#import "ViewController.h"
#import "CalculatorBrain.h"


@implementation ViewController

-(CalculatorBrain *) brain
    {
    if (!brain) brain = [[CalculatorBrain alloc] init];

        return brain;
     }
-(IBAction) digitPressed:(UIButton *) sender
    {
    NSString *digit = [[sender titleLabel]text];

    if (userIsInTheMiddleOfTypingANumber)
    {
        [display setText:[[display text] stringByAppendingString:digit]];

    }else{

        [display setText:digit];
        userIsInTheMiddleOfTypingANumber = YES;


    }
}

-(IBAction) operationPressed: (UIButton *) sender
  {

    if (userIsInTheMiddleOfTypingANumber)
  {
        [[self brain] setOperand:[[display text] doubleValue]];
        userIsInTheMiddleOfTypingANumber = NO;

  }
    NSString *operation = [[sender titleLabel] text];
    double result = [[self brain] performOperation:operation];
    [display setText:[NSString stringWithFormat:@"%g", result]];
  }

@end
<code>





//This is my CalculatorBrain.h

<pre>
#import <Foundation/Foundation.h>

@interface CalculatorBrain : NSObject {

    double operand;
    NSString *waitingOperation;
    double waitingOperand;


}
-(void)setOperand:(double)anOperand;
-(double)performOperation:(NSString *) operation;

@end
<code>





//This is my CalculatorBrain.m
<pre>
#import "CalculatorBrain.h"

@implementation CalculatorBrain

-(void)setOperand:(double)anOperand{

    operand = anOperand;

}
-(void) performWaitingOperation{

    if ([@"+" isEqual:waitingOperation])
    {
        operand = waitingOperand + operand;
    }
    else if ([@"*" isEqual:waitingOperation])
    {
        operand = waitingOperand - operand;
    }
    else if ([@"-" isEqual:waitingOperation])
    {
        operand = waitingOperand * operand;
    }
    else if([@"/" isEqual:waitingOperation])
    {

        if (operand)
        {
            operand = waitingOperand / operand;


      }
   }

}
-(double)performOperation:(NSString *) operation
{

    if ([operation isEqual:@"sqrt"])
    {
        operand = sqrt(operand);
    }
    else if([@"*+/-=" isEqual:operation])
    {
        [self performWaitingOperation];
        waitingOperation = operation;
        waitingOperand = operand;

 }
return operand;

}

@end
<code>
4

3 回答 3

2

除了 if([@"*+/-=" isEqual:operation]) 捕获之外,另一个捕获是:

if ([@"*" isEqual:waitingOperation])
    {
        operand = waitingOperand - operand;
    }
    else if ([@"-" isEqual:waitingOperation])
    {
        operand = waitingOperand * operand;
    }

逻辑是错误的。

接下来将 performWaitingOperation 函数更改为此,

-(double) performWaitingOperation{

    if ([@"+" isEqual:waitingOperation])
    {
        return operand = waitingOperand + operand;
    }
    else if ([@"*" isEqual:waitingOperation])
    {
        return operand = waitingOperand * operand;
    }
    else if ([@"-" isEqual:waitingOperation])
    {
        return operand = waitingOperand - operand;
    }
    else if([@"/" isEqual:waitingOperation])
    {

        if (operand)
        {
            return operand = waitingOperand / operand;


      }
   }else if ([@"=" isEqual:waitingOperation]){
        return operand;
     }

}

和改变[self performWaitingOperation]; to operand = [self performWaitingOperation];

好的入门:计算器教程

希望这可以帮助..

于 2013-02-15T06:01:33.890 回答
1

如果您有任何问题,请参阅以下链接 它为您提供了 Calculator Brain 项目。

于 2013-02-15T04:31:05.547 回答
1

我想这里就是这条线。if([@" +/-=" isEqual:operation])。除非您的字符串“操作”实际上是字符串“ +/-=”,否则它永远不会评估为真。你可能想,instad,分别与每个人进行比较。例如 if([@"*" isEqual:operation] || [@"+" isEqual:operation] ... 等等

于 2013-02-15T04:19:21.080 回答