From 582542e39f1930c854c73d53744a8d4e369a7cf9 Mon Sep 17 00:00:00 2001 From: gonzo Date: Fri, 19 May 2023 10:31:51 +0200 Subject: initial commit, contract struct + bullet loan --- Makefile | 45 +++++++++++++++++++++++++++++++++++++++++++++ bullet_loan.c | 31 +++++++++++++++++++++++++++++++ contract.h | 12 ++++++++++++ loans.h | 8 ++++++++ 4 files changed, 96 insertions(+) create mode 100644 Makefile create mode 100644 bullet_loan.c create mode 100644 contract.h create mode 100644 loans.h 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; diff --git a/loans.h b/loans.h new file mode 100644 index 0000000..8d99de7 --- /dev/null +++ b/loans.h @@ -0,0 +1,8 @@ +#pragma once +#include +#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); -- cgit v1.2.3