Advertisement
Guest User

Untitled

a guest
May 24th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 16.18 KB | None | 0 0
  1. #pragma once
  2.  
  3. namespace Form1 {
  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.     /// Сводка для MyForm
  14.     /// </summary>
  15.     public ref class MyForm : public System::Windows::Forms::Form
  16.     {
  17.     public:
  18.         MyForm(void)
  19.         {
  20.             InitializeComponent();
  21.             //
  22.             //TODO: добавьте код конструктора
  23.             //
  24.         }
  25.  
  26.     protected:
  27.         /// <summary>
  28.         /// Освободить все используемые ресурсы.
  29.         /// </summary>
  30.         ~MyForm()
  31.         {
  32.             if (components)
  33.             {
  34.                 delete components;
  35.             }
  36.         }
  37.     private: System::Windows::Forms::GroupBox^  groupBox1;
  38.     protected:
  39.     private: System::Windows::Forms::Label^  label1;
  40.     private: System::Windows::Forms::NumericUpDown^  numericUpDown1;
  41.     private: System::Windows::Forms::RadioButton^  radioButton1;
  42.     private: System::Windows::Forms::DataGridView^  dataGridView1;
  43.     private: System::Windows::Forms::Button^  button1;
  44.     private: System::Windows::Forms::MenuStrip^  menuStrip1;
  45.     private: System::Windows::Forms::ToolStripMenuItem^  сгенерироватьМатрицуToolStripMenuItem;
  46.     private: System::Windows::Forms::ToolStripMenuItem^  выводToolStripMenuItem;
  47.     private: System::Windows::Forms::DataGridView^  dataGridView2;
  48.     private: System::Windows::Forms::Label^  label2;
  49.     private: System::Windows::Forms::RadioButton^  radioButton2;
  50.     private: System::Windows::Forms::ToolStripMenuItem^  очиститьToolStripMenuItem;
  51.  
  52.     private:
  53.         /// <summary>
  54.         /// Обязательная переменная конструктора.
  55.         /// </summary>
  56.         System::ComponentModel::Container ^components;
  57.  
  58. #pragma region Windows Form Designer generated code
  59.         /// <summary>
  60.         /// Требуемый метод для поддержки конструктора — не изменяйте
  61.         /// содержимое этого метода с помощью редактора кода.
  62.         /// </summary>
  63.         void InitializeComponent(void)
  64.         {
  65.             this->groupBox1 = (gcnew System::Windows::Forms::GroupBox());
  66.             this->radioButton2 = (gcnew System::Windows::Forms::RadioButton());
  67.             this->label1 = (gcnew System::Windows::Forms::Label());
  68.             this->numericUpDown1 = (gcnew System::Windows::Forms::NumericUpDown());
  69.             this->radioButton1 = (gcnew System::Windows::Forms::RadioButton());
  70.             this->dataGridView1 = (gcnew System::Windows::Forms::DataGridView());
  71.             this->button1 = (gcnew System::Windows::Forms::Button());
  72.             this->menuStrip1 = (gcnew System::Windows::Forms::MenuStrip());
  73.             this->сгенерироватьМатрицуToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
  74.             this->очиститьToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
  75.             this->выводToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
  76.             this->dataGridView2 = (gcnew System::Windows::Forms::DataGridView());
  77.             this->label2 = (gcnew System::Windows::Forms::Label());
  78.             this->groupBox1->SuspendLayout();
  79.             (cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->numericUpDown1))->BeginInit();
  80.             (cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->dataGridView1))->BeginInit();
  81.             this->menuStrip1->SuspendLayout();
  82.             (cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->dataGridView2))->BeginInit();
  83.             this->SuspendLayout();
  84.             //
  85.             // groupBox1
  86.             //
  87.             this->groupBox1->Controls->Add(this->radioButton2);
  88.             this->groupBox1->Controls->Add(this->label1);
  89.             this->groupBox1->Controls->Add(this->numericUpDown1);
  90.             this->groupBox1->Controls->Add(this->radioButton1);
  91.             this->groupBox1->Location = System::Drawing::Point(31, 24);
  92.             this->groupBox1->Name = L"groupBox1";
  93.             this->groupBox1->Size = System::Drawing::Size(502, 114);
  94.             this->groupBox1->TabIndex = 0;
  95.             this->groupBox1->TabStop = false;
  96.             //
  97.             // radioButton2
  98.             //
  99.             this->radioButton2->AutoSize = true;
  100.             this->radioButton2->Location = System::Drawing::Point(189, 20);
  101.             this->radioButton2->Name = L"radioButton2";
  102.             this->radioButton2->Size = System::Drawing::Size(67, 17);
  103.             this->radioButton2->TabIndex = 3;
  104.             this->radioButton2->TabStop = true;
  105.             this->radioButton2->Text = L"Вручную";
  106.             this->radioButton2->UseVisualStyleBackColor = true;
  107.             this->radioButton2->CheckedChanged += gcnew System::EventHandler(this, &MyForm::radioButton2_CheckedChanged);
  108.             //
  109.             // label1
  110.             //
  111.             this->label1->AutoSize = true;
  112.             this->label1->Location = System::Drawing::Point(186, 55);
  113.             this->label1->Name = L"label1";
  114.             this->label1->Size = System::Drawing::Size(123, 13);
  115.             this->label1->TabIndex = 2;
  116.             this->label1->Text = L"Размерность матрицы";
  117.             //
  118.             // numericUpDown1
  119.             //
  120.             this->numericUpDown1->Location = System::Drawing::Point(215, 71);
  121.             this->numericUpDown1->Minimum = System::Decimal(gcnew cli::array< System::Int32 >(4) { 1, 0, 0, 0 });
  122.             this->numericUpDown1->Name = L"numericUpDown1";
  123.             this->numericUpDown1->Size = System::Drawing::Size(67, 20);
  124.             this->numericUpDown1->TabIndex = 1;
  125.             this->numericUpDown1->Value = System::Decimal(gcnew cli::array< System::Int32 >(4) { 1, 0, 0, 0 });
  126.             //
  127.             // radioButton1
  128.             //
  129.             this->radioButton1->AutoSize = true;
  130.             this->radioButton1->Checked = true;
  131.             this->radioButton1->Location = System::Drawing::Point(7, 20);
  132.             this->radioButton1->Name = L"radioButton1";
  133.             this->radioButton1->Size = System::Drawing::Size(87, 17);
  134.             this->radioButton1->TabIndex = 0;
  135.             this->radioButton1->TabStop = true;
  136.             this->radioButton1->Text = L"По формуле";
  137.             this->radioButton1->UseVisualStyleBackColor = true;
  138.             this->radioButton1->CheckedChanged += gcnew System::EventHandler(this, &MyForm::radioButton1_CheckedChanged);
  139.             //
  140.             // dataGridView1
  141.             //
  142.             this->dataGridView1->AllowUserToAddRows = false;
  143.             this->dataGridView1->AllowUserToDeleteRows = false;
  144.             this->dataGridView1->ColumnHeadersHeightSizeMode = System::Windows::Forms::DataGridViewColumnHeadersHeightSizeMode::AutoSize;
  145.             this->dataGridView1->Location = System::Drawing::Point(38, 155);
  146.             this->dataGridView1->Name = L"dataGridView1";
  147.             this->dataGridView1->ReadOnly = true;
  148.             this->dataGridView1->Size = System::Drawing::Size(495, 356);
  149.             this->dataGridView1->TabIndex = 1;
  150.             //
  151.             // button1
  152.             //
  153.             this->button1->Location = System::Drawing::Point(458, 531);
  154.             this->button1->Name = L"button1";
  155.             this->button1->Size = System::Drawing::Size(75, 70);
  156.             this->button1->TabIndex = 2;
  157.             this->button1->Text = L"Ок";
  158.             this->button1->UseVisualStyleBackColor = true;
  159.             this->button1->Click += gcnew System::EventHandler(this, &MyForm::button1_Click);
  160.             //
  161.             // menuStrip1
  162.             //
  163.             this->menuStrip1->Items->AddRange(gcnew cli::array< System::Windows::Forms::ToolStripItem^  >(3) {
  164.                 this->сгенерироватьМатрицуToolStripMenuItem,
  165.                     this->очиститьToolStripMenuItem, this->выводToolStripMenuItem
  166.             });
  167.             this->menuStrip1->Location = System::Drawing::Point(0, 0);
  168.             this->menuStrip1->Name = L"menuStrip1";
  169.             this->menuStrip1->Size = System::Drawing::Size(558, 24);
  170.             this->menuStrip1->TabIndex = 3;
  171.             this->menuStrip1->Text = L"menuStrip1";
  172.             //
  173.             // сгенерироватьМатрицуToolStripMenuItem
  174.             //
  175.             this->сгенерироватьМатрицуToolStripMenuItem->Name = L"сгенерироватьМатрицуToolStripMenuItem";
  176.             this->сгенерироватьМатрицуToolStripMenuItem->Size = System::Drawing::Size(152, 20);
  177.             this->сгенерироватьМатрицуToolStripMenuItem->Text = L"Сгенерировать матрицу";
  178.             this->сгенерироватьМатрицуToolStripMenuItem->Click += gcnew System::EventHandler(this, &MyForm::сгенерироватьМатрицуToolStripMenuItem_Click);
  179.             //
  180.             // очиститьToolStripMenuItem
  181.             //
  182.             this->очиститьToolStripMenuItem->Name = L"очиститьToolStripMenuItem";
  183.             this->очиститьToolStripMenuItem->Size = System::Drawing::Size(71, 20);
  184.             this->очиститьToolStripMenuItem->Text = L"Очистить";
  185.             this->очиститьToolStripMenuItem->Click += gcnew System::EventHandler(this, &MyForm::очиститьToolStripMenuItem_Click);
  186.             //
  187.             // выводToolStripMenuItem
  188.             //
  189.             this->выводToolStripMenuItem->Name = L"выводToolStripMenuItem";
  190.             this->выводToolStripMenuItem->Size = System::Drawing::Size(53, 20);
  191.             this->выводToolStripMenuItem->Text = L"Выход";
  192.             this->выводToolStripMenuItem->Click += gcnew System::EventHandler(this, &MyForm::выводToolStripMenuItem_Click);
  193.             //
  194.             // dataGridView2
  195.             //
  196.             this->dataGridView2->AllowUserToAddRows = false;
  197.             this->dataGridView2->AllowUserToDeleteRows = false;
  198.             this->dataGridView2->ColumnHeadersHeightSizeMode = System::Windows::Forms::DataGridViewColumnHeadersHeightSizeMode::AutoSize;
  199.             this->dataGridView2->Location = System::Drawing::Point(133, 517);
  200.             this->dataGridView2->Name = L"dataGridView2";
  201.             this->dataGridView2->ReadOnly = true;
  202.             this->dataGridView2->Size = System::Drawing::Size(280, 84);
  203.             this->dataGridView2->TabIndex = 4;
  204.             //
  205.             // label2
  206.             //
  207.             this->label2->AutoSize = true;
  208.             this->label2->Location = System::Drawing::Point(35, 531);
  209.             this->label2->Name = L"label2";
  210.             this->label2->Size = System::Drawing::Size(43, 13);
  211.             this->label2->TabIndex = 5;
  212.             this->label2->Text = L"Вектор";
  213.             //
  214.             // MyForm
  215.             //
  216.             this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
  217.             this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
  218.             this->ClientSize = System::Drawing::Size(558, 620);
  219.             this->Controls->Add(this->label2);
  220.             this->Controls->Add(this->dataGridView2);
  221.             this->Controls->Add(this->button1);
  222.             this->Controls->Add(this->dataGridView1);
  223.             this->Controls->Add(this->groupBox1);
  224.             this->Controls->Add(this->menuStrip1);
  225.             this->MainMenuStrip = this->menuStrip1;
  226.             this->Name = L"MyForm";
  227.             this->Text = L"Генератор матриц";
  228.             this->groupBox1->ResumeLayout(false);
  229.             this->groupBox1->PerformLayout();
  230.             (cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->numericUpDown1))->EndInit();
  231.             (cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->dataGridView1))->EndInit();
  232.             this->menuStrip1->ResumeLayout(false);
  233.             this->menuStrip1->PerformLayout();
  234.             (cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->dataGridView2))->EndInit();
  235.             this->ResumeLayout(false);
  236.             this->PerformLayout();
  237.  
  238.         }
  239. #pragma endregion
  240.         //Создаем функцию Form, которая будет генерировать нашу матрицу по формуле
  241.         private: void form(int size, int **mass) {
  242.             for (int i = 0; i < size; i++)
  243.             {
  244.                 for (int j = 0; j < size; j++) {
  245.                     mass[i][j] = /*Тут указывам формулу, по которой будет генерироваться матрица, например*/ i + j * 2;
  246.                 }
  247.             }      
  248.         }
  249.        
  250.         //Создаем функцию typing для ручного ввода
  251.                  private: void typing(int size, int **mass) {
  252.                      for (int i = 0; i < size; i++) {
  253.                          for (int j = 0; j < size; j++) {
  254.                              //Получаем значение из ячеек таблицы
  255.                              mass[i][j] = Convert::ToInt32(dataGridView1->Rows[i]->Cells[j]->Value);
  256.                          }
  257.                      }
  258.                  }
  259.         //Создаем функцию clear для очистки таблицы и векторов
  260.                           private: void clear() {
  261.                               for (int i = 0; i < dataGridView1->Rows->Count; i++) {
  262.                                   dataGridView2->Rows[0]->Cells[i]->Value = 0;
  263.                                   for (int j = 0; j < dataGridView1->Rows->Count; j++) {
  264.                                       dataGridView1->Rows[i]->Cells[j]->Value = 0;
  265.                                   }
  266.                               }
  267.                           }
  268.  
  269.         //Создаем функцию show, которая будет выводить нашу матрицу
  270.         private: void show(int size, int **mass) {
  271.             for (int i = 0; i < size; i++) {
  272.                 for (int j = 0; j < size; j++) {
  273.                     //Название таблицы (в верхнем левом углу)
  274.                     dataGridView1->TopLeftHeaderCell->Value = "Матрица";
  275.  
  276.                     //Вывод номеров столбцов
  277.                     dataGridView1->Columns[j]->HeaderCell->Value = Convert::ToString(j + 1);
  278.  
  279.                     //Вывод номеров строк
  280.                     dataGridView1->Rows[i]->HeaderCell->Value = Convert::ToString(i + 1);
  281.  
  282.                     //Вывод значений ячеек
  283.                     dataGridView1->Rows[i]->Cells[j]->Value = mass[i][j];
  284.  
  285.                 }
  286.             }
  287.         }
  288.  
  289.     //Создаем функцию vektor, которая будет искать в каждой строке наибольший элемент и записывать в отдельный массив
  290.                  private: void vektor(int size, int **mass) {
  291.                      //Создаем динамический массив под вектор
  292.                      int *vekt = new int[size];
  293.  
  294.                      //Ищем наибольшее значение в каждой строке
  295.                      for (int i = 0; i < size; i++) {
  296.                          vekt[i] = mass[i][0];
  297.                          for (int j = 0; j < size; j++) {
  298.                              if (vekt[i] < mass[i][j])
  299.                                  vekt[i] = mass[i][j];
  300.                          }
  301.                      }
  302.  
  303.                      //Выводим вектор во вторую таблицу
  304.                      for (int i = 0; i < size; i++) {
  305.                          dataGridView2->Columns[i]->HeaderCell->Value = Convert::ToString(i + 1);
  306.                          dataGridView2->Rows[0]->Cells[i]->Value = vekt[i];
  307.                      }
  308.  
  309.                      //Удаляем массив vekt
  310.                      delete[] vekt;
  311.                  }
  312.  
  313.     private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
  314.        
  315.         //Получаем размерность матрицы
  316.         int size = Convert::ToInt32(numericUpDown1->Value);
  317.  
  318.         //Создаем двумерный динамический массив для нашец матрицы
  319.         int **mass = new int *[size];
  320.         for (int i = 0; i < size; i++) {
  321.             mass[i] = new int[size];
  322.         }
  323.  
  324.         //Создаем таблицу под матрицу
  325.         dataGridView1->ColumnCount = size;
  326.         dataGridView1->RowCount = size;
  327.  
  328.         //Создаем таблицу под вектор
  329.         dataGridView2->ColumnCount = size;
  330.         dataGridView2->RowCount = 1;
  331.  
  332.         //Если выбран вариант "по формуле"
  333.         if (radioButton1->Checked) {
  334.             //Вызываем функцию form, которая будет создавать матрицу по формуле
  335.             form(size, mass);
  336.         }
  337.  
  338.         //Если выбран вариант "вручную"
  339.         if (radioButton2->Checked) {
  340.             //Вызываем функцию typing для заполнения матрицы значения введенные ввручную
  341.             typing(size, mass);
  342.         }
  343.  
  344.         //Вызываем функцию show для вывода матрицы
  345.         show(size, mass);
  346.  
  347.         //Вызываем функцию vektor
  348.         vektor(size, mass);
  349.  
  350.  
  351.         //Выравниваем ячейки таблиц
  352.         dataGridView1->AutoResizeRowHeadersWidth(DataGridViewRowHeadersWidthSizeMode::AutoSizeToAllHeaders);
  353.         dataGridView1->AutoResizeColumns();
  354.         dataGridView2->AutoResizeColumns();
  355.  
  356.         //Удаляем из памяти массив с матрицей
  357.         for (int i = 0; i < size; i++) {
  358.             delete[] mass[i];
  359.         }
  360.         delete[] mass;
  361.             }
  362.  
  363. private: System::Void сгенерироватьМатрицуToolStripMenuItem_Click(System::Object^  sender, System::EventArgs^  e) {
  364.     //Системно нажимаем на кнопку "Ok"
  365.     button1->PerformClick();
  366.     }
  367. private: System::Void выводToolStripMenuItem_Click(System::Object^  sender, System::EventArgs^  e) {
  368.  
  369.     //Выводим из приложения
  370.     Application::Exit();
  371. }
  372. private: System::Void radioButton2_CheckedChanged(System::Object^  sender, System::EventArgs^  e) {
  373.    
  374.     //Очищаем таблицу
  375.     clear();
  376.     dataGridView1->ReadOnly = false;
  377.  
  378. }
  379. private: System::Void очиститьToolStripMenuItem_Click(System::Object^  sender, System::EventArgs^  e) {
  380.     clear();
  381. }
  382. private: System::Void radioButton1_CheckedChanged(System::Object^  sender, System::EventArgs^  e) {
  383.     dataGridView1->ReadOnly = true;
  384. }
  385. };
  386. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement