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