diff options
| author | gonzo <gonzo@toniatuh.com> | 2023-05-19 10:31:51 +0200 |
|---|---|---|
| committer | gonzo <gonzo@toniatuh.com> | 2023-05-19 10:31:51 +0200 |
| commit | 582542e39f1930c854c73d53744a8d4e369a7cf9 (patch) | |
| tree | fd784c0b88d457605e536097c30c3fbbf0f3758f | |
| download | loan_calc-582542e39f1930c854c73d53744a8d4e369a7cf9.tar.gz | |
initial commit, contract struct + bullet loan
| -rw-r--r-- | Makefile | 45 | ||||
| -rw-r--r-- | bullet_loan.c | 31 | ||||
| -rw-r--r-- | contract.h | 12 | ||||
| -rw-r--r-- | loans.h | 8 |
4 files changed, 96 insertions, 0 deletions
diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..a4b2611 --- /dev/null +++ b/Makefile @@ -0,0 +1,45 @@ +UNAME := $$( uname -s ) + +.if ${UNAME} == "Linux" +CC=clang --std=gnu99 +.else +CC=clang --std=c99 +.endif +AR=ar ruvcs + +CFLAGS=-Wall -Wextra -Werror -Weverything -Wpadded -pedantic +LIBS=-lncurses +BLDFLAGS=-g +RELFLAGS=-O2 + +TARGET=lc +DEPS=main.c +HEAD= +OBJ=${DEPS:.c=.o} + +LIB=las.a +LDEPS=bullet_loan.c +LHEAD=contract.h loans.h +LOBJ=${LDEPS:.c=.o} + +.PHONY: all clean release $(TARGET) $(LIB) + +all: $(LIB) $(TARGET) + +$(OBJ): $(HEAD) +$(LOBJ): $(LHEAD) + +%.o: %.c + $(CC) -o ./src/$@ -c $< $(CFLAGS) + +$(TARGET): $(OBJ) $(LIB) + $(CC) -o $@ $(OBJ) $(LIB) $(LIBS) $(BLDFLAGS) + +$(LIB): $(LOBJ) + $(AR) $(LIB) $(LOBJ) + +release: $(OBJ) + $(CC) -o $(TARGET) $(OBJ) $(LIBS) $(RELFLAGS) + +clean: + rm -f *.o *~ *.core $(OBJ) $(TARGET) diff --git a/bullet_loan.c b/bullet_loan.c new file mode 100644 index 0000000..189ca05 --- /dev/null +++ b/bullet_loan.c @@ -0,0 +1,31 @@ +#include "loans.h" + +contract *bullet_init(int n, int d, float r, float P) { + if ((n < 0)) return NULL; + if ((d > n)||(d < 0)) return NULL; + contract *c = (contract *)calloc(1, sizeof(contract)); + c->n = (unsigned int)n; + c->d = (unsigned int)d; + c->r = r; + c->P = P; + + return c; +} + +float bullet_update(contract *c) { + if (c->k > c->n) + return 0.0f; + c->k++; + c->I = c->r * c->P; + if (c->k == c->n) { + c->D = c->P; + c->P = 0.0f; + } + c->A = c->D + c->I; + + return c->A; +} + +void bullet_free(contract *c) { + free(c); +} diff --git a/contract.h b/contract.h new file mode 100644 index 0000000..4c74cb0 --- /dev/null +++ b/contract.h @@ -0,0 +1,12 @@ +#pragma once + +typedef struct contract { + unsigned int n; /* duration of the loan */ + unsigned int k; /* current year */ + unsigned int d; /* delay period of the loan */ + float r; /* interest rate */ + float P; /* part of the pricipal left at year k */ + float I; /* the interest for year k */ + float D; /* part of the capital reinbursed on year k */ + float A; /* annuity for year k */ +} contract; @@ -0,0 +1,8 @@ +#pragma once +#include <stdlib.h> +#include "contract.h" + +/**** BULLET LOAN ****/ +contract *bullet_init(int n, int d, float r, float P); +float bullet_update(contract *); +void bullet_free(contract *c); |
