Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

#import <UIKit/UIKit.h> @interface AppDelegate : UIResponder <UIApplicationDeleg

ID: 668629 • Letter: #

Question

#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

@end

————————

#import "AppDelegate.h"

@interface AppDelegate ()

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

  // Override point for customization after application launch.

  return YES;

}

- (void)applicationWillResignActive:(UIApplication *)application {

  // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.

  // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.

}

- (void)applicationDidEnterBackground:(UIApplication *)application {

  // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.

  // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.

}

- (void)applicationWillEnterForeground:(UIApplication *)application {

  // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.

}

- (void)applicationDidBecomeActive:(UIApplication *)application {

  // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.

}

- (void)applicationWillTerminate:(UIApplication *)application {

  // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.

}

@end

—————————

import <UIKit/UIKit.h>

#import "NSObject+CalculatorBrain.h"

@interface ViewController : UIViewController {

  IBOutlet UILabel *display;

  CalculatorBrain *brain;

  BOOL userIsInTheMiddleOfTypingANumber;

  

}

@end

———————————

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (IBAction)digitPressed:(UIButton*)sender

{

  NSString *digit = [[sender titleLabel] text];

  if (userIsInTheMiddleOfTypingANumber)

  {

  [display setText:[[display text] stringByAppendingString:digit]];

  }

  else

  {

  [display setText:digit];

  userIsInTheMiddleOfTypingANumber = YES;

  }

}

{

  if (userIsInTheMiddleOfTypingANumber){

  [[self brain] setOperand:[[display text] doubleValue]];

  userIsInTheMiddleOfTypingANumber = NO;

  }

  NSString *operation = [[sender titleLabel] text];

  double result = [[self brain] perfromOperation:operation];

  [display setText:[NSString stringWIthFormat:@"%g", result]];

}

- (IBAction)operationPressed:(UIButton*)sender

{

}

@end

—————————————

#import <Foundation/Foundation.h>

@interface NSObject (CalculatorBrain) {

  double operand;

  NSString *waitingOperation;

  double waitingOperand;

}

- (void) setOperand:(double)aDouble;

- (double)performOperation:(NSString *)operation;

@end

——————————

#import <UIKit/UIKit.h>

#import "NSObject+CalculatorBrain.h"

@interface CalculatorViewController : UIViewController {

  IBOutlet UILabel *display;

  CalculatorBrain *brain;

}

-(IBAction)digitPressed:(UIButton*)sender;

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

@end

@implementation NSObject (CalculatorBrain)

- (CalculatorBrain *)brain

{

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

  return brain

}

- (void) setOperand:(double)aDouble;

{

  operand = aDouble;

  

}

- (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])

  {

  operand = - operand;

  }

  else

  {

  [self performWaitingOperation];

  waitingOperation = operation;

  waitingOperand = operand;

  }

  return operand;

}

@end

Hi I'm not really sure where my mistakes are. In need of some help. I followed a tutorial but the NSObject file for calculator brain kept the first par ton it and it will not allow me to implement it in the code with the "+" sign so I'm not sure what to do right now. If anyone can help me please do I have provided a screenshot of all my code.

Explanation / Answer

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

@interface ViewController : UIViewController {
IBOutlet UILabel *display;
CalculatorBrain *brain;
BOOL userIsInTheMiddleOfTypingANumber;
  
}


@end
———————————
#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (IBAction)digitPressed:(UIButton*)sender
{
NSString *digit = [[sender titleLabel] text];
if (userIsInTheMiddleOfTypingANumber)
{
[display setText:[[display text] stringByAppendingString:digit]];
}
else
{
[display setText:digit];
userIsInTheMiddleOfTypingANumber = YES;
}
}

{
if (userIsInTheMiddleOfTypingANumber){
[[self brain] setOperand:[[display text] doubleValue]];
userIsInTheMiddleOfTypingANumber = NO;
}
NSString *operation = [[sender titleLabel] text];
double result = [[self brain] perfromOperation:operation];
[display setText:[NSString stringWIthFormat:@"%g", result]];
}

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

@end
—————————————
#import <Foundation/Foundation.h>

@interface NSObject (CalculatorBrain) {
double operand;
NSString *waitingOperation;
double waitingOperand;
}
- (void) setOperand:(double)aDouble;
- (double)performOperation:(NSString *)operation;

@end
——————————
#import <UIKit/UIKit.h>
#import "NSObject+CalculatorBrain.h"

@interface CalculatorViewController : UIViewController {
IBOutlet UILabel *display;
CalculatorBrain *brain;
}
-(IBAction)digitPressed:(UIButton*)sender;
-(IBAction)operationPressed:(UIButton*)sender;
@end

@implementation NSObject (CalculatorBrain)
- (CalculatorBrain *)brain
{
if (!brain) brain = [[CalculatorBrain alloc] init];
return brain
}
- (void) setOperand:(double)aDouble;
{
operand = aDouble;
  
}
- (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])
{
operand = - operand;
}
else
{
[self performWaitingOperation];
waitingOperation = operation;
waitingOperand = operand;
}
return operand;
}