Compound Interest |
|
|
#define IDD_MAIN_DLG 101 #define IDC_PRINCIPAL 1001 #define IDC_ANNUAL_RATE 1002 #define IDC_NBR_OF_PERIODS 1003 #define IDC_COMPOUND 1004 #define IDC_QUATERLY 1005 #define IDC_SEMIANNUALLY 1006 #define IDC_RADIO4 1007 #define IDC_ANNUALLY 1007 #define IDC_CALCULATE_BTN 1008 #define IDC_INTEREST_EARNED 1009 #define IDC_AMOUNT_EARNED 1010 |
#include "resource.h" //--------------------------------------------------------------------------- // Dialog IDD_MAIN_DLG DIALOGEX 200, 100, 329, 132 STYLE DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | WS_POPUP | WS_CAPTION | WS_SYSMENU CAPTION "Compound Interest" FONT 8, "MS Shell Dlg" // , 400, 0, 0x1 BEGIN GROUPBOX "Preparation",IDC_STATIC,6,6,156,72 LTEXT "Principal: ...............",IDC_STATIC,18,21,72,8 EDITTEXT IDC_PRINCIPAL,90,18,40,12,ES_RIGHT | ES_AUTOHSCROLL LTEXT "Interest: ...............",IDC_STATIC,18,40,72,8 EDITTEXT IDC_ANNUAL_RATE,90,36,40,12,ES_RIGHT | ES_AUTOHSCROLL LTEXT "Number of Periods: ...",IDC_STATIC,18,58,72,8 EDITTEXT IDC_NBR_OF_PERIODS,90,54,40,12,ES_RIGHT | ES_AUTOHSCROLL LTEXT "%",IDC_STATIC,132,42,8,8 LTEXT "years",IDC_STATIC,132,54,19,8 GROUPBOX "Compound Frequency",IDC_STATIC,168,6,90,72 CONTROL "Monthly",IDC_COMPOUND,"Button",BS_AUTORADIOBUTTON | BS_LEFTTEXT | WS_GROUP,180,20,60,10 PUSHBUTTON "Calculate",IDC_CALCULATE_BTN,270,9,50,14 PUSHBUTTON "Close",IDCANCEL,270,30,50,14 CONTROL "Quarterly",IDC_QUATERLY,"Button",BS_AUTORADIOBUTTON | BS_LEFTTEXT,180,33,60,10 CONTROL "Semiannually",IDC_SEMIANNUALLY,"Button", BS_AUTORADIOBUTTON | BS_LEFTTEXT,180,46,60,10 CONTROL "Annually",IDC_ANNUALLY,"Button",BS_AUTORADIOBUTTON | BS_LEFTTEXT,180,59,60,10 GROUPBOX "Results",IDC_STATIC,6,84,252,36 LTEXT "Interest Earned:",IDC_STATIC,15,99,54,8 EDITTEXT IDC_INTEREST_EARNED,78,96,52,12,ES_RIGHT | ES_AUTOHSCROLL LTEXT "Amount Earned:",IDC_STATIC,144,99,53,8 EDITTEXT IDC_AMOUNT_EARNED,198,96,48,12,ES_RIGHT | ES_AUTOHSCROLL END //--------------------------------------------------------------------------- |
#include <windows.h> #include <cstdio> #include <cmath> #include "Resource.h" //--------------------------------------------------------------------------- HWND hWnd; LRESULT CALLBACK DlgProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam); //--------------------------------------------------------------------------- INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { DialogBox(hInstance, MAKEINTRESOURCE(IDD_MAIN_DLG), hWnd, reinterpret_cast<DLGPROC>(DlgProc)); return FALSE; } //--------------------------------------------------------------------------- LRESULT CALLBACK DlgProc(HWND hWndDlg, UINT Msg, WPARAM wParam, LPARAM lParam) { // These variables will carry the values in the text boxes LPTSTR strPrincipal = new char[20], strInterest = new char[20], strPeriods = new char[20], strInterestEarned = new char[20], strAmountEarned = new char[20]; // These are handled for the various controls HWND hWndPrincipal, hWndInterest, hWndPeriods, hWndCompound, hWndInterestEarned, hWndAmountEarned, hWndCalculate; double Principal, AnnualRate, InterestEarned; double FutureValue, RatePerPeriod; int NumberOfPeriods, CompoundType; double i; int n; hWndPrincipal = GetDlgItem(hWndDlg, IDC_PRINCIPAL); hWndInterest = GetDlgItem(hWndDlg, IDC_ANNUAL_RATE); hWndPeriods = GetDlgItem(hWndDlg, IDC_NBR_OF_PERIODS); hWndCompound = GetDlgItem(hWndDlg, IDC_COMPOUND); hWndInterestEarned = GetDlgItem(hWndDlg, IDC_INTEREST_EARNED); hWndAmountEarned = GetDlgItem(hWndDlg, IDC_AMOUNT_EARNED); hWndCalculate = GetDlgItem(hWndDlg, IDC_CALCULATE_BTN); switch(Msg) { case WM_INITDIALOG: // Identify each control SetWindowText(hWndPrincipal, "0.00"); SetWindowText(hWndInterest, "7.55"); SetWindowText(hWndPeriods, "0"); SetWindowText(hWndInterestEarned, "0.00"); SetWindowText(hWndAmountEarned, "0.00"); CheckRadioButton(hWndDlg, IDC_COMPOUND, IDC_ANNUALLY, IDC_COMPOUND); return TRUE; case WM_COMMAND: switch(wParam) { case IDC_CALCULATE_BTN: GetWindowText(hWndPrincipal, strPrincipal, 20); GetWindowText(hWndInterest, strInterest, 10); GetWindowText(hWndPeriods, strPeriods, 8); Principal = atof(strPrincipal); AnnualRate = atof(strInterest) / 100; if( IsDlgButtonChecked(hWndDlg, IDC_COMPOUND) == BST_CHECKED ) CompoundType = 12; else if( IsDlgButtonChecked(hWndDlg, IDC_QUATERLY) == BST_CHECKED ) CompoundType = 4; else if( IsDlgButtonChecked(hWndDlg, IDC_SEMIANNUALLY) == BST_CHECKED ) CompoundType = 2; else // if( IsDlgButtonChecked(hWndDlg, IDC_ANNUALLY) == BST_CHECKED ) CompoundType = 1; NumberOfPeriods = atoi(strPeriods); i = AnnualRate / CompoundType; n = CompoundType * NumberOfPeriods; RatePerPeriod = AnnualRate / NumberOfPeriods; FutureValue = Principal * pow(1 + i, n); InterestEarned = FutureValue - Principal; sprintf(strInterestEarned, "$%.2f", InterestEarned); sprintf(strAmountEarned, "$%.2f", FutureValue); SetWindowText(hWndInterestEarned, strInterestEarned); SetWindowText(hWndAmountEarned, strAmountEarned); return TRUE; case IDCANCEL: EndDialog(hWndDlg, 0); return TRUE; } break; } return FALSE; } //--------------------------------------------------------------------------- |
|
||
Home | Copyright © 2004-2014 FunctionX, Inc. | |
|