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

Nope, this isn't a homework service. You need to make an effort on your own first. If you have problems, post your code and the errors or specific questions.

...no effort=no answers!
try to solve it by your own!..
if you see some errors then post it..
maybe we can fixed it out!:)

alannabrittany, what are you having problems with in particular? Have you tried attempting the problem at all? You aren't going to get much help if you don't try it and post code explaining your troubles. Before you start programming, just jot down the main things to be implemented and write pseudo-code for it. Before I tackle any problem, I do those steps first, then I try to program. You will find it easier to program after you get the main idea on how the program will look. Just a little guideline on what you should think of implementing:

1) Look at the input file and see if there are special cases (ex. Are there both unary operators and binary operators?)

2) You will probably have to read in the input file, line by line (as a String of course) and use the charAt() method to extract each of the characters.

3) While extracting each character, test to see if it's an operator (+, - , *, /) or if it's an operand (ex. A number). You can accomplish this by using if statements.

4) You can't do arithmetic operations with Strings, so remember to use the Integer.parseInt(String str) method to convert each number from its String form to its integer representation.

5) Check the file to see if error handling is needed, meaning, are there more operators than operands? I'm assuming all inputs are valid, so this wouldn't be an issue.

These are all the main points I can think of at the moment. If you have any questions, just respond to this post. Hope this helps!

im not exactly sure where to start and i dont really understand it. i dont know if im right, but i tried to understand the question by following the sample inputs. For example, the first sample input, (1 5 9 + 8 – + ), you take leave the 1 alone for now. Next, you take the first operand ( +) and add 5 and 9 together which equals 14. Then you take the next operand (-) and subtract 14 and 8 which is 6. Then you take the last operand the ( +) and add 6 and 1 together which is 7.

let me know if i have the basic idea..


should i use the indexOf() method to call the operands ?

i dont know what to do.. im really confused !

Yes you have the basic idea. Before you code, go through more examples on paper to make sure you understand exactly how post-fixed notation works. Then figure out exactly how to go step by step, again on paper, what needs to be stored, what the order is, when you delete things, when you make an arithmetic calculation, and what constitutes legal post-fixed code. Do this before trying to code.

Write down the steps involved, and from that, decide what functions will be needed. Take extremely simple cases like 4 3 + and get them to work. Decide what you need to store, and from that, how to read in the data. Try to write a program that reads in data and can display answers, even if the answers aren't right. Then try to tackle the rest of the program one piece at a time.

.,'just an idea..

try to read the input by each character..
use charAt() to do this,
while reading put a condition,,
if the character is a number then store it in an array in which you will store all the integer character..,
if the character is an operator then store it in another array in which you will store all your operator,..
take note that the array of your numbers, its not yet an integer value yet it is still consider as char value...
you cant directly convert char to integer.
you must convert first char to string then after it you can convert now to integer...
now, cos you store your data in a an array you can have some operations you want to do into it.,


-just try it...
hope it help you to get some idea

im not exactly sure where to start and i dont really understand it. i dont know if im right, but i tried to understand the question by following the sample inputs. For example, the first sample input, (1 5 9 + 8 – + ), you take leave the 1 alone for now. Next, you take the first operand ( +) and add 5 and 9 together which equals 14. Then you take the next operand (-) and subtract 14 and 8 which is 6. Then you take the last operand the ( +) and add 6 and 1 together which is 7.

let me know if i have the basic idea..


should i use the indexOf() method to call the operands ?

i dont know what to do.. im really confused !

One suggestion. When you take the first operand and do the math then use String.replace() in order to put the result where it was:
1 5 9 + 8 – + -->
1 14 8 – +

.,'just an idea..

try to read the input by each character..
use charAt() to do this,
while reading put a condition,,
if the character is a number then store it in an array in which you will store all the integer character..,
if the character is an operator then store it in another array in which you will store all your operator,..
take note that the array of your numbers, its not yet an integer value yet it is still consider as char value...
you cant directly convert char to integer.
you must convert first char to string then after it you can convert now to integer...
now, cos you store your data in a an array you can have some operations you want to do into it.,


-just try it...
hope it help you to get some idea

And then how would alannabrittany know which number goes to which operand? And you should not store an integer to a char array because if the int is 11 it cannot be turned into the char '11'
If you want to use arrays then use String.split(" "); which will store each number or operand into an array:
String s="1 5 9 + 8 – + ";
will be: {"1","5","9","+","8","-","+"}
Then you can loop, when you find an operand take the previous two: a[i-1]+a[i-2] and then put the result back into the array and continue looping (you will haev to change the valur of i):

if (i is '+') {
 int sum= a[i-1]+a[i-2]
 a[i-2] = sum
 i=i-2;
 //shift the rest of the array to the left:
 loop: a[i+1]=a[i+3]; //overwrite the second number and the operand
}

{"1","5","9","+","8","-","+"} will turn into:
{"1","14","8","-","+"}

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.