•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the Computer Science and Software Design section within the Software Development category of DaniWeb, a massive community of 397,581 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,201 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Computer Science and Software Design advertiser:
Views: 633 | Replies: 5
![]() |
•
•
Join Date: May 2008
Posts: 4
Reputation:
Rep Power: 0
Solved Threads: 0
Problem
Math
Input File: MathIn.txt
Output File: MathOut.txt
Project File: Math
Mathematicians on the planet Earth, write math expressions using in-fixed notation. In this notation, math operators are written between the operands they operate on
(e.g., 2 + 3). On Mars, math strings are written in post-fixed form. In this notation, math operators are written after the two operands they operate on (e.g., 2 3 +).
The nice thing about post-fixed notation is that we don’t need rules of precedence to decide what math operator should be evaluated first. For instance, in the in-fixed math string 6 + 4 / 2, the rules of precedence dictate that we should divide before we add. Without these rules, there is an ambiguity in the expression. The same math expression written in post fixed notation is 4 2 / 6 +.
Fortunately programmers who write translators are from Mars, and they translate math expressions from in-fixed to post-fixed notation before evaluating them. Thus, we need not worry about the rules of precedence at run time.
To evaluate a post-fixed string, we start at the left most character and examine characters until we find an operator. Once an operator is found, it is applied to the two operands immediately before it, and then the operand and the two operators in the post-fixed string are replaced with the result. Then we continue from this point, repeating the procedure. When we reach the end of the string, there will only be one item left in the string, the result. Thus the in-fixed string 5 6 2 + 4 / - is equivalent to the post-fixed string
5 - (6 + 2) / 4, both of which evaluate to 3.
Inputs
The input file will contain math strings in post-fixed notation, one per line. Operands will consist of one digit. Operators and operands will be separated by one space. There will be no more than 80 characters in the math expressions.
Outputs
There will be one line of output for each math expression. The line will contain the value of the math expression.
Sample input
1 5 9 + 8 – +
5 6 2 + 4 / -
4 7 9 8 * + 2 + -
Sample Output
7
3
77
Math
Input File: MathIn.txt
Output File: MathOut.txt
Project File: Math
Mathematicians on the planet Earth, write math expressions using in-fixed notation. In this notation, math operators are written between the operands they operate on
(e.g., 2 + 3). On Mars, math strings are written in post-fixed form. In this notation, math operators are written after the two operands they operate on (e.g., 2 3 +).
The nice thing about post-fixed notation is that we don’t need rules of precedence to decide what math operator should be evaluated first. For instance, in the in-fixed math string 6 + 4 / 2, the rules of precedence dictate that we should divide before we add. Without these rules, there is an ambiguity in the expression. The same math expression written in post fixed notation is 4 2 / 6 +.
Fortunately programmers who write translators are from Mars, and they translate math expressions from in-fixed to post-fixed notation before evaluating them. Thus, we need not worry about the rules of precedence at run time.
To evaluate a post-fixed string, we start at the left most character and examine characters until we find an operator. Once an operator is found, it is applied to the two operands immediately before it, and then the operand and the two operators in the post-fixed string are replaced with the result. Then we continue from this point, repeating the procedure. When we reach the end of the string, there will only be one item left in the string, the result. Thus the in-fixed string 5 6 2 + 4 / - is equivalent to the post-fixed string
5 - (6 + 2) / 4, both of which evaluate to 3.
Inputs
The input file will contain math strings in post-fixed notation, one per line. Operands will consist of one digit. Operators and operands will be separated by one space. There will be no more than 80 characters in the math expressions.
Outputs
There will be one line of output for each math expression. The line will contain the value of the math expression.
Sample input
1 5 9 + 8 – +
5 6 2 + 4 / -
4 7 9 8 * + 2 + -
Sample Output
7
3
77
•
•
Join Date: Oct 2007
Location: Cambridge, MA
Posts: 241
Reputation:
Rep Power: 1
Solved Threads: 20
What do you mean "what do I need to do"? You need to write a program that meets the specifications. For example:
perl -pe '1 while (s^(-?[.\d]+) (-?[.\d]+) ([-+*/])(?!\d)^"$1 $3 $2"^ee)'
•
•
Join Date: May 2008
Posts: 4
Reputation:
Rep Power: 0
Solved Threads: 0
•
•
•
•
What do you mean "what do I need to do"? You need to write a program that meets the specifications. For example:
perl -pe '1 while (s^(-?[.\d]+) (-?[.\d]+) ([-+*/])(?!\d)^"$1 $3 $2"^ee)'
i mean what should i write for the code in java
Last edited by alannabrittany : May 14th, 2008 at 10:26 pm.
Well at last you've posted what you're going to use to implement it.
Post your attempt in this forum.
http://www.daniweb.com/forums/forum9.html
Evaluating RPN expressions typically uses a stack and goes like this
- see a number, push it onto the stack
- see an operator, pop the required number of operands off the stack, evaluate and push the result.
Repeat until end of input.
Post your attempt in this forum.
http://www.daniweb.com/forums/forum9.html
Evaluating RPN expressions typically uses a stack and goes like this
- see a number, push it onto the stack
- see an operator, pop the required number of operands off the stack, evaluate and push the result.
Repeat until end of input.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
Do not PM me for help; You'll be ignored, or told to learn to read.
Do not ask me if I'm muslim - I'm not. Nor do I care about yours or anyone else's mysticism. Religion is a matrix, take the RED PILL.
Do not PM me for help; You'll be ignored, or told to learn to read.
Do not ask me if I'm muslim - I'm not. Nor do I care about yours or anyone else's mysticism. Religion is a matrix, take the RED PILL.
•
•
Join Date: Nov 2004
Location: Netherlands
Posts: 5,660
Reputation:
Rep Power: 18
Solved Threads: 192
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb Computer Science and Software Design Marketplace
Other Threads in the Computer Science and Software Design Forum
- Previous Thread: Where can I find info about damn-low level architecture?
- Next Thread: Algorithm Help please!



Linear Mode