Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Скачиваний:
23
Добавлен:
21.08.2019
Размер:
3.85 Кб
Скачать
#include <Windows.h>
#include <string>
#include "resource.h";
#include <vector>
#include <ctime>
#include <stdlib.h>
#include <math.h>

using namespace std;

BOOL CALLBACK MainPage(HWND, UINT, WPARAM, LPARAM);

double * calculateMethod(double **, double *, int, bool&);
double **X;
double *B;
double *res;
bool calc;

int CALLBACK WinMain(HINSTANCE hInstance, HINSTANCE hPrevInst, LPSTR lpCmdLine, int nCmdShow)
{
	DialogBoxParam(hInstance, MAKEINTRESOURCE(SourcePage), 0, (MainPage), 0);

	return 0;
}

void setArrays(HWND hwnd)
{
	HWND hWnd;
	srand(time(0)); 

	while (true)
	{
		calc = true;
		for (int i = 0; i < 2; i++)
		{
			for (int j = 0; j < 2; j++)
			{
				X[i][j] = rand() % 100 - 10;
			}
		}
		for (int i = 0; i < 2; i++)
		{
			B[i] = rand() % 100 - 10;
		}

		char buff[50] = {};

		_itoa_s((int)X[0][0], buff, 10);
		hWnd = GetDlgItem(hwnd, a11);
		SetWindowText(hWnd, buff);

		_itoa_s((int)X[0][1], buff, 10);
		hWnd = GetDlgItem(hwnd, a13);
		SetWindowText(hWnd, buff);

		_itoa_s((int)X[1][0], buff, 10);
		hWnd = GetDlgItem(hwnd, a21);
		SetWindowText(hWnd, buff);

		_itoa_s((int)X[1][1], buff, 10);
		hWnd = GetDlgItem(hwnd, a23);
		SetWindowText(hWnd, buff);

		_itoa_s((int)B[0], buff, 10);
		hWnd = GetDlgItem(hwnd, b1);
		SetWindowText(hWnd, buff);

		_itoa_s((int)B[1], buff, 10);
		hWnd = GetDlgItem(hwnd, b2);
		SetWindowText(hWnd, buff);

		res = calculateMethod(X, B, 2, calc);
		if (calc)
			break;
	}
}

BOOL CALLBACK MainPage(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	switch (uMsg)
	{
	case WM_INITDIALOG:
	{
		X = new double*[2];
		for (size_t i = 0; i < 2; i++)
		{
			X[i] = new double[2];
		}
		B = new double[2];
		res = new double[2];

		setArrays(hwnd);

		break;
	}
	case WM_COMMAND:
	{
		switch (LOWORD(wParam))
		{
		case IDC_BUTTON1:
		{
			HWND hw1, hw2;
			hw1 = GetDlgItem(hwnd, IDC_EDIT1);
			hw2 = GetDlgItem(hwnd, IDC_EDIT2);

			char buff1[50] = {};
			char buff2[50] = {};

			GetWindowText(hw1, buff1, 10);
			GetWindowText(hw2, buff2, 10);

			float tmpItem1 = atof(buff1);
			float tmpItem2 = atof(buff2);

			if (fabs(res[0] - tmpItem1) <= 0.01 && fabs(res[1] - tmpItem2) <= 0.01)
			{
				MessageBox(0, "Верно ", "Info", MB_ICONINFORMATION);
			}
			else
			{
				MessageBox(0, "Неверно ", "Info", MB_ICONINFORMATION);
			}
			break;
		}
		case IDC_BUTTON3:
		{
			setArrays(hwnd);
			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;
}
Соседние файлы в папке Project1
  • #
    21.08.20195.89 Кб23Project1.rc
  • #
    21.08.20191.32 Кб23Project1.vcxproj.filters
  • #
    21.08.2019165 б22Project1.vcxproj.user
  • #
    21.08.20196.1 Кб22Project1.vcxproj.xml
  • #
    21.08.20191.23 Кб22resource.h
  • #
    21.08.20193.85 Кб23Source.cpp