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);
269 if (n == 0 || i == 0 || pmt == 0)
271 "3 non-zero values required to calculate Present Value");
273 pv = pmt * (1 - pow(1 + i1, -n)) / i1;
281 if (pv == 0 || i == 0 || pmt == 0)
283 "Number of Payments");
285 n = -log(1 - pv * i1 / pmt) / log(1 + i1);
293 double delta = 0.0000001;
301 if (pv == 0 || n == 0 || pmt == 0)
303 "3 non-zero values required to calculate Interest Rate");
310 while (xdelta > delta) {
312 fmann = pow(fman, n);
313 fi = ffact * i1 + fmann - 1;
314 fprimi = ffact - n * fmann * fman;
315 fdelta = fi / fprimi;
330 if (pv == 0 || n == 0 || i == 0)
332 "3 non-zero values required to calculate Payment Amount");
334 pmt = pv * i1 / (1 - pow(1 + i1, -n));
343 while ((c = *s++) !=
'\0' && c !=
'\n')
344 if ((c <
'0' || c >
'9') && c !=
'.' && c !=
',')
350 fprintf(stderr,
"%s", s);
355 int digit_count, left_of_point;
356 static char sstr[80];
357 static char fstr[80];
358 char *src_ptr, *dst_ptr, *sptr, *FPtr;
360 sprintf(sstr,
"%-18.2f", a);
365 while (*src_ptr++ !=
'\0')
368 left_of_point =
FALSE;
370 while (src_ptr >= sptr) {
372 if (*src_ptr >=
'0' && *src_ptr <=
'9') {
373 if (digit_count == 3) {
379 }
else if (*src_ptr ==
'.')
380 left_of_point =
TRUE;
381 *dst_ptr++ = *src_ptr--;
383 src_ptr = dst_ptr - 1;
385 while (src_ptr >= FPtr)
386 *dst_ptr++ = *src_ptr--;
387 while (*--dst_ptr ==
' ')
394 static char sstr[80];
397 sprintf(sstr,
"%-3.5f", a);
403 printf(
"ABEND: Error %d:\n", e);
409 if (*s ==
'-' || *s ==
'.' || (*s >=
'0' && *s <=
'9'))
double calculate_pmt(double, double, double)
char * format_interest(float)
double calculate_n(double, double, double)
char * format_currency(float)
void numbers(char *d, char *s)
double calculate_i(double, double, double)
double calculate_pv(double, double, double)
void error_press_any_key(char *)
int main(int argc, char **argv)