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 > Mac OS X Development Forums > Objective-C, Python, Ruby Development

Reply
 
LinkBack Thread Tools Display Modes
Old 07-12-2011, 05:15 PM   #1 (permalink)
Registered Member
 
Join Date: Apr 2010
Posts: 20
chris2810 is on a distinguished road
Default Building an Expression Tree

Hi,

i have a problem.
I tried a lot of things, but i canīt figure it out.

Maybe someone can help.

I want to build an expression tree from a math expression in objective-c.

I have pseudo code, but i canīt do it in Objective-C.

Here is the pseudo code.



PopConnectPush
{
pop the top node off the operator stack and call it N;
pop the top node off the tree stack and make it N's right child;
pop the top node off the tree stack and make it N's left child;
push N back into the tree stack;
}


Convert Expression to Tree
initialize operator and tree stacks;
while (there are tokens remaining in the expression)
{
T = next token from expression;
if (T == '(')
{
create a node and store T in it;
push the node onto the operator stack;
}
else if (T is a variable or numeric literal)
{
create a node and store T in it;
push the node onto the tree stack;
}
else if (T is '+', '-', '+', or '/')
{
create a node and store T in it;
if ((operator stack is empty) or
(the value at the top of the operator stack is '(') or
(priority(operator at top of stack) < priority(T)))
{
push the node onto the operator stack;
}
else // clear operator stack and push new one onto it
{
do
{
PopConnectPush;
}
while ((the operator stack is not empty) and
(the top of the operator stack is not '(') and
(priority(T) < priority(operator at top of stack)));

create a node and store T in it;
push the node onto operator stack;
}
else if (T is ')') // clear operator stack back to the '('
{
while (top of operator stack is not '(')
{
PopConnectPush;
}
}
else
{
report error!
}
}

// no more tokens left in expression
while (operator stack is not empty)
{
PopConnectPush;
}

// pointer to root of final tree is on top of the tree stack
}

Last edited by chris2810; 07-13-2011 at 06:24 AM.
chris2810 is offline   Reply With Quote
Reply

Bookmarks

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: 476
16 members and 460 guests
7twenty7, AlanFloyd, AppsBlogger, David-T, HemiMG, iAppDeveloper, imac74, Jaxen66, lovoyl, Music Man, mutantskin, Paul Slocum, SLIC, solardrift, unicornleo, usernametaken
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,683
Threads: 94,131
Posts: 402,932
Top Poster: BrianSlick (7,990)
Welcome to our newest member, unicornleo
Powered by vBadvanced CMPS v3.1.0

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