Boards.ie uses cookies. By continuing to browse this site you are agreeing to our use of cookies. Click here to find out more x
Post Reply  
 
Thread Tools Search this Thread
01-06-2012, 10:21   #1
IamtheWalrus
Registered User
 
Join Date: Jul 2008
Posts: 1,795
Calculator Program Question

Hi all,

I have a program I need to write and was wondering if anyone could give me some pointers. I have written the a lot of the program but need hints on an algorithm because I'm stuck. I am using Java.

I had thought of traversing the file, storing the instruction and number in a 2D array. The array ends when I reach 'Apply'. Does tat sound like a good idea?

Write some code to calculate a result from a set of instructions. Instructions comprise of a keyword and a number that are separated by a space per line. Instructions are loaded from file and results are output to the screen. Any number of Instructions can be specified. Instructions can be any binary operators of your choice (e.g., add, divide, subtract, multiply etc). The instructions will ignore mathematical precedence. The last instruction should be “apply” and a number (e.g., “apply 3”). The calculator is then initialised with that number and the previous instructions are applied to that number.

 Example:

 [Input from file]
add 2
multiply 3
apply 3

[Output to screen]
15

[Explanation]
(3 + 2) * 3 = 15

Any suggestions are welcome.

Walrus
IamtheWalrus is offline  
Advertisement
01-06-2012, 13:20   #2
amen
Registered User
 
Join Date: Oct 1999
Location: Galway
Posts: 2,181
you should post code examples put this is a classic push/pop from a stack exercise.
amen is offline  
01-06-2012, 16:46   #3
everdead.ie
Registered User
 
Join Date: Feb 2008
Posts: 8,648
Quote:
Originally Posted by IamtheWalrus View Post
Hi all,

I have a program I need to write and was wondering if anyone could give me some pointers. I have written the a lot of the program but need hints on an algorithm because I'm stuck. I am using Java.

I had thought of traversing the file, storing the instruction and number in a 2D array. The array ends when I reach 'Apply'. Does tat sound like a good idea?

Write some code to calculate a result from a set of instructions. Instructions comprise of a keyword and a number that are separated by a space per line. Instructions are loaded from file and results are output to the screen. Any number of Instructions can be specified. Instructions can be any binary operators of your choice (e.g., add, divide, subtract, multiply etc). The instructions will ignore mathematical precedence. The last instruction should be “apply” and a number (e.g., “apply 3”). The calculator is then initialised with that number and the previous instructions are applied to that number.

Example:

[Input from file]
add 2
multiply 3
apply 3

[Output to screen]
15

[Explanation]
(3 + 2) * 3 = 15

Any suggestions are welcome.

Walrus
If you are going to use an array and then traverse it I would recommend using a loop to fill it until it reaches apply and then use the following number to initialise your value.

Then when traversing your array you just keep going until the next value in the array is null.
everdead.ie is offline  
01-06-2012, 21:04   #4
FSL
Registered User
 
Join Date: Feb 2006
Posts: 991
Should the answer not be 11. The instructions say the calculator is initialised with the number following the apply viz 3 the next instruction back is multiply and the number is 3 so 3 x 3 = 9 the next ( and last) instruction back is add and the number is 2 so 9 + 2 = 11 output should be 11.

Your example initialises the calculator with 2 + (the first instruction), then adds 3 x (the next instruction giving) 5 x and finally applies the 3 to the 5 x giving 15.

Looking at it again it could be 15 if you initialise with 3 add 2 and multiply by 3. That is initialise with the last number and then perform the steps from the first going forward.

Last edited by FSL; 01-06-2012 at 21:11.
FSL is offline  
03-06-2012, 22:34   #5
IamtheWalrus
Registered User
 
Join Date: Jul 2008
Posts: 1,795
Thanks for the suggestions all. I got it gone.
IamtheWalrus is offline  
Post Reply

Quick Reply
Message:
Remove Text Formatting
Bold
Italic
Underline

Insert Image
Wrap [QUOTE] tags around selected text
 
Decrease Size
Increase Size
Please sign up or log in to join the discussion

Thread Tools Search this Thread
Search this Thread:

Advanced Search