Code:
// Step2.cpp : edit box handling
//
#include <windows.h>
#include <math.h>
#include <stdio.h>
#include <tchar.h>
#include "resource.h"
// Calculate the real and imaginary roots of the given quadratic equation
void GetQuadraticRoots(double a, double b, double c,
double *pReal1, double *pReal2, double *pImag1, double *pImag2)
{
*pImag1 = *pImag2 = 0;
if (a == 0)
{
if (b == 0) // indeterminate - 'c = 0' so just set it to 0
*pReal1 = *pReal2 = 0;
else // linear: bx + c = 0 (so x = -c/b)
*pReal1 = *pReal2 = -c / b;
return;
}
*pReal1 = *pReal2 = -b / (2 * a);
if ((b * b) >= (4 * a * c))
{
// Real roots
*pReal1 += sqrt(b*b - 4*a*c) / (2 * a);
*pReal2 += sqrt(b*b - 4*a*c) / (2 * a);
}
else
{
// Complex roots
*pImag1 = sqrt(4*a*c - b*b) / (2 * a);
*pImag2 = -*pImag1;
}
}
BOOL MainDialog_OnCommand(HWND hWnd, WORD wCommand, WORD wNotify, HWND hControl)
{
double a, b, c, r1, r2, i1, i2;
TCHAR buffer[64], *pEnd;
switch (wCommand)
{
case IDOK:
SetDlgItemText(hWnd,IDC_A, "2");
GetDlgItemText(hWnd, IDC_A, buffer, sizeof(buffer) / sizeof(TCHAR));
a = _tcstod(buffer, &pEnd);
SetDlgItemText(hWnd,IDC_B, "4");
GetDlgItemText(hWnd, IDC_B, buffer, sizeof(buffer) / sizeof(TCHAR));
b = _tcstod(buffer, &pEnd);
SetDlgItemText(hWnd,IDC_C, "6");
GetDlgItemText(hWnd, IDC_C, buffer, sizeof(buffer) / sizeof(TCHAR));
c = _tcstod(buffer, &pEnd);
GetQuadraticRoots(a, b, c, &r1, &r2, &i1, &i2);
_sntprintf(buffer, sizeof(buffer) / sizeof(buffer[0]),
_T("Roots: %f%+fj, %f%+fj\n"), r1, i1, r2, i2);
MessageBox(hWnd, buffer, _T("Quadratic Solver"), MB_OK);
break;
case IDCANCEL:
EndDialog(hWnd, 1);
break;
}
return TRUE;
}
// Main dialog message-handling function
BOOL MainDialogProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_COMMAND:
return MainDialog_OnCommand(hWnd, LOWORD(wParam), HIWORD(wParam), (HWND)lParam);
case WM_CLOSE:
EndDialog(hWnd, 0);
return TRUE;
}
return FALSE;
}
int _tmain(void)
{
DialogBoxParam(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_DIALOG1),
NULL, (DLGPROC)MainDialogProc, 0);
return 0;
}