Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

prefix/postfix evaluation in C / C++?

This topic is 5219 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, anyone know how to evaluate prefix (or postfix)expressions? I finally found a tutorial which teach people how to convert infix->prefix, infix->postfix... in a string. But then now I need to evaluate them... how? I need one which can evaluate long expresion... (or atleast not just 2 var and 1 op... i kinda fig out how to evaluate 2 var, but i need to evaluate more than 2 var and 1 op...) Thanks for helping~ ( sorry for being rude, but i need the tutorial badly... ) [edited by - NewbieA on February 6, 2004 10:03:20 AM]

Share this post


Link to post
Share on other sites
Advertisement
i am not sure what you are asking....but are you asking for trying to figure out something like

2 + 3 + 4

let the user type something in like that and then the computer returns the answer ?

it is really just an extension of just adding two numbers together which you said you have figure out already

parse all that out into a buffer like


int main()
char buf[10][8]; // just some numbers for the size
// i know you cant do this but this is pseudo code
buf[0] = "2";
buf[1] = "+";
buf[2] = "3";
buf[3] = "+";
buf[4] = "4";
buf[5] = NULL;

int left = atoi(buf[0]);
if (buf[1] == "+")
answer = addNumbers(left, 2);
cout << answer;
}

//////////////////////////////////////////////////
int addNumbers(int left_value, int next_buf_index)
{

int right_value = atoi(buff[next_buff_index]);
next_buff_index++;
int ans = left_value + right_value;
// at end of user entered input
if (buff[next_buff_index] == NULL)
return ans;

return addNumbers(ans, next_buff_index);

}


it is recursion basically...hope you know what that is
you of course need to add some if statements to detect if it is other operations besides just adding

"A soldier is a part of the 1% of the population that keeps the other 99% free" - Lt. Colonel Todd, 1/38th Infantry, Ft. Benning, GA

Share this post


Link to post
Share on other sites
Yes, that's what i meant, ncsu121978.
(Sorry if i start to talk strangly, coz i've been trying to fig out how to do this the whole day... )

user input : 2 + 3 + 4
and prefix is : + + 2 3 4 ..,right?
i reach this far... ( + + 2 3 4, in char[] ), but then...@_@ is what i couldn't fig out...


Thanks ncsu121978,
Thanks

[edited by - NewbieA on February 6, 2004 10:22:00 AM]

Share this post


Link to post
Share on other sites
well then once you get those prefixes parsed into your buffer like you have them
then rearrange them to fall between the numbers in the buffer and then use my method

or you could use some kind of queue and place the operators in the queue. now you know to use the first operator in the queue on the first two numbers

the next operator in the queue on that answer and the third number

the next operator in the queue on that answer and the fourth number....

wash, rinse, repeat

and what does @_@ mean ?

"A soldier is a part of the 1% of the population that keeps the other 99% free" - Lt. Colonel Todd, 1/38th Infantry, Ft. Benning, GA

[edited by - ncsu121978 on February 6, 2004 10:29:38 AM]

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!