Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 04-07-2011, 05:23 PM   #1 (permalink)
Registered Member
 
Join Date: Feb 2011
Posts: 122
architectpianist is on a distinguished road
Question Problems with expression evaluation

Hi, I'm experimenting with an algorithm for simplifying string expressions. I have a postfix expression, and just for testing I entered it in code. It is 123*+4-, derived from the infix 1+2*3-4. However, the simplifying isn't working. I thought it was because of a char to int conversion, but I took care of that and it's working for the first operation it sees, but not for the others. Here's the relevant code:
Code:
//All operands are pushed into the stack in order of scanning
if (c == '+' || c == '-' || c == '*' || c == '/' || c == '^')
		{
			NSLog(@"Operator: %c", c);
			char temp = [[stack lastObject] charValue];
			NSLog(@"Temp should be %@", [stack lastObject]);
			[stack removeLastObject];
			int retVal;
			NSLog(@"Operand 1 (current topStack): %c", [[stack lastObject] charValue]);
			NSLog(@"Operand 2 (temp): %c", temp);
			char last = [[stack lastObject] charValue];
			if (c == '+')
				retVal = (last - 48) + (temp - 48); //Char conversions
			else if (c == '-')
				retVal = (last - 48) - (temp - 48);
			else if (c == '*')
				retVal = (last - 48) * (temp - 48);
			else if (c == '/')
				retVal = (last - 48) / (temp - 48);
			else if (c == '^')
				retVal = pow((temp - 48), (last - 48));
			NSLog(@"RetVal: %i", retVal);
			[stack removeLastObject];
			[stack addObject:[NSNumber numberWithChar:retVal]];
			NSLog(@"%@ added to stack", [stack lastObject]);
			NSLog(@"-------Ended operator %c", c);
		}
The output makes sense for only the first operation:
Code:
1 added to stack
2 added to stack
3 added to stack
Operator: *
Temp should be 51
Operand 1 (current topStack): 2
Operand 2 (temp): 3
RetVal: 6
6 added to stack
-------Ended operator *
Operator: +
Temp should be 6
Operand 1 (current topStack): 1 //Correct
Operand 2 (temp): //???Should be 6 (the last added object to the stack)
RetVal: -41
-41 added to stack
-------Ended operator +
4 added to stack
Operator: -
Temp should be 52
Operand 1 (current topStack): ◊ //???
Operand 2 (temp): 4
RetVal: -93
-93 added to stack
-------Ended operator -
Value: (
    "-93"
)
Thanks for any help, because I don't get this at all.
architectpianist is offline   Reply With Quote
Reply

Bookmarks

Tags
algorithm, char, expression, int, postfix

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 364
13 members and 351 guests
dansparrow, dre, ilmman, LezB44, michelle, Nobbsy, Objective Zero, samdanielblr, Sami Gh, shagor012, sledzeppelin, thephotographer, tinamm64
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,663
Threads: 94,119
Posts: 402,896
Top Poster: BrianSlick (7,990)
Welcome to our newest member, LezB44
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 01:41 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0