Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- namespace Project2 {
- using namespace System;
- using namespace System::ComponentModel;
- using namespace System::Collections;
- using namespace System::Windows::Forms;
- using namespace System::Data;
- using namespace System::Drawing;
- /// <summary>
- /// Summary for MainForm
- /// </summary>
- public ref class MainForm : public System::Windows::Forms::Form
- {
- public:
- MainForm(void)
- {
- InitializeComponent();
- //
- //TODO: Add the constructor code here
- //
- }
- protected:
- /// <summary>
- /// Clean up any resources being used.
- /// </summary>
- ~MainForm()
- {
- if (components)
- {
- delete components;
- }
- }
- private: System::Windows::Forms::DataGridView^ matrixGridView;
- protected:
- protected:
- private: System::Windows::Forms::Label^ matrixCapture;
- private: System::Windows::Forms::Button^ calculateButton;
- private: System::Windows::Forms::DataGridView^ rightPartGridView;
- private: System::Windows::Forms::Label^ equalLabel;
- private: System::Windows::Forms::Label^ taskLabel;
- private: System::Windows::Forms::Label^ rightPartLabel;
- private: System::Windows::Forms::DataGridViewTextBoxColumn^ tableColumn1;
- private: System::Windows::Forms::DataGridViewTextBoxColumn^ tableColumn2;
- private: System::Windows::Forms::DataGridViewTextBoxColumn^ tableColumn3;
- private: System::Windows::Forms::DataGridViewTextBoxColumn^ tableColumn4;
- private: System::Windows::Forms::DataGridViewTextBoxColumn^ rightPartColumn;
- private:
- /// <summary>
- /// Required designer variable.
- /// </summary>
- System::ComponentModel::Container ^components;
- #pragma region Windows Form Designer generated code
- /// <summary>
- /// Required method for Designer support - do not modify
- /// the contents of this method with the code editor.
- /// </summary>
- void InitializeComponent(void)
- {
- this->matrixGridView = (gcnew System::Windows::Forms::DataGridView());
- this->tableColumn1 = (gcnew System::Windows::Forms::DataGridViewTextBoxColumn());
- this->tableColumn2 = (gcnew System::Windows::Forms::DataGridViewTextBoxColumn());
- this->tableColumn3 = (gcnew System::Windows::Forms::DataGridViewTextBoxColumn());
- this->tableColumn4 = (gcnew System::Windows::Forms::DataGridViewTextBoxColumn());
- this->matrixCapture = (gcnew System::Windows::Forms::Label());
- this->calculateButton = (gcnew System::Windows::Forms::Button());
- this->rightPartGridView = (gcnew System::Windows::Forms::DataGridView());
- this->rightPartColumn = (gcnew System::Windows::Forms::DataGridViewTextBoxColumn());
- this->equalLabel = (gcnew System::Windows::Forms::Label());
- this->taskLabel = (gcnew System::Windows::Forms::Label());
- this->rightPartLabel = (gcnew System::Windows::Forms::Label());
- (cli::safe_cast<System::ComponentModel::ISupportInitialize^ >(this->matrixGridView))->BeginInit();
- (cli::safe_cast<System::ComponentModel::ISupportInitialize^ >(this->rightPartGridView))->BeginInit();
- this->SuspendLayout();
- //
- // matrixGridView
- //
- this->matrixGridView->AllowUserToAddRows = false;
- this->matrixGridView->AllowUserToDeleteRows = false;
- this->matrixGridView->ColumnHeadersHeightSizeMode = System::Windows::Forms::DataGridViewColumnHeadersHeightSizeMode::AutoSize;
- this->matrixGridView->Columns->AddRange(gcnew cli::array< System::Windows::Forms::DataGridViewColumn^ >(4) {this->tableColumn1,
- this->tableColumn2, this->tableColumn3, this->tableColumn4});
- this->matrixGridView->Location = System::Drawing::Point(12, 57);
- this->matrixGridView->Name = L"matrixGridView";
- this->matrixGridView->Size = System::Drawing::Size(283, 111);
- this->matrixGridView->TabIndex = 0;
- this->matrixGridView->CellEnter += gcnew System::Windows::Forms::DataGridViewCellEventHandler(this, &MainForm::matrixGridView_CellEnter);
- this->matrixGridView->CellValueChanged += gcnew System::Windows::Forms::DataGridViewCellEventHandler(this, &MainForm::matrixGridView_CellValueChanged);
- //
- // tableColumn1
- //
- this->tableColumn1->HeaderText = L"[x1]";
- this->tableColumn1->Name = L"tableColumn1";
- this->tableColumn1->Width = 60;
- //
- // tableColumn2
- //
- this->tableColumn2->HeaderText = L"[x2]";
- this->tableColumn2->Name = L"tableColumn2";
- this->tableColumn2->Width = 60;
- //
- // tableColumn3
- //
- this->tableColumn3->HeaderText = L"[x3]";
- this->tableColumn3->Name = L"tableColumn3";
- this->tableColumn3->Width = 60;
- //
- // tableColumn4
- //
- this->tableColumn4->HeaderText = L"[x4]";
- this->tableColumn4->Name = L"tableColumn4";
- this->tableColumn4->Width = 60;
- //
- // matrixCapture
- //
- this->matrixCapture->AutoSize = true;
- this->matrixCapture->Location = System::Drawing::Point(9, 41);
- this->matrixCapture->Name = L"matrixCapture";
- this->matrixCapture->Size = System::Drawing::Size(124, 13);
- this->matrixCapture->TabIndex = 1;
- this->matrixCapture->Text = L"Матриця рівняння (4x4)";
- //
- // calculateButton
- //
- this->calculateButton->Location = System::Drawing::Point(343, 174);
- this->calculateButton->Name = L"calculateButton";
- this->calculateButton->Size = System::Drawing::Size(104, 23);
- this->calculateButton->TabIndex = 2;
- this->calculateButton->Text = L"Знайти розв\'язки";
- this->calculateButton->UseVisualStyleBackColor = true;
- this->calculateButton->Click += gcnew System::EventHandler(this, &MainForm::calculateButton_Click);
- //
- // rightPartGridView
- //
- this->rightPartGridView->AllowUserToAddRows = false;
- this->rightPartGridView->AllowUserToDeleteRows = false;
- this->rightPartGridView->ColumnHeadersHeightSizeMode = System::Windows::Forms::DataGridViewColumnHeadersHeightSizeMode::AutoSize;
- this->rightPartGridView->Columns->AddRange(gcnew cli::array< System::Windows::Forms::DataGridViewColumn^ >(1) {this->rightPartColumn});
- this->rightPartGridView->Location = System::Drawing::Point(343, 57);
- this->rightPartGridView->Name = L"rightPartGridView";
- this->rightPartGridView->Size = System::Drawing::Size(104, 111);
- this->rightPartGridView->TabIndex = 3;
- this->rightPartGridView->CellEnter += gcnew System::Windows::Forms::DataGridViewCellEventHandler(this, &MainForm::rightPartGridView_CellEnter);
- this->rightPartGridView->CellValueChanged += gcnew System::Windows::Forms::DataGridViewCellEventHandler(this, &MainForm::rightPartGridView_CellValueChanged);
- //
- // rightPartColumn
- //
- this->rightPartColumn->HeaderText = L"[y]";
- this->rightPartColumn->Name = L"rightPartColumn";
- this->rightPartColumn->Width = 60;
- //
- // equalLabel
- //
- this->equalLabel->AutoSize = true;
- this->equalLabel->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 25, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
- static_cast<System::Byte>(204)));
- this->equalLabel->Location = System::Drawing::Point(301, 100);
- this->equalLabel->Name = L"equalLabel";
- this->equalLabel->Size = System::Drawing::Size(37, 39);
- this->equalLabel->TabIndex = 4;
- this->equalLabel->Text = L"=";
- //
- // taskLabel
- //
- this->taskLabel->AutoSize = true;
- this->taskLabel->Location = System::Drawing::Point(9, 9);
- this->taskLabel->Name = L"taskLabel";
- this->taskLabel->Size = System::Drawing::Size(267, 13);
- this->taskLabel->TabIndex = 5;
- this->taskLabel->Text = L"За допомогою правила Крамера розв’язати СЛАР:";
- //
- // rightPartLabel
- //
- this->rightPartLabel->AutoSize = true;
- this->rightPartLabel->Location = System::Drawing::Point(340, 41);
- this->rightPartLabel->Name = L"rightPartLabel";
- this->rightPartLabel->Size = System::Drawing::Size(113, 13);
- this->rightPartLabel->TabIndex = 6;
- this->rightPartLabel->Text = L"Вектор значень (4x1)";
- //
- // MainForm
- //
- this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
- this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
- this->ClientSize = System::Drawing::Size(460, 207);
- this->Controls->Add(this->rightPartLabel);
- this->Controls->Add(this->taskLabel);
- this->Controls->Add(this->equalLabel);
- this->Controls->Add(this->rightPartGridView);
- this->Controls->Add(this->calculateButton);
- this->Controls->Add(this->matrixCapture);
- this->Controls->Add(this->matrixGridView);
- this->Name = L"MainForm";
- this->Text = L"Розрахункова (завдання 2)";
- this->Load += gcnew System::EventHandler(this, &MainForm::MainForm_Load);
- (cli::safe_cast<System::ComponentModel::ISupportInitialize^ >(this->matrixGridView))->EndInit();
- (cli::safe_cast<System::ComponentModel::ISupportInitialize^ >(this->rightPartGridView))->EndInit();
- this->ResumeLayout(false);
- this->PerformLayout();
- }
- #pragma endregion
- /*
- Початкова ініціалізація форми
- */
- private: System::Void MainForm_Load(System::Object^ sender, System::EventArgs^ e) {
- /*
- Ініціалізація головної матриці
- */
- initializeMatrixGridView(true);
- /*
- Ініціалізація вектора правої частини системи рівнянь
- */
- initializeRightPartGridView(true);
- }
- System::Void initializeMatrixGridView(bool values){
- {
- DataGridViewRow ^ row = gcnew DataGridViewRow;
- row->CreateCells(matrixGridView);
- if (values){
- row->Cells[0]->Value=-1;
- row->Cells[1]->Value=2;
- row->Cells[2]->Value=1;
- row->Cells[3]->Value=1;
- }
- matrixGridView->Rows->Add(row);
- }
- {
- DataGridViewRow ^ row = gcnew DataGridViewRow;
- row->CreateCells(matrixGridView);
- if (values){
- row->Cells[0]->Value=-4;
- row->Cells[1]->Value=5;
- row->Cells[2]->Value=3;
- row->Cells[3]->Value=2;
- }
- matrixGridView->Rows->Add(row);
- }
- {
- DataGridViewRow ^ row = gcnew DataGridViewRow;
- row->CreateCells(matrixGridView);
- if (values){
- row->Cells[0]->Value=1;
- row->Cells[1]->Value=4;
- row->Cells[2]->Value=2;
- row->Cells[3]->Value=1;
- }
- matrixGridView->Rows->Add(row);
- }
- {
- DataGridViewRow ^ row = gcnew DataGridViewRow;
- row->CreateCells(matrixGridView);
- if (values){
- row->Cells[0]->Value=2;
- row->Cells[1]->Value=-1;
- row->Cells[2]->Value=1;
- row->Cells[3]->Value=4;
- }
- matrixGridView->Rows->Add(row);
- }
- };
- System::Void initializeRightPartGridView(bool values){
- {
- DataGridViewRow ^ row = gcnew DataGridViewRow;
- row->CreateCells(rightPartGridView);
- if (values){
- row->Cells[0]->Value=-3;
- }
- rightPartGridView->Rows->Add(row);
- }
- {
- DataGridViewRow ^ row = gcnew DataGridViewRow;
- row->CreateCells(rightPartGridView);
- if (values){
- row->Cells[0]->Value=0;
- }
- rightPartGridView->Rows->Add(row);
- }
- {
- DataGridViewRow ^ row = gcnew DataGridViewRow;
- row->CreateCells(rightPartGridView);
- if (values){
- row->Cells[0]->Value=3;
- }
- rightPartGridView->Rows->Add(row);
- }
- {
- DataGridViewRow ^ row = gcnew DataGridViewRow;
- row->CreateCells(rightPartGridView);
- if (values){
- row->Cells[0]->Value=-6;
- }
- rightPartGridView->Rows->Add(row);
- }
- };
- /*
- Знаходження розвязку
- */
- private: System::Void calculateButton_Click(System::Object^ sender, System::EventArgs^ e) {
- double delta = getDelta(getMainMatrix());
- double delta1 = getDelta(getMatrixForDelta1());
- double delta2 = getDelta(getMatrixForDelta2());
- double delta3 = getDelta(getMatrixForDelta3());
- double delta4 = getDelta(getMatrixForDelta4());
- double x1 = delta1/delta;
- double x2 = delta2/delta;
- double x3 = delta3/delta;
- double x4 = delta4/delta;
- String ^ res = "Результат: " + "x1="+x1 + "; " + "x2="+x2 + "; " + "x3="+x3 + "; " + "x4="+x4 + "; ";
- MessageBox::Show(res,"Результат:",MessageBoxButtons::OK, MessageBoxIcon::Information);
- }
- /*
- Створення пустої матриці розміром 4 на 4
- */
- double ** createEmptyMatrix(){
- const int n=4;
- double **a = new double * [n];
- for (int i=0; i<n; i++){
- a[i] = new double [n];
- }
- return a;
- }
- double ** getMainMatrix(){
- double **a = createEmptyMatrix();
- /* відбувається заповнення матриці числами, що є в основній матриці
- (це потрібно для пошуку delta);
- */
- a[0][0] = Convert::ToDouble(matrixGridView->Rows[0]->Cells[0]->Value);
- a[0][1] = Convert::ToDouble(matrixGridView->Rows[0]->Cells[1]->Value);
- a[0][2] = Convert::ToDouble(matrixGridView->Rows[0]->Cells[2]->Value);
- a[0][3] = Convert::ToDouble(matrixGridView->Rows[0]->Cells[3]->Value);
- a[1][0] = Convert::ToDouble(matrixGridView->Rows[1]->Cells[0]->Value);
- a[1][1] = Convert::ToDouble(matrixGridView->Rows[1]->Cells[1]->Value);
- a[1][2] = Convert::ToDouble(matrixGridView->Rows[1]->Cells[2]->Value);
- a[1][3] = Convert::ToDouble(matrixGridView->Rows[1]->Cells[3]->Value);
- a[2][0] = Convert::ToDouble(matrixGridView->Rows[2]->Cells[0]->Value);
- a[2][1] = Convert::ToDouble(matrixGridView->Rows[2]->Cells[1]->Value);
- a[2][2] = Convert::ToDouble(matrixGridView->Rows[2]->Cells[2]->Value);
- a[2][3] = Convert::ToDouble(matrixGridView->Rows[2]->Cells[3]->Value);
- a[3][0] = Convert::ToDouble(matrixGridView->Rows[3]->Cells[0]->Value);
- a[3][1] = Convert::ToDouble(matrixGridView->Rows[3]->Cells[1]->Value);
- a[3][2] = Convert::ToDouble(matrixGridView->Rows[3]->Cells[2]->Value);
- a[3][3] = Convert::ToDouble(matrixGridView->Rows[3]->Cells[3]->Value);
- return a;
- }
- double ** getMatrixForDelta1(){
- double **a = createEmptyMatrix();
- /* відбувається заповнення матриці числами, що є в основній матриці,
- але перший стовпець чисел міняється на вектор значень (це потрібно для пошуку delta1);
- */
- a[0][0] = Convert::ToDouble(rightPartGridView->Rows[0]->Cells[0]->Value);
- a[0][1] = Convert::ToDouble(matrixGridView->Rows[0]->Cells[1]->Value);
- a[0][2] = Convert::ToDouble(matrixGridView->Rows[0]->Cells[2]->Value);
- a[0][3] = Convert::ToDouble(matrixGridView->Rows[0]->Cells[3]->Value);
- a[1][0] = Convert::ToDouble(rightPartGridView->Rows[1]->Cells[0]->Value);
- a[1][1] = Convert::ToDouble(matrixGridView->Rows[1]->Cells[1]->Value);
- a[1][2] = Convert::ToDouble(matrixGridView->Rows[1]->Cells[2]->Value);
- a[1][3] = Convert::ToDouble(matrixGridView->Rows[1]->Cells[3]->Value);
- a[2][0] = Convert::ToDouble(rightPartGridView->Rows[2]->Cells[0]->Value);
- a[2][1] = Convert::ToDouble(matrixGridView->Rows[2]->Cells[1]->Value);
- a[2][2] = Convert::ToDouble(matrixGridView->Rows[2]->Cells[2]->Value);
- a[2][3] = Convert::ToDouble(matrixGridView->Rows[2]->Cells[3]->Value);
- a[3][0] = Convert::ToDouble(rightPartGridView->Rows[3]->Cells[0]->Value);
- a[3][1] = Convert::ToDouble(matrixGridView->Rows[3]->Cells[1]->Value);
- a[3][2] = Convert::ToDouble(matrixGridView->Rows[3]->Cells[2]->Value);
- a[3][3] = Convert::ToDouble(matrixGridView->Rows[3]->Cells[3]->Value);
- return a;
- }
- double ** getMatrixForDelta2(){
- double **a = createEmptyMatrix();
- /* відбувається заповнення матриці числами, що є в основній матриці,
- але другий стовпець чисел міняється на вектор значень (це потрібно для пошуку delta2);
- */
- a[0][0] = Convert::ToDouble(matrixGridView->Rows[0]->Cells[0]->Value);
- a[0][1] = Convert::ToDouble(rightPartGridView->Rows[0]->Cells[0]->Value);
- a[0][2] = Convert::ToDouble(matrixGridView->Rows[0]->Cells[2]->Value);
- a[0][3] = Convert::ToDouble(matrixGridView->Rows[0]->Cells[3]->Value);
- a[1][0] = Convert::ToDouble(matrixGridView->Rows[1]->Cells[0]->Value);
- a[1][1] = Convert::ToDouble(rightPartGridView->Rows[1]->Cells[0]->Value);
- a[1][2] = Convert::ToDouble(matrixGridView->Rows[1]->Cells[2]->Value);
- a[1][3] = Convert::ToDouble(matrixGridView->Rows[1]->Cells[3]->Value);
- a[2][0] = Convert::ToDouble(matrixGridView->Rows[2]->Cells[0]->Value);
- a[2][1] = Convert::ToDouble(rightPartGridView->Rows[2]->Cells[0]->Value);
- a[2][2] = Convert::ToDouble(matrixGridView->Rows[2]->Cells[2]->Value);
- a[2][3] = Convert::ToDouble(matrixGridView->Rows[2]->Cells[3]->Value);
- a[3][0] = Convert::ToDouble(matrixGridView->Rows[3]->Cells[0]->Value);
- a[3][1] = Convert::ToDouble(rightPartGridView->Rows[3]->Cells[0]->Value);
- a[3][2] = Convert::ToDouble(matrixGridView->Rows[3]->Cells[2]->Value);
- a[3][3] = Convert::ToDouble(matrixGridView->Rows[3]->Cells[3]->Value);
- return a;
- }
- double ** getMatrixForDelta3(){
- double **a = createEmptyMatrix();
- /*відбувається заповнення матриці числами, що є в основній матриці,
- але третій стовпець чисел міняється на вектор значень (це потрібно для пошуку delta3);
- */
- a[0][0] = Convert::ToDouble(matrixGridView->Rows[0]->Cells[0]->Value);
- a[0][1] = Convert::ToDouble(matrixGridView->Rows[0]->Cells[1]->Value);
- a[0][2] = Convert::ToDouble(rightPartGridView->Rows[0]->Cells[0]->Value);
- a[0][3] = Convert::ToDouble(matrixGridView->Rows[0]->Cells[3]->Value);
- a[1][0] = Convert::ToDouble(matrixGridView->Rows[1]->Cells[0]->Value);
- a[1][1] = Convert::ToDouble(matrixGridView->Rows[1]->Cells[1]->Value);
- a[1][2] = Convert::ToDouble(rightPartGridView->Rows[1]->Cells[0]->Value);
- a[1][3] = Convert::ToDouble(matrixGridView->Rows[1]->Cells[3]->Value);
- a[2][0] = Convert::ToDouble(matrixGridView->Rows[2]->Cells[0]->Value);
- a[2][1] = Convert::ToDouble(matrixGridView->Rows[2]->Cells[1]->Value);
- a[2][2] = Convert::ToDouble(rightPartGridView->Rows[2]->Cells[0]->Value);
- a[2][3] = Convert::ToDouble(matrixGridView->Rows[2]->Cells[3]->Value);
- a[3][0] = Convert::ToDouble(matrixGridView->Rows[3]->Cells[0]->Value);
- a[3][1] = Convert::ToDouble(matrixGridView->Rows[3]->Cells[1]->Value);
- a[3][2] = Convert::ToDouble(rightPartGridView->Rows[3]->Cells[0]->Value);
- a[3][3] = Convert::ToDouble(matrixGridView->Rows[3]->Cells[3]->Value);
- return a;
- }
- double ** getMatrixForDelta4(){
- double **a = createEmptyMatrix();
- /* відбувається заповнення матриці числами, що є в основній матриці,
- але четвертий стовпець чисел міняється на вектор значень (це потрібно для пошуку delta4);
- */
- a[0][0] = Convert::ToDouble(matrixGridView->Rows[0]->Cells[0]->Value);
- a[0][1] = Convert::ToDouble(matrixGridView->Rows[0]->Cells[1]->Value);
- a[0][2] = Convert::ToDouble(matrixGridView->Rows[0]->Cells[2]->Value);
- a[0][3] = Convert::ToDouble(rightPartGridView->Rows[0]->Cells[0]->Value);
- a[1][0] = Convert::ToDouble(matrixGridView->Rows[1]->Cells[0]->Value);
- a[1][1] = Convert::ToDouble(matrixGridView->Rows[1]->Cells[1]->Value);
- a[1][2] = Convert::ToDouble(matrixGridView->Rows[1]->Cells[2]->Value);
- a[1][3] = Convert::ToDouble(rightPartGridView->Rows[1]->Cells[0]->Value);
- a[2][0] = Convert::ToDouble(matrixGridView->Rows[2]->Cells[0]->Value);
- a[2][1] = Convert::ToDouble(matrixGridView->Rows[2]->Cells[1]->Value);
- a[2][2] = Convert::ToDouble(matrixGridView->Rows[2]->Cells[2]->Value);
- a[2][3] = Convert::ToDouble(rightPartGridView->Rows[2]->Cells[0]->Value);
- a[3][0] = Convert::ToDouble(matrixGridView->Rows[3]->Cells[0]->Value);
- a[3][1] = Convert::ToDouble(matrixGridView->Rows[3]->Cells[1]->Value);
- a[3][2] = Convert::ToDouble(matrixGridView->Rows[3]->Cells[2]->Value);
- a[3][3] = Convert::ToDouble(rightPartGridView->Rows[3]->Cells[0]->Value);
- return a;
- }
- ////////////////////////////////////////////////
- double getDelta(double **a){
- return determinant(a,4); // виклик функції determinant =>
- }
- /*
- Пошук в матриці a[m][n] елемента з вказаним значенням
- Повертається його номер рядка і стовпця uI, uJ, якщо елемент знайдено.
- match - шукати рівний елемент, або відмінний від вказаного.
- Поверне 0 - якщо не знайдено, не 0 - якщо знайдено
- */
- int search (double **a, int m, int n, double what,
- bool match, unsigned int &uI, unsigned int &uJ, unsigned int starti, unsigned int startj) {
- if ((!m) || (!n)) return 0;
- if ((starti >= n) || (startj >= m)) return 0;
- for (unsigned int i = starti; i < n; i++)
- for (unsigned int j = startj; j < m; j++) {
- if (match == true) {
- if (a[i][i] == what) {
- uI = i; uJ = j; return 1;
- }
- }
- else if (a[i][j] != what) {
- uI = i; uJ = j; return 1;
- }
- }
- return 0;
- }
- /*
- Міняє в матриці a[n][m] рядки з номерами x1 и x2 місцями
- */
- void swaprows (double **a, int n, int m, unsigned int x1, unsigned int x2) {
- if ((!n) || (!m)) return;
- if ((x1 >= n) || (x2 >= n) || (x1 == x2)) return;
- double tmp;
- for (unsigned int x = 0; x < m; x++) {
- tmp = a[x1][x];
- a[x1][x] = a[x2][x];
- a[x2][x] = tmp;
- }
- return;
- };
- /*
- Міняє в матриці a[n][m] стовпці з номерами x1 и x2 місцями
- */
- void swapcolumns (double **a, int n, int m, unsigned int x1, unsigned int x2) {
- if ((!n) || (!m)) return;
- if ((x1 >= m) || (x2 >= m) || (x1 == x2)) return;
- double tmp;
- for (unsigned int x = 0; x < n; x++) {
- tmp = a[x][x1];
- a[x][x1] = a[x][x2];
- a[x][x2] = tmp;
- }
- return;
- };
- /*
- Обчислення визначника квадратної матриці a[n][n]
- */
- /////////////////////// =>
- double determinant (double **a, unsigned int n) {
- unsigned int m = n;
- if (m == 0) return 0;
- if (m == 1) return a[0][0];
- if (m == 2) return (a[0][0] * a[1][1] - a[1][0] * a[0][1]);
- bool sign = false; // зміна знака визначника. По замовчуванні - не міняємо знак.
- double det = 1; // визначник
- double tmp;
- unsigned int x, y;
- for (unsigned int i = 0; i < n; i++) { // цикл по цілій, головній діагоналі
- if (a[i][i] == 0) { // якщо елемент на діагоналі рівний 0, то шукаємо ненульовий елемент в матриці
- if (!search(a,m,n,0, false, y, x, i, i)) return 0; // якщо всі елементи нульові - тоді визначник = 0
- if (i != y) { // міняємо i-ий рядоком y-им
- swaprows(a,m,n,i, y);
- sign = !sign;
- }
- if (i != x) { // міняємо i-ий стовпець з x-им
- swapcolumns(a,m,n,i, x);
- sign = !sign;
- }
- // таким чином, в a[i][i], тепер ненульовий елемент.
- }
- // виносимо елемент a[i][i] за визначник
- det *= a[i][i];
- tmp = a[i][i];
- for (x = i; x < m; x++) {
- a[i][x] = a[i][x] / tmp;
- }
- // таким чином a[i][i] тепер рівен 1
- // зануляємо всі елементи, які стоять під (i, i)-им,
- for (y = i + 1; y < n; y++) {
- tmp = a[y][i];
- for (x = i; x < m; x++)
- a[y][x] -= (a[i][x]*tmp);
- }
- }
- if (sign) return det*(-1);
- return det;
- };
- /* тут ми створюємо захист! Вивід повідомлення про помилку,
- якщо користувач ввів букви а не цифри;
- */
- static String^ oldVal = "";
- // для головної матриці;
- private: System::Void matrixGridView_CellValueChanged(System::Object^ sender, System::Windows::Forms::DataGridViewCellEventArgs^ e) {
- if ((e->RowIndex >= 0) && (e->ColumnIndex >= 0)
- && (e->RowIndex < matrixGridView->Rows->Count) && (e->ColumnIndex < matrixGridView->Rows[e->RowIndex]->Cells->Count))
- {
- String^ value = (String^)matrixGridView->Rows[e->RowIndex]->Cells[e->ColumnIndex]->Value;//Отримуємо нове значення
- //Перевіряємо value
- bool bad = value != "123";//приклад
- if (bad)
- matrixGridView->Rows[e->RowIndex]->Cells[e->ColumnIndex]->Value = oldVal; //Повертаємо старе значення
- }
- }
- private: System::Void matrixGridView_CellEnter(System::Object^ sender, System::Windows::Forms::DataGridViewCellEventArgs^ e) {
- if ((e->RowIndex >= 0) && (e->ColumnIndex >= 0)
- && (e->RowIndex < matrixGridView->Rows->Count) && (e->ColumnIndex < matrixGridView->Rows[e->RowIndex]->Cells->Count))
- oldVal = (String^)matrixGridView->Rows[e->RowIndex]->Cells[e->ColumnIndex]->Value; //Зберігаємо старе значення
- }
- // для вектора значень;
- private: System::Void rightPartGridView_CellValueChanged(System::Object^ sender, System::Windows::Forms::DataGridViewCellEventArgs^ e) {
- if ((e->RowIndex >= 0) && (e->ColumnIndex >= 0)
- && (e->RowIndex < rightPartGridView->Rows->Count) && (e->ColumnIndex < rightPartGridView->Rows[e->RowIndex]->Cells->Count))
- {
- String^ value = (String^)rightPartGridView->Rows[e->RowIndex]->Cells[e->ColumnIndex]->Value;//Отримуємо нове значення
- //Перевіряємо value
- bool bad = value != "123";//приклад
- if (bad)
- rightPartGridView->Rows[e->RowIndex]->Cells[e->ColumnIndex]->Value = oldVal; //Повертаємо старе значення
- } }
- private: System::Void rightPartGridView_CellEnter(System::Object^ sender, System::Windows::Forms::DataGridViewCellEventArgs^ e) {
- if ((e->RowIndex >= 0) && (e->ColumnIndex >= 0)
- && (e->RowIndex < rightPartGridView->Rows->Count) && (e->ColumnIndex < rightPartGridView->Rows[e->RowIndex]->Cells->Count))
- oldVal = (String^)rightPartGridView->Rows[e->RowIndex]->Cells[e->ColumnIndex]->Value; //Зберігаємо старе значення
- }
- };
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement