2
3
4
5
6
7
8
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74int main(
int argc,
char **argv) {
75 double pv = 0, pmt = 0, i = 0, n = 0;
78 signal(SIGINT,
ABEND);
79 signal(SIGQUIT,
ABEND);
80 signal(SIGHUP,
ABEND);
83 ((strcmp(argv[1],
"--help") == 0 || strcmp(argv[1],
"-h") == 0) ||
85 printf(
"Usage: iloan [present_value number_of_payments "
86 "interest_rate payment_amount]\n\n");
91 sscanf(tmp_str,
"%lf", &pv);
92 sscanf(argv[2],
"%lf", &n);
93 sscanf(argv[3],
"%lf", &i);
95 sscanf(tmp_str,
"%lf", &pmt);
106 "Error: At least three values must be greater than zero.");
112 printf(
"Usage: iloan [present_value number_of_payments "
113 "interest_rate payment_amount]\n\n");
117 printf(
"\nInstallment Loan Calculator\n\n");
118 printf(
"Three of these values must be greater than 0. The "
120 printf(
"with a value of 0 will be calculated.\n\n");
144 "be greater than zero.");
147 printf(
"\nYou entered:\n\n");
149 printf(
"Present Value - - - - - -> %s\n",
152 printf(
"Number of Payments - - -> %s\n",
155 printf(
"Interest Rate - - - - - -> %s\n",
158 printf(
"Payment Amount - - - - -> %s\n",
160 printf(
"\n\nCalculation result:\n\n");
179 signal(SIGINT, SIG_DFL);
180 signal(SIGQUIT, SIG_DFL);
181 signal(SIGHUP, SIG_DFL);
184
185
186
187
209
226
248
267
274
278 if (n == 0 || i == 0 || pmt == 0)
280 "3 non-zero values required to calculate Present Value");
282 pv = pmt * (1 - pow(1 + i1, -n)) / i1;
288
291 if (pv == 0 || i == 0 || pmt == 0)
293 "Number of Payments");
295 n = -log(1 - pv * i1 / pmt) / log(1 + i1);
301
304 double delta = 0.0000001;
312 if (pv == 0 || n == 0 || pmt == 0)
314 "3 non-zero values required to calculate Interest Rate");
321 while (xdelta > delta) {
323 fmann = pow(fman, n);
324 fi = ffact * i1 + fmann - 1;
325 fprimi = ffact - n * fmann * fman;
326 fdelta = fi / fprimi;
339
342 if (pv == 0 || n == 0 || i == 0)
344 "3 non-zero values required to calculate Payment Amount");
346 pmt = pv * i1 / (1 - pow(1 + i1, -n));
352
356 while ((c = *s++) !=
'\0' && c !=
'\n')
357 if ((c <
'0' || c >
'9') && c !=
'.' && c !=
',')
362
364 fprintf(stderr,
"%s", s);
368
370 int digit_count, left_of_point;
371 static char sstr[80];
372 static char fstr[80];
373 char *src_ptr, *dst_ptr, *sptr, *FPtr;
375 sprintf(sstr,
"%-18.2f", a);
380 while (*src_ptr++ !=
'\0')
383 left_of_point =
FALSE;
385 while (src_ptr >= sptr) {
387 if (*src_ptr >=
'0' && *src_ptr <=
'9') {
388 if (digit_count == 3) {
394 }
else if (*src_ptr ==
'.')
395 left_of_point =
TRUE;
396 *dst_ptr++ = *src_ptr--;
398 src_ptr = dst_ptr - 1;
400 while (src_ptr >= FPtr)
401 *dst_ptr++ = *src_ptr--;
402 while (*--dst_ptr ==
' ')
408
410 static char sstr[80];
413 sprintf(sstr,
"%-3.5f", a);
418
420 printf(
"ABEND: Error %d:\n", e);
424
427 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)