3 Replies - 3739 Views - Last Post: 16 March 2010 - 02:17 PM Rate Topic: -----

#1 maddy123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 16-March 10

Prefix equation evaluation program in Visual C++

Posted 16 March 2010 - 12:14 PM

Hi, need help in developing a program for prefix evaluation using ADT and STL in visual C++

This post has been edited by maddy123: 16 March 2010 - 12:49 PM

Is This A Good Question/Topic? 0
  • +

Replies To: Prefix equation evaluation program in Visual C++

#2 AkselJ  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 2
  • View blog
  • Posts: 20
  • Joined: 07-March 10

Re: Prefix equation evaluation program in Visual C++

Posted 16 March 2010 - 12:43 PM

This should be in C and C++ Forum. There is no such thing as Visual Basic C++, you probably mean Visual C++.
Was This Post Helpful? 0
  • +
  • -

#3 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1639
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Prefix equation evaluation program in Visual C++

Posted 16 March 2010 - 12:44 PM

Moving to C/C++ because, as pointed out, there isn't a Visual Basic C++ language

Sorry but we can not give you the entire code to solve your problem. We are here to assist in solving problems with code you have written, with any error messages you are getting or what isn't working the way that you expect it to, we would be more than happy to help you with those problems. If there is a topic that you don't understand we would be more than happy to help you with that as well. Make sure to post any code with in code tags like below.

:code:

Be sure to visit our tutorial and snippet areas or read the FAQS at the top of the forums. Also, take a moment to read the rules about posting topics.

Thank you for helping us to help you solve your problem.
Was This Post Helpful? 0
  • +
  • -

#4 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Prefix equation evaluation program in Visual C++

Posted 16 March 2010 - 02:17 PM

Well there is not too much to be done here. Just spend a little time thinking about it.

Prefix notation would be something like this:

*+a-bc2 =>> (a + (b - c) * 2)

So to evaluate this we need to construct a "machine" that has 1 "token queue", 1 "operator register", and 2 "operand registers"

Note all a "register" is here is a variable used to temporarily hold some information.

So lets say that our Token Queue is this:


queue: * + 10 - 8 4 2, OpReg: empty, Reg1: empty, Reg2: empty

start by placing grabbing the first token: * and since it is an operator we will place it in the operator register.

queue: + 10 - 8 4 2, OpReg: *, Reg1: empty, Reg2: empty

next look at the next token, it is + and is ALSO an operator, so take the current OpReg and enqueue it, and place this new operator in the register

queue: 10 - 8 4 2 *, OpReg: +, Reg1: empty, Reg2: empty

The next token 10 is a value so we will place it in Reg1 (the first empty register)

queue: - 8 4 2 *, OpReg: +, Reg1: 10, Reg1: empty

The next token - is another operator, so we need to write the contents of the registers to the queue (OpReg, then Reg1, then Reg2 (but it is empty)

queue: 8 4 2 * + 10, OpReg: -, Reg1: empty, Reg1: empty

Read 8 place in Reg1

queue: 4 2 * + 10 4, OpReg: -, Reg1: 8, Reg2: empty

Read 4 place in Reg2 -- And preform current operation, place result in Reg1. enqueue result

queue: 2 * + 10 4, OpReg: empty, Reg1: empty, Reg2: empty

Read 2 and since there are not operations pending, enqueue

queue: * + 10 4 2, OpReg: empty, Reg1: empty, Reg2: empty

read *

queue: * + 10 4 2, OpReg: *, Reg1: empty, Reg2: empty

read + -- enqueue OpReg

queue: 10 4 2 *, OpReg: +, Reg1: empty, Reg2: empty

read 10 and put in Reg1

queue: 4 2 *, OpReg: +, Reg1: 10, Reg2: empty

read: 4 and preform operation, enqueue result

queue: 2 * 14, OpReg: empty, Reg1: empty, Reg2: empty

read 2 and enqueue

queue: * 14 2, OpReg: empty, Reg1: empty, Reg2: empty

Read * and place in OpReg

queue: 14 2, OpReg: *, Reg1: empty, Reg2: empty

Read 14

queue: 2, OpReg: *, Reg1: 14, Reg2: empty

Read 2 and preform operation, enqueue result

queue: 28, OpReg: empty, Reg1: empty, Reg2: empty




so: * + 10 - 8 4 2 == (10 + (8 - 4)) * 2 = 28 tada

This is of course not the only way but it gets the job done pretty easily. it is not hard to figure out the rules needed for the machine to work.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1