User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 401,465 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,080 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 Java advertiser: Lunarpages Java Web Hosting
Views: 400 | Replies: 8
Reply
Join Date: May 2008
Posts: 4
Reputation: alannabrittany is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
alannabrittany alannabrittany is offline Offline
Newbie Poster

Can anyone help me write the program for this problem?

  #1  
May 15th, 2008
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
AddThis Social Bookmark Button
Reply With Quote  
Join Date: May 2007
Location: USA
Posts: 2,657
Reputation: Ezzaral is a glorious beacon of light Ezzaral is a glorious beacon of light Ezzaral is a glorious beacon of light Ezzaral is a glorious beacon of light Ezzaral is a glorious beacon of light 
Rep Power: 11
Solved Threads: 262
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Posting Maven

Re: Can anyone help me write the program for this problem?

  #2  
May 15th, 2008
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.
Reply With Quote  
Join Date: May 2008
Posts: 35
Reputation: alpe gulay is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
alpe gulay's Avatar
alpe gulay alpe gulay is offline Offline
Light Poster

Re: Can anyone help me write the program for this problem?

  #3  
May 15th, 2008
...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!
.,'Baby Pro'.,
what you are today!
it's because on what you did yesterday'
what you did today!
is what in future you will be...'
Reply With Quote  
Join Date: Jul 2007
Posts: 30
Reputation: Compton11 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
Compton11 Compton11 is offline Offline
Light Poster

Re: Can anyone help me write the program for this problem?

  #4  
May 15th, 2008
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!
Reply With Quote  
Join Date: May 2008
Posts: 4
Reputation: alannabrittany is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
alannabrittany alannabrittany is offline Offline
Newbie Poster

Re: Can anyone help me write the program for this problem?

  #5  
May 15th, 2008
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 !
Reply With Quote  
Join Date: Jan 2008
Posts: 1,486
Reputation: VernonDozier is a jewel in the rough VernonDozier is a jewel in the rough VernonDozier is a jewel in the rough VernonDozier is a jewel in the rough 
Rep Power: 6
Solved Threads: 186
VernonDozier VernonDozier is offline Offline
Nearly a Posting Virtuoso

Re: Can anyone help me write the program for this problem?

  #6  
May 15th, 2008
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.
Reply With Quote  
Join Date: May 2008
Posts: 35
Reputation: alpe gulay is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
alpe gulay's Avatar
alpe gulay alpe gulay is offline Offline
Light Poster

Re: Can anyone help me write the program for this problem?

  #7  
May 16th, 2008
.,'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
.,'Baby Pro'.,
what you are today!
it's because on what you did yesterday'
what you did today!
is what in future you will be...'
Reply With Quote  
Join Date: Dec 2007
Location: Greece
Posts: 473
Reputation: javaAddict is on a distinguished road 
Rep Power: 1
Solved Threads: 49
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Pro in Training

Re: Can anyone help me write the program for this problem?

  #8  
May 16th, 2008
Originally Posted by alannabrittany View Post
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 – +
I AM the 12th CYLON
Reply With Quote  
Join Date: Dec 2007
Location: Greece
Posts: 473
Reputation: javaAddict is on a distinguished road 
Rep Power: 1
Solved Threads: 49
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Pro in Training

Re: Can anyone help me write the program for this problem?

  #9  
May 16th, 2008
Originally Posted by alpe gulay View Post
.,'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","-","+"}
Last edited by javaAddict : May 16th, 2008 at 5:02 am. Reason: error in index
I AM the 12th CYLON
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Java Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the Java Forum

All times are GMT -4. The time now is 1:33 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC