Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Calculator Program Question

Options
  • 01-06-2012 10:21am
    #1
    Closed Accounts Posts: 6,075 ✭✭✭


    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


Comments

  • Registered Users Posts: 2,781 ✭✭✭amen


    you should post code examples put this is a classic push/pop from a stack exercise.


  • Registered Users Posts: 9,153 ✭✭✭everdead.ie


    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.


  • Registered Users Posts: 1,456 ✭✭✭FSL


    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.


  • Closed Accounts Posts: 6,075 ✭✭✭IamtheWalrus


    Thanks for the suggestions all. I got it gone.


Advertisement