2
3
4
5
6
7
8
18#ifdef __USE_GNU_GETTEXT
20#define _(String) gettext(String)
21#define gettext_noop(String) String
22#define N_(String) gettext_noop(String)
24#define _(String) (String)
25#define N_(String) String
26#define textdomain(Domain)
27#define bindtextdomain(Package, Directory)
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86int main(
int argc,
char **argv) {
87 double pv = 0, pmt = 0, i = 0, n = 0;
90 signal(SIGINT,
ABEND);
91 signal(SIGQUIT,
ABEND);
92 signal(SIGHUP,
ABEND);
95 ((strcmp(argv[1],
_(
"--help")) == 0 || strcmp(argv[1],
"-h") == 0) ||
97 printf(
_(
"Usage: iloan [present_value number_of_payments interest_rate payment_amount]\n\n"));
102 sscanf(tmp_str,
"%lf", &pv);
103 sscanf(argv[2],
"%lf", &n);
104 sscanf(argv[3],
"%lf", &i);
106 sscanf(tmp_str,
"%lf", &pmt);
117 _(
"Error: At least three values must be greater than zero."));
123 printf(
_(
"Usage: iloan [present_value number_of_payments interest_rate payment_amount]\n\n"));
127 printf(
_(
"\nInstallment Loan Calculator\n\n"));
128 printf(
_(
"Three of these values must be greater than 0. The "
130 printf(
_(
"with a value of 0 will be calculated.\n\n"));
154 "be greater than zero."));
157 printf(
"\nYou entered:\n\n");
159 printf(
_(
"Present Value - - - - - -> %s\n"),
162 printf(
_(
"Number of Payments - - -> %s\n"),
165 printf(
_(
"Interest Rate - - - - - -> %s\n"),
168 printf(
_(
"Payment Amount - - - - -> %s\n"),
170 printf(
_(
"\n\nCalculation result:\n\n"));
189 signal(SIGINT, SIG_DFL);
190 signal(SIGQUIT, SIG_DFL);
191 signal(SIGHUP, SIG_DFL);
194
195
196
197
219
236
258
277
284
288 if (n == 0 || i == 0 || pmt == 0)
290 _(
"3 non-zero values required to calculate Present Value"));
292 pv = pmt * (1 - pow(1 + i1, -n)) / i1;
298
301 if (pv == 0 || i == 0 || pmt == 0)
303 "Number of Payments"));
305 n = -log(1 - pv * i1 / pmt) / log(1 + i1);
311
314 double delta = 0.0000001;
322 if (pv == 0 || n == 0 || pmt == 0)
324 _(
"3 non-zero values required to calculate Interest Rate"));
331 while (xdelta > delta) {
333 fmann = pow(fman, n);
334 fi = ffact * i1 + fmann - 1;
335 fprimi = ffact - n * fmann * fman;
336 fdelta = fi / fprimi;
349
352 if (pv == 0 || n == 0 || i == 0)
354 _(
"3 non-zero values required to calculate Payment Amount"));
356 pmt = pv * i1 / (1 - pow(1 + i1, -n));
362
366 while ((c = *s++) !=
'\0' && c !=
'\n')
367 if ((c <
'0' || c >
'9') && c !=
'.' && c !=
',')
372
374 fprintf(stderr,
"%s", s);
378
380 int digit_count, left_of_point;
381 static char sstr[80];
382 static char fstr[80];
383 char *src_ptr, *dst_ptr, *sptr, *FPtr;
385 sprintf(sstr,
"%-18.2f", a);
390 while (*src_ptr++ !=
'\0')
393 left_of_point =
FALSE;
395 while (src_ptr >= sptr) {
397 if (*src_ptr >=
'0' && *src_ptr <=
'9') {
398 if (digit_count == 3) {
404 }
else if (*src_ptr ==
'.')
405 left_of_point =
TRUE;
406 *dst_ptr++ = *src_ptr--;
408 src_ptr = dst_ptr - 1;
410 while (src_ptr >= FPtr)
411 *dst_ptr++ = *src_ptr--;
412 while (*--dst_ptr ==
' ')
418
420 static char sstr[80];
423 sprintf(sstr,
"%-3.5f", a);
428
430 printf(
_(
"ABEND: Error %d:\n"), e);
434
437 if (*s ==
'-' || *s ==
'.' || (*s >=
'0' && *s <=
'9'))
double accept_n()
The accept_n function prompts the user to enter the number of payments for the loan....
double calculate_i(double, double, double)
The calculate_i function calculates the annual interest rate for a loan based on the present value,...
void numbers(char *d, char *s)
The numbers function takes two character pointers as arguments: a destination pointer (d) and a sourc...
double accept_pv()
The following functions are used to accept user input for the present value, number of payments,...
void ABEND(int)
The ABEND function is a signal handler that is called when the program receives certain signals (e....
double calculate_pmt(double, double, double)
The calculate_pmt function calculates the payment amount for a loan based on the present value,...
void error_press_any_key(char *)
The error_press_any_key function is a utility function that displays an error message to the user and...
void accept_str(char *s)
The accept_str function prompts the user with a given string and reads input from the standard input ...
double calculate_pv(double, double, double)
The calculate_pv function calculates the present value of a loan based on the number of payments,...
char * format_interest(float)
The format_interest function takes a floating-point number representing an interest rate and formats ...
double accept_i()
The accept_i function prompts the user to enter the annual interest rate for the loan....
int is_numeric(char *)
The is_numeric function checks if a given string consists of numeric characters, including digits,...
char * format_currency(float)
The format_currency function takes a floating-point number as input and formats it as a currency stri...
double calculate_n(double, double, double)
The calculate_n function calculates the number of payments required to pay off a loan based on the pr...
int main(int argc, char **argv)
Capture the current terminal settings for later restoration.