Adding this stuff is easy. Just add the number, check to see if any of the values are over what they should be, and if they are, round it up.
Subtraction is more complicated. The steps should be:
1. Subtract the number from pence, as all transaction amounts are in pence
2. If pence < 0, subtract the correct number of shillings and add the related number of pence
3. if shillings is < 0, subtract the correct number of pounds and add the related number of shillings
4. If pounds is <= 0, reset the transaction and say something has gone wrong
This results in the following code:
Code: Select all
if transaction_pence > (pence + (shilling * 12) + (pound * 240)):
jump nosale
$ pence = pence + transaction_pence
if pence <= 0:
$ shilling += (transaction_pence / 12)
$ pence -= ((transaction_pence / 12)+1)*12
if shilling < 0:
$ pound += (transaction_pence / 240)
$ shilling -= ((transaction_pence / 240)+1)*20
1. Subtract 5 pence, (0, 3, -5)
2. Pence is negative, so remove 1 shilling from the shillings
3. add 12 pence to the pence
3. Shillings is positive, so nothing happens
This gives the correct result of 0, 2, 7. (Excuse the lack of currency symbols.)
Instead, the code gives me 0, 2, -5
Code: Select all
if pence <= 0:
3 += (-5 / 12) = 2
-5 - ((-5 / 12)+1)*12
-5 += (-0+1)*12
-5 += 1*12
-5 + 12 = 7
if 2 < 0: it is positive, nothing more to do