Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

ООП / C++ / Lab4C / Matrix

.cpp
Скачиваний:
65
Добавлен:
18.02.2017
Размер:
9.54 Кб
Скачать
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Matrix.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
	: TForm(Owner)
{
Count = 0;
Select = -1;
dx = 0;
dy = 0;
op = 0;
FArg = -1;
SArg = -1;
Yes = false;
MouseDown = false;
Image1->Canvas->Brush->Color = clWhite;
Image1->Canvas->FillRect(Rect(0, 0, Image1->Width, Image1->Height));
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Image1MouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift,
		  int X, int Y)
{
//Проверяем нажатие на матрицу ----------------------------------------------
MouseDown = true;
//Выделяем/снимаем выделение в положительном случае -------------------------
	for (int i = 0; i < Count; i++) {
		if ((X >= A[i]->X) && (X <= A[i]->X + A[i]->WS) && (Y >= A[i]->Y + 1) && (Y <= A[i]->Y + A[i]->HS)) {
			Yes = true;
			if (A[i]->Selected) {
				Image1->Canvas->Pen->Color = clWhite;
				Image1->Canvas->Pen->Width = 2;
				Image1->Canvas->Brush->Style = bsClear;
				Image1->Canvas->Rectangle(A[i]->X - 1, A[i]->Y, A[i]->X + A[i]->WS + 1, A[i]->Y + A[i]->HS);
				A[i]->Selected = false;
				Select = -1;
			}
			else {
				if (Select != -1) {
					Image1->Canvas->Pen->Color = clWhite;
					Image1->Canvas->Pen->Width = 2;
					Image1->Canvas->Brush->Style = bsClear;
					Image1->Canvas->Rectangle(A[Select]->X - 1, A[Select]->Y, A[Select]->X + A[Select]->WS + 1, A[Select]->Y + A[Select]->HS);
					Select = -1;
				}
				Image1->Canvas->Pen->Color = clGreen;
				Image1->Canvas->Pen->Width = 2;
				Image1->Canvas->Brush->Style=bsClear;
				Image1->Canvas->Rectangle(A[i]->X - 1, A[i]->Y, A[i]->X + A[i]->WS + 1, A[i]->Y + A[i]->HS);
				A[i]->Selected = true;
				Select = i;
				dx = X - A[i]->X;
				dy = Y - A[i]->Y;
				}
			}
		}
//Делаем новую матрицу в отрицательном случае -------------------------------
	if (!Yes) {
		if (Count < 10) {
			A[Count] = new CMatrix();
			String S[10] = {""};
			for (int i = 0; i < (A[Count]->Height)*(A[Count]->Width); i++) {
				S[i / A[Count]->Width] += IntToStr(A[Count]->Matrix[i]) + " ";
			}
			for (int i = 0; i < A[Count]->Height; i++) {
				Image1->Canvas->Brush->Style = bsClear;
				Image1->Canvas->TextOutW(X, Y + (i * 12), S[i]);
			}
			A[Count]->X = X;
			A[Count]->Y = Y;
			Count++;
		}
		else
			MessageDlg("Добавлено максимальное количество объектов", mtInformation, TMsgDlgButtons() << mbOK, 0);
	}
	Yes = false;
	Label1->Caption = Select;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
	if (Count < 10) {
		A[Count] = new CMatrix(StrToInt(Edit1->Text), StrToInt(Edit2->Text));
		String S[10] = {""};
		for (int i = 0; i < (A[Count]->Height)*(A[Count]->Width); i++) {
			S[i / A[Count]->Width] += IntToStr(A[Count]->Matrix[i]) + " ";
		}
		for (int i = 0; i < A[Count]->Height; i++) {
			Image1->Canvas->Brush->Style = bsClear;
			Image1->Canvas->TextOutW(10, 10 + (i * 12), S[i]);
		}
		A[Count]->X = 10;
		A[Count]->Y = 10;
		Count++;
	}
	else
		MessageDlg("Добавлено максимальное количество объектов", mtInformation, TMsgDlgButtons() << mbOK, 0);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
	if (Count < 10) {
		A[Count] = new CMatrix();
		String S[10] = {""};
		for (int i = 0; i < (A[Count]->Height)*(A[Count]->Width); i++) {
			S[i / A[Count]->Width] += IntToStr(A[Count]->Matrix[i]) + " ";
		}
		for (int i = 0; i < A[Count]->Height; i++) {
			Image1->Canvas->Brush->Style = bsClear;
			Image1->Canvas->TextOutW(10, 10 + (i * 12), S[i]);
		}
		A[Count]->X = 10;
		A[Count]->Y = 10;
		Count++;
	}
	else
		MessageDlg("Добавлено максимальное количество объектов", mtInformation, TMsgDlgButtons() << mbOK, 0);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button3Click(TObject *Sender)
{
	Count = 0;
	Select = -1;
	dx = 0;
	dy = 0;
	op = 0;
	FArg = -1;
	SArg = -1;
	Yes = false;
	MouseDown = false;
	Image1->Canvas->Brush->Color = clWhite;
	Image1->Canvas->FillRect(Rect(0, 0, Image1->Width, Image1->Height));
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Image1MouseUp(TObject *Sender, TMouseButton Button, TShiftState Shift,
		  int X, int Y)
{
	MouseDown = false;
	if (Button==mbRight) {
	for (int i = 0; i < Count; i++) {
		if (A[i]->Selected) {
			A[i]->X = X - dx;
			A[i]->Y = Y - dy;
		}
	}
	Image1->Canvas->Brush->Color = clWhite;
	Image1->Canvas->FillRect(Rect(0, 0, Image1->Width, Image1->Height));
	for (int i = 0; i < Count; i++) {
		String S[10] = {""};
		for (int j = 0; j < (A[i]->Height)*(A[i]->Width); j++) {
			S[j / A[i]->Width] += IntToStr(A[i]->Matrix[j]) + " ";
		}
		for (int j = 0; j < A[i]->Height; j++) {
			Image1->Canvas->Brush->Style = bsClear;
			Image1->Canvas->TextOutW(A[i]->X, A[i]->Y + (j * 12), S[j]);
		}
		A[i]->Selected = false;
		Select = -1;
		}
		Label1->Caption = Select;
	}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button4Click(TObject *Sender)
{
	if (Select == -1) {
		MessageDlg("Выберите первый аргумент", mtWarning, TMsgDlgButtons() << mbOK, 0);
	}
	else {
		op = 1;
		FArg = Select;
	}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button5Click(TObject *Sender)
{
	if (Select == -1) {
		MessageDlg("Выберите первый аргумент", mtWarning, TMsgDlgButtons() << mbOK, 0);
	}
	else {
		op = 2;
		FArg = Select;
	}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button6Click(TObject *Sender)
{
	if (Select == -1) {
		MessageDlg("Выберите первый аргумент", mtWarning, TMsgDlgButtons() << mbOK, 0);
	}
	else {
		op = 3;
		FArg = Select;
	}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button7Click(TObject *Sender)
{
	if (Select == -1) {
		MessageDlg("Выберите второй аргумент", mtWarning, TMsgDlgButtons() << mbOK, 0);
	}

	else {
		SArg = Select;
		if (op == 1) {
			if ((A[FArg]->Height == A[SArg]->Height) && (A[FArg]->Width == A[SArg]->Width)) {
				A[Count] = new CMatrix(A[SArg]->Height, A[SArg]->Width);
				*(A[Count]) = *(A[FArg]) + *(A[SArg]);
				String S[10] = {""};
				for (int i = 0; i < (A[Count]->Height)*(A[Count]->Width); i++) {
					S[i / A[Count]->Width] += IntToStr(A[Count]->Matrix[i]) + " ";
				}
				for (int i = 0; i < A[Count]->Height; i++) {
					Image1->Canvas->Brush->Style = bsClear;
					Image1->Canvas->TextOutW(10, 10 + (i * 12), S[i]);
				}
				A[Count]->X = 10;
				A[Count]->Y = 10;
				Count++;
				A[FArg]->Selected = false;
				A[SArg]->Selected = false;
				A[Count - 1]->Selected = false;
				FArg = -1;
				SArg = -1;
				op = -1;
			}
			else {
				MessageDlg("Обе размерности матриц должны совпадать", mtWarning, TMsgDlgButtons() << mbOK, 0);
			}
			Image1->Canvas->Pen->Color = clWhite;
			Image1->Canvas->Pen->Width = 2;
			Image1->Canvas->Brush->Style = bsClear;
			Image1->Canvas->Rectangle(A[Select]->X - 1, A[Select]->Y, A[Select]->X + A[Select]->WS + 1, A[Select]->Y + A[Select]->HS);
			A[Select]->Selected = false;
			Select = -1;
		}
		else
			if (op == 2) {
				if ((A[FArg]->Height == A[SArg]->Height) && (A[FArg]->Width == A[SArg]->Width)) {
					if ((A[FArg]->Height == A[SArg]->Height) && (A[FArg]->Width == A[SArg]->Width)) {
						A[Count] = new CMatrix(A[SArg]->Height, A[SArg]->Width);
						*(A[Count]) = *(A[FArg]) - *(A[SArg]);
						String S[10] = {""};
						for (int i = 0; i < (A[Count]->Height)*(A[Count]->Width); i++) {
							S[i / A[Count]->Width] += IntToStr(A[Count]->Matrix[i]) + " ";
						}
						for (int i = 0; i < A[Count]->Height; i++) {
							Image1->Canvas->Brush->Style = bsClear;
							Image1->Canvas->TextOutW(100, 10 + (i * 12), S[i]);
						}
						A[Count]->X = 10;
						A[Count]->Y = 10;
						Count++;
						A[FArg]->Selected = false;
						A[SArg]->Selected = false;
						A[Count - 1]->Selected = false;
						FArg = -1;
						SArg = -1;
						op = -1;
					}
				}
				else {
					MessageDlg("Обе размерности матриц должны совпадать", mtWarning, TMsgDlgButtons() << mbOK, 0);
				}
				Image1->Canvas->Pen->Color = clWhite;
				Image1->Canvas->Pen->Width = 2;
				Image1->Canvas->Brush->Style = bsClear;
				Image1->Canvas->Rectangle(A[Select]->X - 1, A[Select]->Y, A[Select]->X + A[Select]->WS + 1, A[Select]->Y + A[Select]->HS);
				A[Select]->Selected = false;
				Select = -1;
			}
			else
				if (op ==3) {
					if (A[FArg]->Width == A[SArg]->Height) {

					}
					else
						MessageDlg("Умножение возможно только для матриц вида AxB * BxC", mtWarning, TMsgDlgButtons() << mbOK, 0);
				}
				else
					MessageDlg("Предвиденная ошибка", mtWarning, TMsgDlgButtons() << mbOK, 0);

	}
}
//---------------------------------------------------------------------------
Соседние файлы в папке Lab4C