|
C-Menu 0.2.9
A User Interface Toolkit
|
Installment Loan Calculator. More...
#include <math.h>#include <signal.h>#include <stdbool.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <unistd.h>Go to the source code of this file.
Macros | |
| #define | FALSE 0 |
| #define | TRUE 1 |
Functions | |
| void | ABEND (int e) |
| The ABEND function is a signal handler that is called when the program receives certain signals (e.g., SIGINT, SIGQUIT, SIGHUP). It takes an integer argument representing the signal number and prints an error message indicating that an abnormal end (ABEND) has occurred, along with the signal number. After displaying the message, it exits the program with a failure status. | |
| double | accept_i () |
| The accept_i function prompts the user to enter the annual interest rate for the loan. It validates the input to ensure it is numeric and non-negative. If the input is invalid, an error message is displayed, and the user is prompted to enter the value again until a valid input is provided. | |
| double | accept_n () |
| The accept_n function prompts the user to enter the number of payments for the loan. It validates the input to ensure it is numeric and non-negative. If the input is invalid, an error message is displayed, and the user is prompted to enter the value again until a valid input is provided. | |
| double | accept_pmt () |
| double | accept_pv () |
| The following functions are used to accept user input for the present value, number of payments, interest rate, and payment amount. They validate the input to ensure it is numeric and meets the required conditions (e.g., non-negative). If the input is invalid, an error message is displayed, and the user is prompted to enter the value again. | |
| void | accept_str (char *s) |
| The accept_str function prompts the user with a given string and reads input from the standard input into a buffer. It uses the fprintf function to display the prompt and the read function to capture the user's input. The input is stored in the global buffer in_str, which can be used by other functions for further processing. | |
| double | calculate_i (double pv, double n, double pmt) |
| The calculate_i function calculates the annual interest rate for a loan based on the present value, number of payments, and payment amount. It uses an iterative method to find the interest rate that satisfies the loan equation. If any of the input values are zero, it displays an error message and prompts the user to provide valid inputs. | |
| double | calculate_n (double pv, double i, double pmt) |
| The calculate_n function calculates the number of payments required to pay off a loan based on the present value, interest rate, and payment amount. It uses logarithmic functions to compute the result. If any of the input values are zero, it displays an error message and prompts the user to provide valid inputs. | |
| double | calculate_pmt (double pv, double n, double i) |
| The calculate_pmt function calculates the payment amount for a loan based on the present value, number of payments, and interest rate. It uses the formula for the payment amount of an annuity to compute the result. If any of the input values are zero, it displays an error message and prompts the user to provide valid inputs. | |
| double | calculate_pv (double n, double i, double pmt) |
| The calculate_pv function calculates the present value of a loan based on the number of payments, interest rate, and payment amount. It uses the formula for the present value of an annuity to compute the result. If any of the input values are zero, it displays an error message and prompts the user to provide valid inputs. | |
| void | error_press_any_key (char *s) |
| The error_press_any_key function is a utility function that displays an error message to the user and waits for them to press any key before continuing. It takes a string argument that contains the error message to be displayed. After the user presses a key, it prints two newlines for formatting. | |
| char * | format_currency (float a) |
| The format_currency function takes a floating-point number as input and formats it as a currency string. It adds commas as thousand separators and ensures that the number is displayed with two decimal places. The function uses a static buffer to store the formatted string and returns a pointer to that buffer. The formatted string is right-aligned and padded with spaces if necessary. | |
| char * | format_interest (float a) |
| The format_interest function takes a floating-point number representing an interest rate and formats it as a string with five decimal places. It uses a static buffer to store the formatted string and returns a pointer to that buffer. The formatted string is left-aligned and padded with spaces if necessary. | |
| int | is_numeric (char *s) |
| The is_numeric function checks if a given string consists of numeric characters, including digits, decimal points, and commas. It iterates through each character in the string and returns FALSE if it encounters any character that is not a digit, a decimal point, or a comma. If all characters are valid, it returns TRUE. | |
| int | main (int argc, char **argv) |
| iloan is a trivial application to demonstrate how a command-line program can be integrated into C-Menu Form with simple file, argument, or pipe i-o. | |
| void | numbers (char *d, char *s) |
| The numbers function takes two character pointers as arguments: a destination pointer (d) and a source pointer (s). It iterates through the characters in the source string (s) and copies only the numeric characters (digits, decimal points, and minus signs) to the destination string (d). The function effectively filters out any non-numeric characters from the source string and constructs a new string containing only the valid numeric characters. Finally, it null-terminates the destination string. | |
Variables | |
| int | f_i = 0 |
| int | f_n = 0 |
| int | f_pmt = 0 |
| int | f_pv = 0 |
| bool | f_quiet = false |
| char | in_str [BUFSIZ+1] |
| void ABEND | ( | int | e | ) |
The ABEND function is a signal handler that is called when the program receives certain signals (e.g., SIGINT, SIGQUIT, SIGHUP). It takes an integer argument representing the signal number and prints an error message indicating that an abnormal end (ABEND) has occurred, along with the signal number. After displaying the message, it exits the program with a failure status.
Definition at line 419 of file iloan.c.
Referenced by main().
| double accept_i | ( | ) |
The accept_i function prompts the user to enter the annual interest rate for the loan. It validates the input to ensure it is numeric and non-negative. If the input is invalid, an error message is displayed, and the user is prompted to enter the value again until a valid input is provided.
Definition at line 227 of file iloan.c.
References accept_str(), error_press_any_key(), in_str, and is_numeric().
Referenced by main().
| double accept_n | ( | ) |
The accept_n function prompts the user to enter the number of payments for the loan. It validates the input to ensure it is numeric and non-negative. If the input is invalid, an error message is displayed, and the user is prompted to enter the value again until a valid input is provided.
Definition at line 210 of file iloan.c.
References accept_str(), error_press_any_key(), in_str, and is_numeric().
Referenced by main().
| double accept_pmt | ( | ) |
brief The accept_pmt function prompts the user to enter the payment amount for the loan. It validates the input to ensure it is numeric and non-negative. If the input is invalid, an error message is displayed, and the user is prompted to enter the value again until a valid input is provided.
Definition at line 249 of file iloan.c.
References accept_str(), error_press_any_key(), in_str, and is_numeric().
Referenced by main().
| double accept_pv | ( | ) |
The following functions are used to accept user input for the present value, number of payments, interest rate, and payment amount. They validate the input to ensure it is numeric and meets the required conditions (e.g., non-negative). If the input is invalid, an error message is displayed, and the user is prompted to enter the value again.
Definition at line 188 of file iloan.c.
References accept_str(), error_press_any_key(), in_str, and is_numeric().
Referenced by main().
| void accept_str | ( | char * | s | ) |
The accept_str function prompts the user with a given string and reads input from the standard input into a buffer. It uses the fprintf function to display the prompt and the read function to capture the user's input. The input is stored in the global buffer in_str, which can be used by other functions for further processing.
Definition at line 363 of file iloan.c.
References in_str.
Referenced by accept_i(), accept_n(), accept_pmt(), and accept_pv().
| double calculate_i | ( | double | pv, |
| double | n, | ||
| double | pmt ) |
The calculate_i function calculates the annual interest rate for a loan based on the present value, number of payments, and payment amount. It uses an iterative method to find the interest rate that satisfies the loan equation. If any of the input values are zero, it displays an error message and prompts the user to provide valid inputs.
Definition at line 302 of file iloan.c.
References error_press_any_key(), f_quiet, and format_interest().
Referenced by main().
| double calculate_n | ( | double | pv, |
| double | i, | ||
| double | pmt ) |
The calculate_n function calculates the number of payments required to pay off a loan based on the present value, interest rate, and payment amount. It uses logarithmic functions to compute the result. If any of the input values are zero, it displays an error message and prompts the user to provide valid inputs.
Definition at line 289 of file iloan.c.
References error_press_any_key(), f_quiet, and format_currency().
Referenced by main().
| double calculate_pmt | ( | double | pv, |
| double | n, | ||
| double | i ) |
The calculate_pmt function calculates the payment amount for a loan based on the present value, number of payments, and interest rate. It uses the formula for the payment amount of an annuity to compute the result. If any of the input values are zero, it displays an error message and prompts the user to provide valid inputs.
Definition at line 340 of file iloan.c.
References error_press_any_key(), f_quiet, and format_currency().
Referenced by main().
| double calculate_pv | ( | double | n, |
| double | i, | ||
| double | pmt ) |
The calculate_pv function calculates the present value of a loan based on the number of payments, interest rate, and payment amount. It uses the formula for the present value of an annuity to compute the result. If any of the input values are zero, it displays an error message and prompts the user to provide valid inputs.
Definition at line 275 of file iloan.c.
References error_press_any_key(), f_quiet, and format_currency().
Referenced by main().
| void error_press_any_key | ( | char * | s | ) |
The error_press_any_key function is a utility function that displays an error message to the user and waits for them to press any key before continuing. It takes a string argument that contains the error message to be displayed. After the user presses a key, it prints two newlines for formatting.
Definition at line 268 of file iloan.c.
Referenced by accept_i(), accept_n(), accept_pmt(), accept_pv(), calculate_i(), calculate_n(), calculate_pmt(), calculate_pv(), and main().
| char * format_currency | ( | float | a | ) |
The format_currency function takes a floating-point number as input and formats it as a currency string. It adds commas as thousand separators and ensures that the number is displayed with two decimal places. The function uses a static buffer to store the formatted string and returns a pointer to that buffer. The formatted string is right-aligned and padded with spaces if necessary.
Definition at line 369 of file iloan.c.
Referenced by calculate_n(), calculate_pmt(), calculate_pv(), and main().
| char * format_interest | ( | float | a | ) |
The format_interest function takes a floating-point number representing an interest rate and formats it as a string with five decimal places. It uses a static buffer to store the formatted string and returns a pointer to that buffer. The formatted string is left-aligned and padded with spaces if necessary.
Definition at line 409 of file iloan.c.
Referenced by calculate_i(), and main().
| int is_numeric | ( | char * | s | ) |
The is_numeric function checks if a given string consists of numeric characters, including digits, decimal points, and commas. It iterates through each character in the string and returns FALSE if it encounters any character that is not a digit, a decimal point, or a comma. If all characters are valid, it returns TRUE.
Definition at line 353 of file iloan.c.
Referenced by accept_i(), accept_n(), accept_pmt(), and accept_pv().
| int main | ( | int | argc, |
| char ** | argv ) |
iloan is a trivial application to demonstrate how a command-line program can be integrated into C-Menu Form with simple file, argument, or pipe i-o.
Positional arguments: present_value The present value of the loan (the amount borrowed). number_of_payments The total number of payments to be made. interest_rate The annual interest rate (as a percentage). payment_amount The amount of each payment. The program calculates the missing value based on the three provided values. For example, if the present value, number of payments, and interest rate are provided, it will calculate the payment amount. If the present value, interest rate, and payment amount are provided, it will calculate the number of payments, and so on. The program can be used in a non-interactive way by passing the four values as arguments, with the value to be calculated set to 0. For example, to calculate the payment amount for a $10,000 loan with a 5% annual interest rate and 60 monthly payments, you could run:
This is proof-of-concept code, and is not intended for production use. It is not designed to be robust, and does not handle all edge cases or input errors. It is intended solely to demonstrate how a simple command-line program can be integrated into C-Menu Form. In the future, more sophisticated abstractions, such as async event handlers, serialization, rpc, database, and a standardized plugin interface will be integrated into C-Menu. Feel free to modify and enhance this code as needed, but please do not use it in production without proper testing and validation. It is provided as-is without any warranty or support. Use at your own risk.
Definition at line 74 of file iloan.c.
References ABEND(), accept_i(), accept_n(), accept_pmt(), accept_pv(), calculate_i(), calculate_n(), calculate_pmt(), calculate_pv(), error_press_any_key(), f_i, f_n, f_pmt, f_pv, f_quiet, format_currency(), format_interest(), and numbers().
| void numbers | ( | char * | d, |
| char * | s ) |
The numbers function takes two character pointers as arguments: a destination pointer (d) and a source pointer (s). It iterates through the characters in the source string (s) and copies only the numeric characters (digits, decimal points, and minus signs) to the destination string (d). The function effectively filters out any non-numeric characters from the source string and constructs a new string containing only the valid numeric characters. Finally, it null-terminates the destination string.
Definition at line 425 of file iloan.c.
Referenced by main().
| bool f_quiet = false |
Definition at line 41 of file iloan.c.
Referenced by calculate_i(), calculate_n(), calculate_pmt(), calculate_pv(), and main().
| char in_str[BUFSIZ+1] |
Definition at line 20 of file iloan.c.
Referenced by accept_i(), accept_n(), accept_pmt(), accept_pv(), and accept_str().