Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8433031
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T06:16:45+00:00 2026-06-10T06:16:45+00:00

This is code from an addition calculator that does operations by entering the two

  • 0

This is code from an addition calculator that does operations by entering the two operands first and then the operation; like “5 enter 2 enter +” would result in “7”. When the user taps on a number a double will be sent to pushOperand: When a user taps on the addition button the string @"+" will be sent like to performOperation:. My question is what is the point of making those copies in program and runProgram: if they’re all shallow copies and their elements all end up pointing to the same elements of NSNumber and NSString objects as _programStack, program, and stack?

#import <Foundation/Foundation.h>

@interface CalculatorBrain : NSObject
@property (nonatomic, readonly) id program;

+(double)runProgram:(id)program; 
-(double)performOperation:(NSString *)operation;

@end

#import "CalculatorBrain.h"     

@interface CalculatorBrain ()
@property (nonatomic, strong) NSMutableArray *programStack;
@end

@implementation CalculatorBrain
@synthesize programStack = _programStack;

-(NSMutableArray *) programStack {
    if (!_programStack)
        _programStack = [[NSMutableArray alloc] init];
    return _programStack;
}

-(void)pushOperand:(double)operand {
    [self.programStack addObject: [NSNumber numberWithDouble: operand]];
}

-(double)performOperation:(NSString *)operation {
        [self.programStack addObject: operation];
        double result = [CalculatorBrain runProgram: self.program];
        return result;
    }

-(id)program {
    return [self.programStack copy];
}

+(double)runProgram:(id)program {
    NSMutableArray *stack;
    if ([program isKindOfClass: [NSArray class]])
        stack = [program mutableCopy];
    return [self popOperandOffProgramStack: stack];
}

+(double)popOperandOffProgramStack:(NSMutableArray *)stack {
    double result = 0;
    id topOfStack = [stack lastObject];
    if (topOfStack)
        [stack removeLastObject];

    if ([topOfStack isMemberOfClass: [NSNumber class]])
        result = [topOfStack doubleValue];

    if ([topOfStack isKindOfClass: [NSString class]]) {
        NSString *operation = topOfStack;

        if ([operation isEqualToString: @"+"]) {
            result = [self popOperandOffProgramStack: stack] + [self popOperandOffProgramStack: stack];
    }
    return result;
} 
@end
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T06:16:47+00:00Added an answer on June 10, 2026 at 6:16 am

    NSNumber and NSString are immutable so making a shallow copy of a collection of objects that can’t changee is safe.

    In program it is important to return a copy of programStack rather than the the actual mutable array. This is because programStack is a private internal property declared in a class extension so it is not externally visible. If you returned programStack directly an external user could change it since it is an NSMutableArray. The program method returns an NSArray since copies of mutable objects are immutable, which has the right semantics. You want to give the external user a snapshot of the programStack array, not access to your class internals.

    In runProgram the situation is different. The external user passes CalculatorBrain an NSArray to process and the class’ internal logic requires that the stack have elements popped off the array as it is processed. Thus you need to make a mutableCopy so that it can be mutated for processing.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I got this code from the wordpress <head profile=http://gmpg.org/xfn/11> What does this means? what
I just found out this code from a tutorial for matrix addition in c++
Borrowing this code from several different tutorials on how to use Google maps on
In this code (from the WCF REST starterkit - preview2): protected override SampleItem OnAddItem(SampleItem
I got this code from our frontend guy for headings: <h2 class=headline><span>Foobar</span></h2> The span
I picked up this code from a msdn blog : #include <windows.h> #include <stdio.h>
I am using this code from css tips and tricks to cover a background
i have this code from a tutorial: // Do the search and show the
I copied this code from an example . I've read it 100 times. Array.prototype.map
I found this code from here: http://www.cssportal.com/form-elements/text-box.htm But the problem is you can still

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.