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

  • Home
  • SEARCH
  • 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 8139995
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T12:00:56+00:00 2026-06-06T12:00:56+00:00

I’m doing the foundation calculator homework from the cs193p course, and my +evaluateExpression:usingVariables: method

  • 0

I’m doing the foundation calculator homework from the cs193p course, and my
+evaluateExpression:usingVariables: method doesn’t work. It always returns 0.

Here’s my method:

+ (double)evaluateExpression: (id)anExpression usingVariablevalues: (NSDictionary *)variables {
CalculatorBrain *worker = [[[CalculatorBrain alloc] init] autorelease];
double returnValue = 10.0;
if ([anExpression isKindOfClass:[NSMutableArray class]]) {
    for (id term in anExpression) {
        NSLog(@"%f", returnValue);         // breakpoint
        if ([term isKindOfClass:[NSString class]]) {                                    // string
            if ([term hasPrefix:@"%"] && [term length] == 2) {                          // variable
                double value = [(NSNumber *)[variables objectForKey:[term substringFromIndex:1]] doubleValue];
                NSLog(@"Variable: %@, value: %d", [term substringFromIndex:1], value);
                [worker setOperand:value];
            }
            else if ([term length] == 1) {                                              // operation
                returnValue = [worker performOperation:term];
            }
            else {
                NSLog(@"Invalid expression.\n\tMultiple character operation found: %@", term);
            }
        }
        else if ([term isKindOfClass:[NSNumber class]]) {                                   // operand
            double value = [term doubleValue];
            [worker setOperand:value];
        }
        else {
            NSLog(@"Invalid expression.\n\tWrong type in expression: %@, The value is: %@.",[term class], term);    // Wrong type in expression
        }

    }
}
else {
    NSLog(@"Invalid expression.\n\tThe expression is of the wrong type: %@.", [anExpression class]);    // expression is of wrong type
}
//returnValue = worker.operand;
return returnValue;
}

Any hints? it always returns 0.

/* Ouput from Console */
2012-06-27 20:01:05.487 Calculator[2823:207] 0
2012-06-27 20:01:05.488 Calculator[2823:207] 0
2012-06-27 20:01:05.489 Calculator[2823:207] 0
2012-06-27 20:01:05.490 Calculator[2823:207] Variable: x, value: 0
2012-06-27 20:01:05.490 Calculator[2823:207] 0
2012-06-27 20:01:05.491 Calculator[2823:207] 0
2012-06-27 20:01:05.491 Calculator[2823:207] 0

Note: I set returnValue to 10.0 at the beginning to check if it is getting changed at all.

Update:

I found out that it gets 0 from the dictionary. I think the calling code is the cause:

- (IBAction) performSampleExpression {
    NSMutableArray *expr = [[NSMutableArray alloc] init]; 
    [expr addObject:[NSNumber numberWithDouble:3.0]];
    [expr addObject:@"+"];
    [expr addObject:@"%x"];
    [expr addObject:@"*"];
    [expr addObject:[NSNumber numberWithDouble:4.0]];

    NSDictionary *varsdict = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithDouble:10.0], @"x", nil];

    double result = [CalculatorBrain evaluateExpression:expr usingVariablevalues:varsdict];
    if (!result) NSLog(@"result = nil");
    NSLog(@"%f", result);

    display.text = [NSString stringWithFormat:@"%f", result];
    [expr release];
}

Result isn’t nil though.

Update 2:

Using the debugger (nice hint. This might solve it.) I found something strange:

On this line: (first time)

returnValue = worker.operand;

It says returnValue is still 10, and not three (worker.operand IS).
Is the assignment failing, or is this how it should be? (Just wondering).

UPDATE 3:

Okay there is something very strange going on here: I set a breakpoint on the return statement, the last line of +evaluateExpression:usingVariables:, and it says
returnValue = 4.
This means, the actual problem lies in -performSampleExpression. What am I doing wrong?

UPDATE 4:

Changing @"%d", double into @"%f", double helped a lot. That explains the strange Console output. But, it solved it, because I updated the display like display [setText:@"%d", result]; what caused my double to be displayed as 0 due to a wrong cast.
I discovered this by using the debugger, so the one who suggested that practically solved it. (And I asked for hints, not for solutions. After all, homework is to learn from).

  • 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-06T12:00:58+00:00Added an answer on June 6, 2026 at 12:00 pm

    The possible issues can shown in:

     returnValue = [worker performOperation:term];
    

    and

    returnValue = worker.operand;
    

    You should stop debugger in this line (or line below) and watch how to your value of returnValue change.

    Screen

    You stop app clicking on position where it is on the screen, app stops, and then jump to next line with F6 (or Fn+F6). Below (in console) you can see the value.

    EDIT

    You could try this solution – Change allocation to:

    CalculatorBrain *worker = [[[CalculatorBrain alloc] init] autorelease];
    

    And delete line:

     [worker release];
    

    This release right before return may couse the problem without NSCopying @protocol.

    Autorelease and NSCopying

    For better understanding compile this code:

    NSMutableArray *arrOne = [[NSMutableArray alloc] initWithObjects:@"1",@"2", nil];
    NSMutableArray *arrTwo = [[NSMutableArray alloc] init];
    
    arrTwo = arrOne;
    [arrOne addObject:@"3"]; //After that arrTwo "shold" be 1,2 and arrOne 1,2,3. No! There both 1,2,3!
    
    for(int i=0;i<[arrTwo count];i++)
        NSLog(@"%@",[arrTwo objectAtIndex:i]);
    

    So you may consider how you assign a variable. In above example releasing memory shouldn’t effect return values, but by using autorelease with return object you don’t have to assign anything.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am currently running into a problem where an element is coming back from
Does anyone know how can I replace this 2 symbol below from the string
I have a bunch of posts stored in text files formatted in yaml/textile (from

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.