Код программы
BOOL CALLBACK MainPage(HWND, UINT, WPARAM, LPARAM);
HWND hListBox;
int n;
int x, y, wStatic, wLabel, h;
double * calculateMethod(double **, double *, int, bool&);
int CALLBACK WinMain(HINSTANCE hInstance, HINSTANCE hPrevInst, LPSTR lpCmdLine, int nCmdShow)
{
DialogBoxParam(hInstance, MAKEINTRESOURCE(SourcePage), 0, (MainPage), 0);
return 0;
}
BOOL CALLBACK EnumProc(HWND hwnd, LPARAM lParam)
{
vector<HWND> * lpvHwnd = (vector<HWND> *)lParam;
char buf[255];
GetClassName(hwnd, buf, 255);
if (strcmp(buf, "Edit") == 0)
{
lpvHwnd->push_back(hwnd);
}
return TRUE;
}
BOOL CALLBACK MainPage(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_INITDIALOG:
{
hListBox = GetDlgItem(hwnd, IDC_LIST1);
for (int i = 0; i < 10; i++)
{
char tmp[3] = {};
_itoa_s(i + 1, tmp, 10);
SendMessage(hListBox, LB_ADDSTRING, 0, (LPARAM)tmp);
}
break;
}
case WM_COMMAND:
{
switch (LOWORD(wParam))
{
case IDC_BUTTON1:
{
static HWND hwnd_st_u, hwnd_ed_u;
y = 80;
x = 20;
h = 20;
wStatic = 25;
wLabel = 35;
while (true)
{
HWND cloneWin = FindWindowEx(hwnd, 0, "edit", 0);
if (cloneWin)
{
DestroyWindow(cloneWin);
}
else
{
break;
}
}
while (true)
{
HWND cloneWin = FindWindowEx(hwnd, 0, "static", 0);
if (cloneWin)
{
DestroyWindow(cloneWin);
}
else
{
break;
}
}
char tmpN[10] = {};
int i = SendMessage(hListBox, LB_GETCURSEL, 0, 0);
SendMessage(hListBox, LB_GETTEXT, i, (LPARAM)tmpN);
n = atoi(tmpN);
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
string st = "x" + to_string(j + 1);
hwnd_ed_u = CreateWindow("edit", "", WS_CHILD | WS_VISIBLE | WS_TABSTOP | ES_RIGHT | WS_BORDER, x, y - 2, wLabel, h,
hwnd, (HMENU)(501), (HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE), NULL);
SetWindowText(hwnd_ed_u, "0");
x += wLabel + 4;
hwnd_st_u = CreateWindow("static", "ST_U", WS_CHILD | WS_VISIBLE | WS_TABSTOP, x, y, wStatic, h,
hwnd, (HMENU)(502), (HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE), NULL);
SetWindowText(hwnd_st_u, st.c_str());
x += wStatic;
hwnd_st_u = CreateWindow("static", "ST_U", WS_CHILD | WS_VISIBLE | WS_TABSTOP, x, y, wStatic, h,
hwnd, (HMENU)(503), (HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE), NULL);
if (j != n - 1)
{
SetWindowText(hwnd_st_u, "+");
x += wStatic / 2 + 2;
}
else
{
SetWindowText(hwnd_st_u, "=");
x += wStatic / 2 + 6;
hwnd_ed_u = CreateWindow("edit", "", WS_CHILD | WS_VISIBLE | WS_TABSTOP | ES_LEFT | WS_BORDER, x, y - 2, wLabel, h,
hwnd, (HMENU)(504), (HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE), NULL);
SetWindowText(hwnd_ed_u, "0");
}
}
y += h;
x = 20;
}
break;
}
case IDC_BUTTON2:
{
try
{
wStatic = 25;
double **a, *_y, *_x;
bool calc = true;
a = new double*[n];
_y = new double[n];
for (int i = 0; i < n; i++)
{
a[i] = new double[n];
}
static vector<HWND> vHwnd;
vHwnd.clear();
EnumChildWindows(hwnd, EnumProc, (LPARAM)&vHwnd);
int k = 0;
int iArr = 0, jArr = 0, iY = 0;
for (int l = 0; l < vHwnd.size(); l++)
{
char tmp[10] = {};
GetWindowText(vHwnd[l], tmp, 10);
float tmpItem = atof(tmp);
if (k == n)
{
_y[iY] = (double)tmpItem;
iY++;
iArr++;
jArr = 0;
k = 0;
}
else
{
a[iArr][jArr] = (double)tmpItem;
jArr++;
k++;
}
}
_x = calculateMethod(a, _y, n, calc);
if (calc)
{
static HWND hwnd_st_u;
y = 80;
x = 20;
x += wStatic * n * 2 + wLabel * (n + 1) + 20;
wStatic = 200;
for (int i = 0; i < n; i++)
{
string st = "x" + to_string(i + 1) + " = " + to_string(_x[i]);
hwnd_st_u = CreateWindow("static", "ST_U", WS_CHILD | WS_VISIBLE | WS_TABSTOP, x, y, wStatic, h,
hwnd, (HMENU)(502), (HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE), NULL);
SetWindowText(hwnd_st_u, st.c_str());
y += h;
}
}
}
catch (exception ex)
{
}
break;
}
}
break;
}
case WM_CLOSE:
{
EndDialog(hwnd, 0);
break;
}
}
return FALSE;
}
double * calculateMethod(double **arr, double *y, int n, bool& calc)
{
double *x, max;
int k, index;
const double eps = 0.00001;
x = new double[n];
k = 0;
while (k < n)
{
max = abs(arr[k][k]);
index = k;
for (int i = k + 1; i < n; i++)
{
if (abs(arr[i][k]) > max)
{
max = abs(arr[i][k]);
index = i;
}
}
if (max < eps)
{
MessageBox(0, "Решение получить невозможно из-за нулевого столбца ", "Error", MB_ICONERROR);
calc = false;
return 0;
}
for (int j = 0; j < n; j++)
{
double temp = arr[k][j];
arr[k][j] = arr[index][j];
arr[index][j] = temp;
}
double temp = y[k];
y[k] = y[index];
y[index] = temp;
for (int i = k; i < n; i++)
{
double temp = arr[i][k];
if (abs(temp) < eps)
{
continue;
}
for (int j = 0; j < n; j++)
{
arr[i][j] = arr[i][j] / temp;
}
y[i] = y[i] / temp;
if (i == k)
{
continue;
}
for (int j = 0; j < n; j++)
{
arr[i][j] = arr[i][j] - arr[k][j];
}
y[i] = y[i] - y[k];
}
k++;
}
for (k = n - 1; k >= 0; k--)
{
x[k] = y[k];
for (int i = 0; i < k; i++)
{
y[i] = y[i] - arr[i][k] * x[k];
}
}
return x;
}