aboutsummaryrefslogtreecommitdiff
path: root/bank.c
blob: 02b45229d94468bad4734a3b7339ced118c1fb88 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <stdint.h>
#include "bignum.h"

char would_overflow(unsigned long value, unsigned long amount) {
	return ((UINT32_MAX - amount) <= value);
}

char bank_withdraw(long amount) {
	bignum(bigamt);

	if(amount < 0) {
		/* can't withdraw all, if too much in bank */
		if(big_cmp(&bank, B_MAXLONG) == 1)
			return 0;
		
		big_copy(&bigamt, &bank);
		big_to_ulong(&bigamt, &amount);
	}

	if(would_overflow(cash, amount)) return 0;

	cash += amount;
	ulong_to_big(&amount, &bigamt);
	big_sub(&bank, &bank, &bigamt);

	return 1;
}

void bank_deposit(long amount) {
	bignum bigamt;

	if(amount < 0) amount = cash;

	cash -= amount;
	ulong_to_big(&amount, &bigamt);
	big_add(&bank, &bank, &bigamt);
}

void bank_interest(void) {
	big_mul(&bank, &bank, &interest_rate);
}