This post has been edited by maddy123: 16 March 2010  12:49 PM
Prefix equation evaluation program in Visual C++
Page 1 of 13 Replies  4289 Views  Last Post: 16 March 2010  02:17 PM
#1
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++
Replies To: Prefix equation evaluation program in Visual C++
#2
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++.
#3
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.
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.
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.
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.
#4
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:
*+abc2 =>> (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.
Prefix notation would be something like this:
*+abc2 =>> (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.
Page 1 of 1
