summaryrefslogtreecommitdiffstats
path: root/main.c
diff options
context:
space:
mode:
authorElouan Gros <elouan.gros@astran.io>2026-09-05 10:06:49 +0200
committergonzo <gonzo@toniatuh.com>2026-09-05 10:30:27 +0200
commit93658108c0951e8b03cdd574ec436c1acb821c7b (patch)
treeab7da3b24923a7b333d8dac901d880bd64ac4e46 /main.c
parentdc7406f717e1381c4ecaefb5277a79a3297eae28 (diff)
downloadloan_calc-93658108c0951e8b03cdd574ec436c1acb821c7b.tar.gz
added columns for running sums + formatting
Diffstat (limited to 'main.c')
-rw-r--r--main.c98
1 files changed, 50 insertions, 48 deletions
diff --git a/main.c b/main.c
index d3b0c62..d716641 100644
--- a/main.c
+++ b/main.c
@@ -1,6 +1,8 @@
-#include <ncurses.h>
#include <form.h>
+#include <limits.h>
+#include <ncurses.h>
#include <signal.h>
+
#include "contract.h"
#include "loans.h"
@@ -9,28 +11,29 @@ static int padscroll, duration;
static WINDOW *pad;
_Noreturn static void finish(int sig);
void display_custom_form(FORM *);
-void print_contract(WINDOW *w, int x, int y, contract *c);
+void print_contract(WINDOW *w, int x, int y, contract *c, double sumI,
+ double sumD);
int run_sim(FORM *, char **contract_subtypes);
int min(int a, int b);
-int main() {
+int main(void) {
int ch, i;
/* setup ncurses */
signal(SIGINT, finish); /* arrange interrupts to terminate */
- initscr(); /* initialize the curses library */
+ initscr(); /* initialize the curses library */
cbreak(); /* take input chars one at a time don't wait for CR */
noecho(); /* echo input */
- nonl(); /* do not LF->CR-LF on output */
+ nonl(); /* do not LF->CR-LF on output */
intrflush(stdscr, FALSE); /* do not flush on interrupts */
- keypad(stdscr, TRUE); /* enable keyboard mapping */
+ keypad(stdscr, TRUE); /* enable keyboard mapping */
getmaxyx(stdscr, HEIGHT, WIDTH);
/* create the form */
FIELD *fields[8];
FORM *form;
- char *contract_types[] = { "loan", "savings", NULL };
- char *contract_subtypes[] = { "bullet", "sla", "mortgage", NULL };
+ char *contract_types[] = {"loan", "savings", NULL};
+ char *contract_subtypes[] = {"bullet", "sla", "mortgage", NULL};
/* initialize fields */
fields[0] = new_field(1, 11, 4, 22, 0, 0); /* contract type */
@@ -51,9 +54,10 @@ int main() {
/* configure fields */
for (i = 0; fields[i]; i++) {
- set_field_back(fields[i], A_UNDERLINE); /* Print a line for the option */
- field_opts_off(fields[i], O_AUTOSKIP); /* Don't go to next field when this */
- /* Field is filled up */
+ /* Print a line for the option */
+ set_field_back(fields[i], A_UNDERLINE);
+ /* Don't go to next field when this field is filled up*/
+ field_opts_off(fields[i], O_AUTOSKIP);
}
/* create form */
@@ -87,8 +91,8 @@ int main() {
form_driver(form, REQ_DEL_PREV);
break;
case '\r':
- if ((E_OK == form_driver(form, REQ_VALIDATION))
- && run_sim(form, contract_subtypes)) {
+ if ((E_OK == form_driver(form, REQ_VALIDATION)) &&
+ run_sim(form, contract_subtypes)) {
unpost_form(form);
formactive = FALSE;
curs_set(0);
@@ -96,51 +100,47 @@ int main() {
padscroll = 0;
mvwprintw(stdscr, 1, 2, "Reinbursement Table");
mvwprintw(stdscr, 3, 3,
- "%4s/%-4s | %12s | %10s | %10s | %10s\n",
- "cur", "tot",
- "principal",
- "interest",
- "part",
+ "%4s/%-4s | %12s | %10s | %10s | %10s\n", "cur",
+ "tot", "principal", "interest", "part",
"annuity");
refresh();
- prefresh(pad, padscroll, 0, 4, 1, HEIGHT-2, WIDTH-1);
+ prefresh(pad, padscroll, 0, 4, 1, HEIGHT - 2, WIDTH - 1);
}
break;
default:
- /* If this is a normal character, it gets */
- /* Printed */
+ /* If this is a normal character, it gets printed */
form_driver(form, ch);
}
} else {
switch (ch) {
case KEY_DOWN:
padscroll += (padscroll < duration) ? 1 : 0;
- pnoutrefresh(pad, padscroll, 0, 4, 1, HEIGHT-2, WIDTH-1);
+ pnoutrefresh(pad, padscroll, 0, 4, 1, HEIGHT - 2, WIDTH - 1);
doupdate();
break;
case KEY_UP:
padscroll -= (padscroll > 0) ? 1 : 0;
- pnoutrefresh(pad, padscroll, 0, 4, 1, HEIGHT-2, WIDTH-1);
+ pnoutrefresh(pad, padscroll, 0, 4, 1, HEIGHT - 2, WIDTH - 1);
doupdate();
break;
case KEY_NPAGE:
padscroll += min(HEIGHT - 6, duration - padscroll);
- pnoutrefresh(pad, padscroll, 0, 4, 1, HEIGHT-2, WIDTH-1);
+ pnoutrefresh(pad, padscroll, 0, 4, 1, HEIGHT - 2, WIDTH - 1);
doupdate();
break;
case KEY_PPAGE:
padscroll -= min(HEIGHT - 6, padscroll);
- pnoutrefresh(pad, padscroll, 0, 4, 1, HEIGHT-2, WIDTH-1);
+ pnoutrefresh(pad, padscroll, 0, 4, 1, HEIGHT - 2, WIDTH - 1);
doupdate();
break;
case KEY_HOME:
padscroll = 0;
- pnoutrefresh(pad, padscroll, 0, 4, 1, HEIGHT-2, WIDTH-1);
+ pnoutrefresh(pad, padscroll, 0, 4, 1, HEIGHT - 2, WIDTH - 1);
doupdate();
break;
case KEY_END:
padscroll = duration;
- pnoutrefresh(pad, padscroll, 0, 4, 1, HEIGHT-2, WIDTH-1);
+ pnoutrefresh(pad, padscroll, 0, 4, 1, HEIGHT - 2, WIDTH - 1);
doupdate();
break;
case 'q':
@@ -148,12 +148,14 @@ int main() {
formactive = TRUE;
curs_set(1);
break;
+ default:;
}
}
}
delwin(pad);
- if (formactive) unpost_form(form);
+ if (formactive)
+ unpost_form(form);
free_form(form);
for (i = 0; fields[i]; i++)
free_field(fields[i]);
@@ -161,7 +163,7 @@ int main() {
finish(0);
}
-_Noreturn static void finish (int sig) {
+_Noreturn static void finish(int sig) {
endwin();
exit(sig);
@@ -177,15 +179,16 @@ void display_custom_form(FORM *form) {
mvprintw(8, 2, "Delay Duration:");
mvprintw(9, 2, "Starting Amount:");
mvprintw(10, 2, "Rate:");
- form_driver(form, REQ_FIRST_FIELD);
+ form_driver(form, REQ_FIRST_FIELD);
}
-void print_contract(WINDOW *w, int x, int y, contract *c) {
+void print_contract(WINDOW *w, int x, int y, contract *c, double sumI,
+ double sumD) {
mvwprintw(w, x, y,
- "%4d/%-4d | %12.2f | %10.2f | %10.2f | %10.2f\n",
- c->k, c->n,
- (double)c->P, (double)c->I,
- (double)c->D, (double)c->A);
+ "%4u/%-4u | %12.2f | %10.2f | %10.2f | %10.2f || %10.2f | "
+ "%10.2f \n",
+ c->k, c->n, (double)c->P, (double)c->I, (double)c->D,
+ (double)c->A, sumI, sumD);
}
int run_sim(FORM *form, char **contract_subtypes) {
@@ -203,20 +206,21 @@ int run_sim(FORM *form, char **contract_subtypes) {
p = atof(field_buffer(fields[5], 0));
r = atof(field_buffer(fields[6], 0));
- if (!n || !yp || (p <= 0.0)) return 0;
+ if (!n || !yp || (p <= 0.0))
+ return 0;
- //adapt wrt yearly payments
+ // adapt wrt yearly payments
if (yp > 1) {
- n = n*yp;
- d = d*yp;
- r = r/(double)yp;
+ n = n * yp;
+ d = d * yp;
+ r = r / (double)yp;
}
/* setup sums */
sumA = sumD = sumI = 0.0;
/* setup pad */
- pad = newpad(n+3, WIDTH-2);
+ pad = newpad(n + 3, WIDTH - 2);
/* run simulation */
loan l = loan_init(lt, n, d, r, p);
@@ -226,18 +230,16 @@ int run_sim(FORM *form, char **contract_subtypes) {
sumI += l.c->I;
sumA += l.c->A;
}
- print_contract(pad, i, 2, l.c);
+ print_contract(pad, i, 2, l.c, sumI, sumD);
loan_update(l);
}
- mvwprintw(pad, n+2, 2,
- " Totals: | %10.2f | %10.2f | %10.2f\n",
- sumI, sumD, sumA);
+ mvwprintw(pad, n + 2, 2,
+ " Totals: | %10.2f | %10.2f | %10.2f\n", sumI,
+ sumD, sumA);
duration = n - HEIGHT + 8;
return 1;
}
-int min(int a, int b) {
- return (a < b) ? a : b;
-}
+int min(int a, int b) { return (a < b) ? a : b; }