This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
I am trying to write a program that takes an input of a file formatted as such:
etc, where after number1 there is a sequence of number -- operator. Then the math for doing it should be number 1 (operator 1) number 2 = new number
The problem is that these numbers are far too large to use an int, probably even a float once you get down to many operations including powers. So I would like to use an array of bytes in order to calculate the number correctly.
My problem is that I have absolutely no idea where to start. Can anybody give me any pointers?
Obviously you are supposed to think through the algorithms you are using.
Try an int where each member of the array represents a decimal digit, and maybe a boolean flag for positive and negative. You can go through from right to left adding numbers at different positions and use the / and % operators to obtain the result and the carry. That's adding. Then work out how to do subtracting multiplying and dividing.
Powers are much more difficult; unless they are whole numbers, they use logs and cannot be readily implemented by this method. The quickest way to implement powers is probably to use a recursive method. It isn't difficult, but I can't remember just at the moment how to do it.
Joined: Nov 15, 2007
He went over it a bit for the powers, so I'm not too worried about that - and I would use an int but he's already specified to use a byte. Oh well...