Advertisement
Guest User

Untitled

a guest
Jan 30th, 2015
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 27.13 KB | None | 0 0
  1. #pragma once
  2.  
  3. namespace Project2 {
  4.  
  5.     using namespace System;
  6.     using namespace System::ComponentModel;
  7.     using namespace System::Collections;
  8.     using namespace System::Windows::Forms;
  9.     using namespace System::Data;
  10.     using namespace System::Drawing;
  11.  
  12.     /// <summary>
  13.     /// Summary for MainForm
  14.     /// </summary>
  15.     public ref class MainForm : public System::Windows::Forms::Form
  16.     {
  17.     public:
  18.         MainForm(void)
  19.         {
  20.             InitializeComponent();
  21.             //
  22.             //TODO: Add the constructor code here
  23.             //
  24.         }
  25.  
  26.     protected:
  27.         /// <summary>
  28.         /// Clean up any resources being used.
  29.         /// </summary>
  30.         ~MainForm()
  31.         {
  32.             if (components)
  33.             {
  34.                 delete components;
  35.             }
  36.         }
  37.     private: System::Windows::Forms::DataGridView^  matrixGridView;
  38.     protected:
  39.  
  40.     protected:
  41.  
  42.  
  43.  
  44.  
  45.     private: System::Windows::Forms::Label^  matrixCapture;
  46.     private: System::Windows::Forms::Button^  calculateButton;
  47.     private: System::Windows::Forms::DataGridView^  rightPartGridView;
  48.  
  49.  
  50.     private: System::Windows::Forms::Label^  equalLabel;
  51.     private: System::Windows::Forms::Label^  taskLabel;
  52.     private: System::Windows::Forms::Label^  rightPartLabel;
  53.     private: System::Windows::Forms::DataGridViewTextBoxColumn^  tableColumn1;
  54.     private: System::Windows::Forms::DataGridViewTextBoxColumn^  tableColumn2;
  55.     private: System::Windows::Forms::DataGridViewTextBoxColumn^  tableColumn3;
  56.     private: System::Windows::Forms::DataGridViewTextBoxColumn^  tableColumn4;
  57.     private: System::Windows::Forms::DataGridViewTextBoxColumn^  rightPartColumn;
  58.  
  59.     private:
  60.         /// <summary>
  61.         /// Required designer variable.
  62.         /// </summary>
  63.         System::ComponentModel::Container ^components;
  64.  
  65. #pragma region Windows Form Designer generated code
  66.         /// <summary>
  67.         /// Required method for Designer support - do not modify
  68.         /// the contents of this method with the code editor.
  69.         /// </summary>
  70.         void InitializeComponent(void)
  71.         {
  72.             this->matrixGridView = (gcnew System::Windows::Forms::DataGridView());
  73.             this->tableColumn1 = (gcnew System::Windows::Forms::DataGridViewTextBoxColumn());
  74.             this->tableColumn2 = (gcnew System::Windows::Forms::DataGridViewTextBoxColumn());
  75.             this->tableColumn3 = (gcnew System::Windows::Forms::DataGridViewTextBoxColumn());
  76.             this->tableColumn4 = (gcnew System::Windows::Forms::DataGridViewTextBoxColumn());
  77.             this->matrixCapture = (gcnew System::Windows::Forms::Label());
  78.             this->calculateButton = (gcnew System::Windows::Forms::Button());
  79.             this->rightPartGridView = (gcnew System::Windows::Forms::DataGridView());
  80.             this->rightPartColumn = (gcnew System::Windows::Forms::DataGridViewTextBoxColumn());
  81.             this->equalLabel = (gcnew System::Windows::Forms::Label());
  82.             this->taskLabel = (gcnew System::Windows::Forms::Label());
  83.             this->rightPartLabel = (gcnew System::Windows::Forms::Label());
  84.             (cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->matrixGridView))->BeginInit();
  85.             (cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->rightPartGridView))->BeginInit();
  86.             this->SuspendLayout();
  87.             //
  88.             // matrixGridView
  89.             //
  90.             this->matrixGridView->AllowUserToAddRows = false;
  91.             this->matrixGridView->AllowUserToDeleteRows = false;
  92.             this->matrixGridView->ColumnHeadersHeightSizeMode = System::Windows::Forms::DataGridViewColumnHeadersHeightSizeMode::AutoSize;
  93.             this->matrixGridView->Columns->AddRange(gcnew cli::array< System::Windows::Forms::DataGridViewColumn^  >(4) {this->tableColumn1,
  94.                 this->tableColumn2, this->tableColumn3, this->tableColumn4});
  95.             this->matrixGridView->Location = System::Drawing::Point(12, 57);
  96.             this->matrixGridView->Name = L"matrixGridView";
  97.             this->matrixGridView->Size = System::Drawing::Size(283, 111);
  98.             this->matrixGridView->TabIndex = 0;
  99.             this->matrixGridView->CellEnter += gcnew System::Windows::Forms::DataGridViewCellEventHandler(this, &MainForm::matrixGridView_CellEnter);
  100.             this->matrixGridView->CellValueChanged += gcnew System::Windows::Forms::DataGridViewCellEventHandler(this, &MainForm::matrixGridView_CellValueChanged);
  101.             //
  102.             // tableColumn1
  103.             //
  104.             this->tableColumn1->HeaderText = L"[x1]";
  105.             this->tableColumn1->Name = L"tableColumn1";
  106.             this->tableColumn1->Width = 60;
  107.             //
  108.             // tableColumn2
  109.             //
  110.             this->tableColumn2->HeaderText = L"[x2]";
  111.             this->tableColumn2->Name = L"tableColumn2";
  112.             this->tableColumn2->Width = 60;
  113.             //
  114.             // tableColumn3
  115.             //
  116.             this->tableColumn3->HeaderText = L"[x3]";
  117.             this->tableColumn3->Name = L"tableColumn3";
  118.             this->tableColumn3->Width = 60;
  119.             //
  120.             // tableColumn4
  121.             //
  122.             this->tableColumn4->HeaderText = L"[x4]";
  123.             this->tableColumn4->Name = L"tableColumn4";
  124.             this->tableColumn4->Width = 60;
  125.             //
  126.             // matrixCapture
  127.             //
  128.             this->matrixCapture->AutoSize = true;
  129.             this->matrixCapture->Location = System::Drawing::Point(9, 41);
  130.             this->matrixCapture->Name = L"matrixCapture";
  131.             this->matrixCapture->Size = System::Drawing::Size(124, 13);
  132.             this->matrixCapture->TabIndex = 1;
  133.             this->matrixCapture->Text = L"Матриця рівняння (4x4)";
  134.             //
  135.             // calculateButton
  136.             //
  137.             this->calculateButton->Location = System::Drawing::Point(343, 174);
  138.             this->calculateButton->Name = L"calculateButton";
  139.             this->calculateButton->Size = System::Drawing::Size(104, 23);
  140.             this->calculateButton->TabIndex = 2;
  141.             this->calculateButton->Text = L"Знайти розв\'язки";
  142.             this->calculateButton->UseVisualStyleBackColor = true;
  143.             this->calculateButton->Click += gcnew System::EventHandler(this, &MainForm::calculateButton_Click);
  144.             //
  145.             // rightPartGridView
  146.             //
  147.             this->rightPartGridView->AllowUserToAddRows = false;
  148.             this->rightPartGridView->AllowUserToDeleteRows = false;
  149.             this->rightPartGridView->ColumnHeadersHeightSizeMode = System::Windows::Forms::DataGridViewColumnHeadersHeightSizeMode::AutoSize;
  150.             this->rightPartGridView->Columns->AddRange(gcnew cli::array< System::Windows::Forms::DataGridViewColumn^  >(1) {this->rightPartColumn});
  151.             this->rightPartGridView->Location = System::Drawing::Point(343, 57);
  152.             this->rightPartGridView->Name = L"rightPartGridView";
  153.             this->rightPartGridView->Size = System::Drawing::Size(104, 111);
  154.             this->rightPartGridView->TabIndex = 3;
  155.             this->rightPartGridView->CellEnter += gcnew System::Windows::Forms::DataGridViewCellEventHandler(this, &MainForm::rightPartGridView_CellEnter);
  156.             this->rightPartGridView->CellValueChanged += gcnew System::Windows::Forms::DataGridViewCellEventHandler(this, &MainForm::rightPartGridView_CellValueChanged);
  157.             //
  158.             // rightPartColumn
  159.             //
  160.             this->rightPartColumn->HeaderText = L"[y]";
  161.             this->rightPartColumn->Name = L"rightPartColumn";
  162.             this->rightPartColumn->Width = 60;
  163.             //
  164.             // equalLabel
  165.             //
  166.             this->equalLabel->AutoSize = true;
  167.             this->equalLabel->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 25, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
  168.                 static_cast<System::Byte>(204)));
  169.             this->equalLabel->Location = System::Drawing::Point(301, 100);
  170.             this->equalLabel->Name = L"equalLabel";
  171.             this->equalLabel->Size = System::Drawing::Size(37, 39);
  172.             this->equalLabel->TabIndex = 4;
  173.             this->equalLabel->Text = L"=";
  174.             //
  175.             // taskLabel
  176.             //
  177.             this->taskLabel->AutoSize = true;
  178.             this->taskLabel->Location = System::Drawing::Point(9, 9);
  179.             this->taskLabel->Name = L"taskLabel";
  180.             this->taskLabel->Size = System::Drawing::Size(267, 13);
  181.             this->taskLabel->TabIndex = 5;
  182.             this->taskLabel->Text = L"За допомогою правила Крамера розв’язати СЛАР:";
  183.             //
  184.             // rightPartLabel
  185.             //
  186.             this->rightPartLabel->AutoSize = true;
  187.             this->rightPartLabel->Location = System::Drawing::Point(340, 41);
  188.             this->rightPartLabel->Name = L"rightPartLabel";
  189.             this->rightPartLabel->Size = System::Drawing::Size(113, 13);
  190.             this->rightPartLabel->TabIndex = 6;
  191.             this->rightPartLabel->Text = L"Вектор значень (4x1)";
  192.             //
  193.             // MainForm
  194.             //
  195.             this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
  196.             this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
  197.             this->ClientSize = System::Drawing::Size(460, 207);
  198.             this->Controls->Add(this->rightPartLabel);
  199.             this->Controls->Add(this->taskLabel);
  200.             this->Controls->Add(this->equalLabel);
  201.             this->Controls->Add(this->rightPartGridView);
  202.             this->Controls->Add(this->calculateButton);
  203.             this->Controls->Add(this->matrixCapture);
  204.             this->Controls->Add(this->matrixGridView);
  205.             this->Name = L"MainForm";
  206.             this->Text = L"Розрахункова (завдання 2)";
  207.             this->Load += gcnew System::EventHandler(this, &MainForm::MainForm_Load);
  208.             (cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->matrixGridView))->EndInit();
  209.             (cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->rightPartGridView))->EndInit();
  210.             this->ResumeLayout(false);
  211.             this->PerformLayout();
  212.  
  213.         }
  214. #pragma endregion
  215.  
  216.         /*
  217.         Початкова ініціалізація форми
  218.         */
  219.     private: System::Void MainForm_Load(System::Object^  sender, System::EventArgs^  e) {
  220.                  /*
  221.                  Ініціалізація головної матриці
  222.                  */
  223.                  initializeMatrixGridView(true);
  224.  
  225.                  /*
  226.                  Ініціалізація вектора правої частини системи рівнянь
  227.                  */
  228.                  initializeRightPartGridView(true);
  229.  
  230.              }
  231.  
  232.              System::Void initializeMatrixGridView(bool values){
  233.                  {                 
  234.                      DataGridViewRow ^ row = gcnew DataGridViewRow;
  235.                      row->CreateCells(matrixGridView);
  236.                      if (values){
  237.                          row->Cells[0]->Value=-1;
  238.                          row->Cells[1]->Value=2;
  239.                          row->Cells[2]->Value=1;
  240.                          row->Cells[3]->Value=1;
  241.                      }
  242.                      matrixGridView->Rows->Add(row);
  243.                  }
  244.                  {                 
  245.                      DataGridViewRow ^ row = gcnew DataGridViewRow;
  246.                      row->CreateCells(matrixGridView);
  247.                      if (values){
  248.                          row->Cells[0]->Value=-4;
  249.                          row->Cells[1]->Value=5;
  250.                          row->Cells[2]->Value=3;
  251.                          row->Cells[3]->Value=2;
  252.                      }
  253.                      matrixGridView->Rows->Add(row);
  254.                  }
  255.                  {                 
  256.                      DataGridViewRow ^ row = gcnew DataGridViewRow;
  257.                      row->CreateCells(matrixGridView);
  258.                      if (values){
  259.                          row->Cells[0]->Value=1;
  260.                          row->Cells[1]->Value=4;
  261.                          row->Cells[2]->Value=2;
  262.                          row->Cells[3]->Value=1;
  263.                      }
  264.                      matrixGridView->Rows->Add(row);
  265.                  }
  266.                  {                 
  267.                      DataGridViewRow ^ row = gcnew DataGridViewRow;
  268.                      row->CreateCells(matrixGridView);
  269.                      if (values){
  270.                          row->Cells[0]->Value=2;
  271.                          row->Cells[1]->Value=-1;
  272.                          row->Cells[2]->Value=1;
  273.                          row->Cells[3]->Value=4;
  274.                      }
  275.                      matrixGridView->Rows->Add(row);
  276.                  }
  277.  
  278.              };
  279.  
  280.              System::Void initializeRightPartGridView(bool values){
  281.                  {
  282.                      DataGridViewRow ^ row = gcnew DataGridViewRow;
  283.                      row->CreateCells(rightPartGridView);
  284.                      if (values){
  285.                          row->Cells[0]->Value=-3;                      
  286.                      }
  287.                      rightPartGridView->Rows->Add(row);
  288.                  }
  289.                  {
  290.                      DataGridViewRow ^ row = gcnew DataGridViewRow;
  291.                      row->CreateCells(rightPartGridView);
  292.                      if (values){
  293.                          row->Cells[0]->Value=0;                       
  294.                      }
  295.                      rightPartGridView->Rows->Add(row);
  296.                  }
  297.                  {
  298.                      DataGridViewRow ^ row = gcnew DataGridViewRow;
  299.                      row->CreateCells(rightPartGridView);
  300.                      if (values){
  301.                          row->Cells[0]->Value=3;                       
  302.                      }
  303.                      rightPartGridView->Rows->Add(row);
  304.                  }
  305.                  {
  306.                      DataGridViewRow ^ row = gcnew DataGridViewRow;
  307.                      row->CreateCells(rightPartGridView);
  308.                      if (values){
  309.                          row->Cells[0]->Value=-6;                      
  310.                      }
  311.                      rightPartGridView->Rows->Add(row);
  312.                  }
  313.  
  314.              };
  315.  
  316.              /*
  317.              Знаходження розвязку
  318.              */
  319.     private: System::Void calculateButton_Click(System::Object^  sender, System::EventArgs^  e) {
  320.                  double delta = getDelta(getMainMatrix());
  321.                  double delta1 = getDelta(getMatrixForDelta1());
  322.                  double delta2 = getDelta(getMatrixForDelta2());
  323.                  double delta3 = getDelta(getMatrixForDelta3());
  324.                  double delta4 = getDelta(getMatrixForDelta4());
  325.  
  326.                  double x1 = delta1/delta;
  327.                  double x2 = delta2/delta;
  328.                  double x3 = delta3/delta;
  329.                  double x4 = delta4/delta;
  330.  
  331.                  String ^ res =  "Результат: " + "x1="+x1 + "; " + "x2="+x2 + "; " + "x3="+x3 + "; " + "x4="+x4 + "; ";
  332.                  MessageBox::Show(res,"Результат:",MessageBoxButtons::OK, MessageBoxIcon::Information);
  333.              }
  334.  
  335.              /*
  336.              Створення пустої матриці розміром 4 на 4
  337.              */
  338.              double ** createEmptyMatrix(){
  339.                  const int n=4;
  340.                  double **a = new double * [n];
  341.                  for (int i=0; i<n; i++){
  342.                      a[i] = new double [n];
  343.                  }
  344.                  return a;
  345.              }
  346.  
  347.              double ** getMainMatrix(){
  348.                  double **a = createEmptyMatrix();
  349.                  /* відбувається заповнення матриці числами, що є в основній матриці
  350.                  (це потрібно для пошуку delta);
  351.                  */
  352.                  a[0][0] = Convert::ToDouble(matrixGridView->Rows[0]->Cells[0]->Value);
  353.                  a[0][1] = Convert::ToDouble(matrixGridView->Rows[0]->Cells[1]->Value);
  354.                  a[0][2] = Convert::ToDouble(matrixGridView->Rows[0]->Cells[2]->Value);
  355.                  a[0][3] = Convert::ToDouble(matrixGridView->Rows[0]->Cells[3]->Value);
  356.  
  357.                  a[1][0] = Convert::ToDouble(matrixGridView->Rows[1]->Cells[0]->Value);
  358.                  a[1][1] = Convert::ToDouble(matrixGridView->Rows[1]->Cells[1]->Value);
  359.                  a[1][2] = Convert::ToDouble(matrixGridView->Rows[1]->Cells[2]->Value);
  360.                  a[1][3] = Convert::ToDouble(matrixGridView->Rows[1]->Cells[3]->Value);
  361.  
  362.                  a[2][0] = Convert::ToDouble(matrixGridView->Rows[2]->Cells[0]->Value);
  363.                  a[2][1] = Convert::ToDouble(matrixGridView->Rows[2]->Cells[1]->Value);
  364.                  a[2][2] = Convert::ToDouble(matrixGridView->Rows[2]->Cells[2]->Value);
  365.                  a[2][3] = Convert::ToDouble(matrixGridView->Rows[2]->Cells[3]->Value);
  366.  
  367.                  a[3][0] = Convert::ToDouble(matrixGridView->Rows[3]->Cells[0]->Value);
  368.                  a[3][1] = Convert::ToDouble(matrixGridView->Rows[3]->Cells[1]->Value);
  369.                  a[3][2] = Convert::ToDouble(matrixGridView->Rows[3]->Cells[2]->Value);
  370.                  a[3][3] = Convert::ToDouble(matrixGridView->Rows[3]->Cells[3]->Value);
  371.  
  372.                  return a;
  373.              }
  374.  
  375.              double ** getMatrixForDelta1(){
  376.                  double **a = createEmptyMatrix();
  377.                  /* відбувається заповнення матриці числами, що є в основній матриці,
  378.                  але перший стовпець чисел міняється на вектор значень (це потрібно для пошуку delta1);
  379.                  */
  380.                  a[0][0] = Convert::ToDouble(rightPartGridView->Rows[0]->Cells[0]->Value);
  381.                  a[0][1] = Convert::ToDouble(matrixGridView->Rows[0]->Cells[1]->Value);
  382.                  a[0][2] = Convert::ToDouble(matrixGridView->Rows[0]->Cells[2]->Value);
  383.                  a[0][3] = Convert::ToDouble(matrixGridView->Rows[0]->Cells[3]->Value);
  384.  
  385.                  a[1][0] = Convert::ToDouble(rightPartGridView->Rows[1]->Cells[0]->Value);
  386.                  a[1][1] = Convert::ToDouble(matrixGridView->Rows[1]->Cells[1]->Value);
  387.                  a[1][2] = Convert::ToDouble(matrixGridView->Rows[1]->Cells[2]->Value);
  388.                  a[1][3] = Convert::ToDouble(matrixGridView->Rows[1]->Cells[3]->Value);
  389.  
  390.                  a[2][0] = Convert::ToDouble(rightPartGridView->Rows[2]->Cells[0]->Value);
  391.                  a[2][1] = Convert::ToDouble(matrixGridView->Rows[2]->Cells[1]->Value);
  392.                  a[2][2] = Convert::ToDouble(matrixGridView->Rows[2]->Cells[2]->Value);
  393.                  a[2][3] = Convert::ToDouble(matrixGridView->Rows[2]->Cells[3]->Value);
  394.  
  395.                  a[3][0] = Convert::ToDouble(rightPartGridView->Rows[3]->Cells[0]->Value);
  396.                  a[3][1] = Convert::ToDouble(matrixGridView->Rows[3]->Cells[1]->Value);
  397.                  a[3][2] = Convert::ToDouble(matrixGridView->Rows[3]->Cells[2]->Value);
  398.                  a[3][3] = Convert::ToDouble(matrixGridView->Rows[3]->Cells[3]->Value);
  399.  
  400.                  return a;
  401.              }
  402.  
  403.              double ** getMatrixForDelta2(){
  404.                  double **a = createEmptyMatrix();
  405.                  /* відбувається заповнення матриці числами, що є в основній матриці,
  406.                  але другий стовпець чисел міняється на вектор значень (це потрібно для пошуку delta2);
  407.                  */
  408.                  a[0][0] = Convert::ToDouble(matrixGridView->Rows[0]->Cells[0]->Value);
  409.                  a[0][1] = Convert::ToDouble(rightPartGridView->Rows[0]->Cells[0]->Value);
  410.                  a[0][2] = Convert::ToDouble(matrixGridView->Rows[0]->Cells[2]->Value);
  411.                  a[0][3] = Convert::ToDouble(matrixGridView->Rows[0]->Cells[3]->Value);
  412.  
  413.                  a[1][0] = Convert::ToDouble(matrixGridView->Rows[1]->Cells[0]->Value);
  414.                  a[1][1] = Convert::ToDouble(rightPartGridView->Rows[1]->Cells[0]->Value);
  415.                  a[1][2] = Convert::ToDouble(matrixGridView->Rows[1]->Cells[2]->Value);
  416.                  a[1][3] = Convert::ToDouble(matrixGridView->Rows[1]->Cells[3]->Value);
  417.  
  418.                  a[2][0] = Convert::ToDouble(matrixGridView->Rows[2]->Cells[0]->Value);
  419.                  a[2][1] = Convert::ToDouble(rightPartGridView->Rows[2]->Cells[0]->Value);
  420.                  a[2][2] = Convert::ToDouble(matrixGridView->Rows[2]->Cells[2]->Value);
  421.                  a[2][3] = Convert::ToDouble(matrixGridView->Rows[2]->Cells[3]->Value);
  422.  
  423.                  a[3][0] = Convert::ToDouble(matrixGridView->Rows[3]->Cells[0]->Value);
  424.                  a[3][1] = Convert::ToDouble(rightPartGridView->Rows[3]->Cells[0]->Value);
  425.                  a[3][2] = Convert::ToDouble(matrixGridView->Rows[3]->Cells[2]->Value);
  426.                  a[3][3] = Convert::ToDouble(matrixGridView->Rows[3]->Cells[3]->Value);
  427.  
  428.                  return a;
  429.              }
  430.  
  431.              double ** getMatrixForDelta3(){
  432.                  double **a = createEmptyMatrix();
  433.                  /*відбувається заповнення матриці числами, що є в основній матриці,
  434.                  але третій стовпець чисел міняється на вектор значень (це потрібно для пошуку delta3);
  435.                  */
  436.                  a[0][0] = Convert::ToDouble(matrixGridView->Rows[0]->Cells[0]->Value);
  437.                  a[0][1] = Convert::ToDouble(matrixGridView->Rows[0]->Cells[1]->Value);
  438.                  a[0][2] = Convert::ToDouble(rightPartGridView->Rows[0]->Cells[0]->Value);
  439.                  a[0][3] = Convert::ToDouble(matrixGridView->Rows[0]->Cells[3]->Value);
  440.  
  441.                  a[1][0] = Convert::ToDouble(matrixGridView->Rows[1]->Cells[0]->Value);
  442.                  a[1][1] = Convert::ToDouble(matrixGridView->Rows[1]->Cells[1]->Value);
  443.                  a[1][2] = Convert::ToDouble(rightPartGridView->Rows[1]->Cells[0]->Value);
  444.                  a[1][3] = Convert::ToDouble(matrixGridView->Rows[1]->Cells[3]->Value);
  445.  
  446.                  a[2][0] = Convert::ToDouble(matrixGridView->Rows[2]->Cells[0]->Value);
  447.                  a[2][1] = Convert::ToDouble(matrixGridView->Rows[2]->Cells[1]->Value);
  448.                  a[2][2] = Convert::ToDouble(rightPartGridView->Rows[2]->Cells[0]->Value);
  449.                  a[2][3] = Convert::ToDouble(matrixGridView->Rows[2]->Cells[3]->Value);
  450.  
  451.                  a[3][0] = Convert::ToDouble(matrixGridView->Rows[3]->Cells[0]->Value);
  452.                  a[3][1] = Convert::ToDouble(matrixGridView->Rows[3]->Cells[1]->Value);
  453.                  a[3][2] = Convert::ToDouble(rightPartGridView->Rows[3]->Cells[0]->Value);
  454.                  a[3][3] = Convert::ToDouble(matrixGridView->Rows[3]->Cells[3]->Value);
  455.  
  456.                  return a;
  457.              }
  458.  
  459.              double ** getMatrixForDelta4(){
  460.                  double **a = createEmptyMatrix();
  461.                  /* відбувається заповнення матриці числами, що є в основній матриці,
  462.                  але четвертий стовпець чисел міняється на вектор значень (це потрібно для пошуку delta4);
  463.                  */
  464.                  a[0][0] = Convert::ToDouble(matrixGridView->Rows[0]->Cells[0]->Value);
  465.                  a[0][1] = Convert::ToDouble(matrixGridView->Rows[0]->Cells[1]->Value);
  466.                  a[0][2] = Convert::ToDouble(matrixGridView->Rows[0]->Cells[2]->Value);
  467.                  a[0][3] = Convert::ToDouble(rightPartGridView->Rows[0]->Cells[0]->Value);
  468.  
  469.                  a[1][0] = Convert::ToDouble(matrixGridView->Rows[1]->Cells[0]->Value);
  470.                  a[1][1] = Convert::ToDouble(matrixGridView->Rows[1]->Cells[1]->Value);
  471.                  a[1][2] = Convert::ToDouble(matrixGridView->Rows[1]->Cells[2]->Value);
  472.                  a[1][3] = Convert::ToDouble(rightPartGridView->Rows[1]->Cells[0]->Value);
  473.  
  474.                  a[2][0] = Convert::ToDouble(matrixGridView->Rows[2]->Cells[0]->Value);
  475.                  a[2][1] = Convert::ToDouble(matrixGridView->Rows[2]->Cells[1]->Value);
  476.                  a[2][2] = Convert::ToDouble(matrixGridView->Rows[2]->Cells[2]->Value);
  477.                  a[2][3] = Convert::ToDouble(rightPartGridView->Rows[2]->Cells[0]->Value);
  478.  
  479.                  a[3][0] = Convert::ToDouble(matrixGridView->Rows[3]->Cells[0]->Value);
  480.                  a[3][1] = Convert::ToDouble(matrixGridView->Rows[3]->Cells[1]->Value);
  481.                  a[3][2] = Convert::ToDouble(matrixGridView->Rows[3]->Cells[2]->Value);
  482.                  a[3][3] = Convert::ToDouble(rightPartGridView->Rows[3]->Cells[0]->Value);
  483.  
  484.                  return a;
  485.              }
  486.              ////////////////////////////////////////////////
  487.              double getDelta(double **a){
  488.                  return determinant(a,4); // виклик функції determinant =>
  489.              }
  490.  
  491.  
  492.              /*
  493.              Пошук в матриці a[m][n] елемента з вказаним значенням
  494.              Повертається його номер рядка і стовпця uI, uJ, якщо елемент знайдено.
  495.              match - шукати рівний елемент, або відмінний від вказаного.
  496.              Поверне 0 - якщо не знайдено, не 0 - якщо знайдено
  497.              */
  498.              int search (double **a, int m, int n, double what,
  499.                  bool match, unsigned int &uI, unsigned int &uJ, unsigned int starti, unsigned int startj) {                   
  500.                      if ((!m) || (!n)) return 0;
  501.                      if ((starti >= n) || (startj >= m)) return 0;
  502.                      for (unsigned int i = starti; i < n; i++)
  503.                          for (unsigned int j = startj; j < m; j++) {
  504.                              if (match == true) {
  505.                                  if (a[i][i] == what) {
  506.                                      uI = i; uJ = j; return 1;
  507.                                  }
  508.                              }
  509.                              else if (a[i][j] != what) {
  510.                                  uI = i; uJ = j; return 1;
  511.                              }
  512.                          }
  513.                          return 0;
  514.              }
  515.  
  516.              /*
  517.              Міняє в матриці a[n][m] рядки з номерами x1 и x2 місцями
  518.              */
  519.              void swaprows (double **a, int n, int m, unsigned int x1, unsigned int x2) {
  520.                  if ((!n) || (!m)) return;
  521.                  if ((x1 >= n) || (x2 >= n) || (x1 == x2)) return;
  522.                  double tmp;
  523.                  for (unsigned int x = 0; x < m; x++) {
  524.                      tmp = a[x1][x];
  525.                      a[x1][x] = a[x2][x];
  526.                      a[x2][x] = tmp;
  527.                  }
  528.                  return;
  529.              };
  530.  
  531.              /*
  532.              Міняє в матриці a[n][m] стовпці з номерами x1 и x2 місцями
  533.              */
  534.              void swapcolumns (double **a, int n, int m, unsigned int x1, unsigned int x2) {
  535.                  if ((!n) || (!m)) return;
  536.                  if ((x1 >= m) || (x2 >= m) || (x1 == x2)) return;
  537.                  double tmp;
  538.                  for (unsigned int x = 0; x < n; x++) {
  539.                      tmp = a[x][x1];
  540.                      a[x][x1] = a[x][x2];
  541.                      a[x][x2] = tmp;
  542.                  }
  543.                  return;
  544.              };
  545.  
  546.              /*
  547.              Обчислення визначника квадратної матриці a[n][n]
  548.              */
  549.             /////////////////////// =>
  550.              double determinant (double **a, unsigned int n) {
  551.                  unsigned int m = n;
  552.                  if (m == 0) return 0;
  553.                  if (m == 1) return a[0][0];
  554.                  if (m == 2) return (a[0][0] * a[1][1] - a[1][0] * a[0][1]);
  555.                  bool sign = false; // зміна знака визначника. По замовчуванні - не міняємо знак.
  556.                  double det = 1; // визначник
  557.                  double tmp;
  558.                  unsigned int x, y;
  559.                  for (unsigned int i = 0; i < n; i++) { // цикл по цілій, головній діагоналі
  560.                      if (a[i][i] == 0) { // якщо елемент на діагоналі рівний 0, то шукаємо ненульовий елемент в матриці
  561.                          if (!search(a,m,n,0, false, y, x, i, i)) return 0; // якщо всі елементи нульові - тоді визначник = 0
  562.                          if (i != y) { // міняємо i-ий рядоком y-им
  563.                              swaprows(a,m,n,i, y);
  564.                              sign = !sign;
  565.                          }
  566.                          if (i != x) { // міняємо i-ий стовпець з x-им
  567.                              swapcolumns(a,m,n,i, x);
  568.                              sign = !sign;
  569.                          }
  570.                          // таким чином, в a[i][i], тепер ненульовий елемент.
  571.                      }
  572.                      // виносимо елемент a[i][i] за визначник
  573.                      det *= a[i][i];
  574.                      tmp = a[i][i];
  575.                      for (x = i; x < m; x++) {
  576.                          a[i][x] = a[i][x] / tmp;
  577.                      }
  578.                      // таким чином a[i][i] тепер рівен 1
  579.                      // зануляємо всі елементи, які стоять під (i, i)-им,
  580.                      for (y = i + 1; y < n; y++) {
  581.                          tmp = a[y][i];
  582.                          for (x = i; x < m; x++)
  583.                              a[y][x] -= (a[i][x]*tmp);
  584.                      }
  585.                  }
  586.                  if (sign) return det*(-1);
  587.                  return det;
  588.              };          
  589.              /* тут ми створюємо захист! Вивід повідомлення про помилку,
  590.              якщо користувач ввів букви а не цифри;
  591.              */
  592.              static String^ oldVal = "";
  593.              // для головної матриці;
  594.     private: System::Void matrixGridView_CellValueChanged(System::Object^  sender, System::Windows::Forms::DataGridViewCellEventArgs^  e) {
  595.                  if ((e->RowIndex >= 0) && (e->ColumnIndex >= 0)
  596.                      && (e->RowIndex < matrixGridView->Rows->Count) && (e->ColumnIndex < matrixGridView->Rows[e->RowIndex]->Cells->Count))
  597.                  {
  598.                      String^ value = (String^)matrixGridView->Rows[e->RowIndex]->Cells[e->ColumnIndex]->Value;//Отримуємо нове значення
  599.                      //Перевіряємо value
  600.                      bool bad = value != "123";//приклад
  601.                      if (bad)
  602.                          matrixGridView->Rows[e->RowIndex]->Cells[e->ColumnIndex]->Value = oldVal; //Повертаємо старе значення
  603.                  }
  604.              }
  605.  
  606. private: System::Void matrixGridView_CellEnter(System::Object^  sender, System::Windows::Forms::DataGridViewCellEventArgs^  e) {
  607.              if ((e->RowIndex >= 0) && (e->ColumnIndex >= 0)
  608.                  && (e->RowIndex < matrixGridView->Rows->Count) && (e->ColumnIndex < matrixGridView->Rows[e->RowIndex]->Cells->Count))
  609.                  oldVal = (String^)matrixGridView->Rows[e->RowIndex]->Cells[e->ColumnIndex]->Value; //Зберігаємо старе значення
  610.          }
  611.  
  612.  
  613.          // для вектора значень;
  614. private: System::Void rightPartGridView_CellValueChanged(System::Object^  sender, System::Windows::Forms::DataGridViewCellEventArgs^  e) {
  615.              if ((e->RowIndex >= 0) && (e->ColumnIndex >= 0)
  616.                  && (e->RowIndex < rightPartGridView->Rows->Count) && (e->ColumnIndex < rightPartGridView->Rows[e->RowIndex]->Cells->Count))
  617.              {
  618.                  String^ value = (String^)rightPartGridView->Rows[e->RowIndex]->Cells[e->ColumnIndex]->Value;//Отримуємо нове значення
  619.                  //Перевіряємо value
  620.                  bool bad = value != "123";//приклад
  621.                  if (bad)
  622.                      rightPartGridView->Rows[e->RowIndex]->Cells[e->ColumnIndex]->Value = oldVal; //Повертаємо старе значення
  623.              } }
  624.  
  625. private: System::Void rightPartGridView_CellEnter(System::Object^  sender, System::Windows::Forms::DataGridViewCellEventArgs^  e) {
  626.              if ((e->RowIndex >= 0) && (e->ColumnIndex >= 0)
  627.                  && (e->RowIndex < rightPartGridView->Rows->Count) && (e->ColumnIndex < rightPartGridView->Rows[e->RowIndex]->Cells->Count))
  628.                  oldVal = (String^)rightPartGridView->Rows[e->RowIndex]->Cells[e->ColumnIndex]->Value; //Зберігаємо старе значення
  629.          }
  630. }; 
  631. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement