summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorgonzo <gonzo@toniatuh.com>2023-05-19 10:44:54 +0200
committergonzo <gonzo@toniatuh.com>2023-05-19 10:44:54 +0200
commitad2a003a77dc234a6eb5628b298630d54b337604 (patch)
treef4a67fe7f1b0af5cceee4c3eaf3e221a13a523d7
parent582542e39f1930c854c73d53744a8d4e369a7cf9 (diff)
downloadloan_calc-ad2a003a77dc234a6eb5628b298630d54b337604.tar.gz
added straight-line loans
-rw-r--r--Makefile2
-rw-r--r--loans.h5
-rw-r--r--straight_line_loan.c29
3 files changed, 35 insertions, 1 deletions
diff --git a/Makefile b/Makefile
index a4b2611..3446aa7 100644
--- a/Makefile
+++ b/Makefile
@@ -18,7 +18,7 @@ HEAD=
OBJ=${DEPS:.c=.o}
LIB=las.a
-LDEPS=bullet_loan.c
+LDEPS=bullet_loan.c straight_line_loan.c
LHEAD=contract.h loans.h
LOBJ=${LDEPS:.c=.o}
diff --git a/loans.h b/loans.h
index 8d99de7..8613028 100644
--- a/loans.h
+++ b/loans.h
@@ -6,3 +6,8 @@
contract *bullet_init(int n, int d, float r, float P);
float bullet_update(contract *);
void bullet_free(contract *c);
+
+/**** STRAIGHT LINE ****/
+contract *sl_init(int n, int d, float r, float P);
+float sl_update(contract *);
+void sl_free(contract *);
diff --git a/straight_line_loan.c b/straight_line_loan.c
new file mode 100644
index 0000000..0d25247
--- /dev/null
+++ b/straight_line_loan.c
@@ -0,0 +1,29 @@
+#include "loans.h"
+
+contract *sl_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;
+ c->D = P/(float)n;
+
+ return c;
+}
+
+float sl_update(contract *c) {
+ if (c->k > c->n)
+ return 0.0f;
+ c->k++;
+ c->I = c->r * c->P;
+ c->P = c->P - c->D;
+ c->A = c->D + c->I;
+
+ return c->A;
+}
+
+void sl_free(contract *c) {
+ free(c);
+}