Advertisement
daniv1

Untitled

Dec 11th, 2017
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 18.28 KB | None | 0 0
  1. #pragma once
  2.  
  3. namespace Project10 {
  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::Label^  label1;
  38.     protected:
  39.     private: System::Windows::Forms::Label^  label2;
  40.     private: System::Windows::Forms::FolderBrowserDialog^  folderBrowserDialog1;
  41.     private: System::Windows::Forms::FolderBrowserDialog^  folderBrowserDialog2;
  42.     private: System::Windows::Forms::FolderBrowserDialog^  folderBrowserDialog3;
  43.     private: System::Windows::Forms::FolderBrowserDialog^  folderBrowserDialog4;
  44.     private: System::Windows::Forms::Label^  label3;
  45.     private: System::Windows::Forms::Label^  label4;
  46.     private: System::Windows::Forms::Label^  label5;
  47.     private: System::Windows::Forms::Label^  label6;
  48.     private: System::Windows::Forms::Label^  label7;
  49.     private: System::Windows::Forms::Label^  label8;
  50.     private: System::Windows::Forms::Label^  label9;
  51.     private: System::Windows::Forms::Label^  label10;
  52.     private: System::Windows::Forms::TextBox^  textBox1;
  53.     private: System::Windows::Forms::TextBox^  textBox2;
  54.     private: System::Windows::Forms::TextBox^  textBox3;
  55.     private: System::Windows::Forms::TextBox^  textBox4;
  56.     private: System::Windows::Forms::TextBox^  textBox5;
  57.     private: System::Windows::Forms::TextBox^  textBox6;
  58.     private: System::Windows::Forms::Button^  button1;
  59.     private: System::Windows::Forms::Button^  button2;
  60.     private: System::Windows::Forms::Button^  button3;
  61.     private: System::Windows::Forms::ComboBox^  comboBox1;
  62.     private: System::Windows::Forms::ComboBox^  comboBox2;
  63.  
  64.     private:
  65.         /// <summary>
  66.         /// Обязательная переменная конструктора.
  67.         /// </summary>
  68.         System::ComponentModel::Container ^components;
  69.  
  70. #pragma region Windows Form Designer generated code
  71.         /// <summary>
  72.         /// Требуемый метод для поддержки конструктора — не изменяйте
  73.         /// содержимое этого метода с помощью редактора кода.
  74.         /// </summary>
  75.         void InitializeComponent(void)
  76.         {
  77.             this->label1 = (gcnew System::Windows::Forms::Label());
  78.             this->label2 = (gcnew System::Windows::Forms::Label());
  79.             this->folderBrowserDialog1 = (gcnew System::Windows::Forms::FolderBrowserDialog());
  80.             this->folderBrowserDialog2 = (gcnew System::Windows::Forms::FolderBrowserDialog());
  81.             this->folderBrowserDialog3 = (gcnew System::Windows::Forms::FolderBrowserDialog());
  82.             this->folderBrowserDialog4 = (gcnew System::Windows::Forms::FolderBrowserDialog());
  83.             this->label3 = (gcnew System::Windows::Forms::Label());
  84.             this->label4 = (gcnew System::Windows::Forms::Label());
  85.             this->label5 = (gcnew System::Windows::Forms::Label());
  86.             this->label6 = (gcnew System::Windows::Forms::Label());
  87.             this->label7 = (gcnew System::Windows::Forms::Label());
  88.             this->label8 = (gcnew System::Windows::Forms::Label());
  89.             this->label9 = (gcnew System::Windows::Forms::Label());
  90.             this->label10 = (gcnew System::Windows::Forms::Label());
  91.             this->textBox1 = (gcnew System::Windows::Forms::TextBox());
  92.             this->textBox2 = (gcnew System::Windows::Forms::TextBox());
  93.             this->textBox3 = (gcnew System::Windows::Forms::TextBox());
  94.             this->textBox4 = (gcnew System::Windows::Forms::TextBox());
  95.             this->textBox5 = (gcnew System::Windows::Forms::TextBox());
  96.             this->textBox6 = (gcnew System::Windows::Forms::TextBox());
  97.             this->button1 = (gcnew System::Windows::Forms::Button());
  98.             this->button2 = (gcnew System::Windows::Forms::Button());
  99.             this->button3 = (gcnew System::Windows::Forms::Button());
  100.             this->comboBox1 = (gcnew System::Windows::Forms::ComboBox());
  101.             this->comboBox2 = (gcnew System::Windows::Forms::ComboBox());
  102.             this->SuspendLayout();
  103.             //
  104.             // label1
  105.             //
  106.             this->label1->AutoSize = true;
  107.             this->label1->Location = System::Drawing::Point(30, 34);
  108.             this->label1->Name = L"label1";
  109.             this->label1->Size = System::Drawing::Size(122, 17);
  110.             this->label1->TabIndex = 0;
  111.             this->label1->Text = L"ОБЕРІТЬ МЕТОД";
  112.             //
  113.             // label2
  114.             //
  115.             this->label2->AutoSize = true;
  116.             this->label2->Location = System::Drawing::Point(21, 167);
  117.             this->label2->Name = L"label2";
  118.             this->label2->Size = System::Drawing::Size(142, 17);
  119.             this->label2->TabIndex = 1;
  120.             this->label2->Text = L"ОБЕРІТЬ РІВНЯННЯ";
  121.             //
  122.             // label3
  123.             //
  124.             this->label3->AutoSize = true;
  125.             this->label3->Location = System::Drawing::Point(295, 34);
  126.             this->label3->Name = L"label3";
  127.             this->label3->Size = System::Drawing::Size(90, 17);
  128.             this->label3->TabIndex = 2;
  129.             this->label3->Text = L"ВХІДНІ ДАНІ";
  130.             //
  131.             // label4
  132.             //
  133.             this->label4->AutoSize = true;
  134.             this->label4->Location = System::Drawing::Point(211, 83);
  135.             this->label4->Name = L"label4";
  136.             this->label4->Size = System::Drawing::Size(24, 17);
  137.             this->label4->TabIndex = 3;
  138.             this->label4->Text = L"а=";
  139.             //
  140.             // label5
  141.             //
  142.             this->label5->AutoSize = true;
  143.             this->label5->Location = System::Drawing::Point(211, 144);
  144.             this->label5->Name = L"label5";
  145.             this->label5->Size = System::Drawing::Size(24, 17);
  146.             this->label5->TabIndex = 4;
  147.             this->label5->Text = L"b=";
  148.             //
  149.             // label6
  150.             //
  151.             this->label6->AutoSize = true;
  152.             this->label6->Location = System::Drawing::Point(211, 206);
  153.             this->label6->Name = L"label6";
  154.             this->label6->Size = System::Drawing::Size(39, 17);
  155.             this->label6->TabIndex = 5;
  156.             this->label6->Text = L"eps=";
  157.             //
  158.             // label7
  159.             //
  160.             this->label7->AutoSize = true;
  161.             this->label7->Location = System::Drawing::Point(211, 286);
  162.             this->label7->Name = L"label7";
  163.             this->label7->Size = System::Drawing::Size(48, 17);
  164.             this->label7->TabIndex = 6;
  165.             this->label7->Text = L"kmax=";
  166.             this->label7->Visible = false;
  167.             //
  168.             // label8
  169.             //
  170.             this->label8->AutoSize = true;
  171.             this->label8->Location = System::Drawing::Point(501, 34);
  172.             this->label8->Name = L"label8";
  173.             this->label8->Size = System::Drawing::Size(100, 17);
  174.             this->label8->TabIndex = 7;
  175.             this->label8->Text = L"РЕЗУЛЬТАТИ";
  176.             //
  177.             // label9
  178.             //
  179.             this->label9->AutoSize = true;
  180.             this->label9->Location = System::Drawing::Point(419, 81);
  181.             this->label9->Name = L"label9";
  182.             this->label9->Size = System::Drawing::Size(51, 17);
  183.             this->label9->TabIndex = 8;
  184.             this->label9->Text = L"Корінь";
  185.             //
  186.             // label10
  187.             //
  188.             this->label10->AutoSize = true;
  189.             this->label10->Location = System::Drawing::Point(419, 144);
  190.             this->label10->Name = L"label10";
  191.             this->label10->Size = System::Drawing::Size(66, 17);
  192.             this->label10->TabIndex = 9;
  193.             this->label10->Text = L"Кількість";
  194.             //
  195.             // textBox1
  196.             //
  197.             this->textBox1->Location = System::Drawing::Point(298, 78);
  198.             this->textBox1->Name = L"textBox1";
  199.             this->textBox1->Size = System::Drawing::Size(100, 22);
  200.             this->textBox1->TabIndex = 10;
  201.             //
  202.             // textBox2
  203.             //
  204.             this->textBox2->Location = System::Drawing::Point(298, 141);
  205.             this->textBox2->Name = L"textBox2";
  206.             this->textBox2->Size = System::Drawing::Size(100, 22);
  207.             this->textBox2->TabIndex = 11;
  208.             //
  209.             // textBox3
  210.             //
  211.             this->textBox3->Location = System::Drawing::Point(298, 206);
  212.             this->textBox3->Name = L"textBox3";
  213.             this->textBox3->Size = System::Drawing::Size(100, 22);
  214.             this->textBox3->TabIndex = 12;
  215.             //
  216.             // textBox4
  217.             //
  218.             this->textBox4->Location = System::Drawing::Point(298, 281);
  219.             this->textBox4->Name = L"textBox4";
  220.             this->textBox4->Size = System::Drawing::Size(100, 22);
  221.             this->textBox4->TabIndex = 13;
  222.             this->textBox4->Visible = false;
  223.             //
  224.             // textBox5
  225.             //
  226.             this->textBox5->Location = System::Drawing::Point(504, 78);
  227.             this->textBox5->Name = L"textBox5";
  228.             this->textBox5->Size = System::Drawing::Size(100, 22);
  229.             this->textBox5->TabIndex = 14;
  230.             //
  231.             // textBox6
  232.             //
  233.             this->textBox6->Location = System::Drawing::Point(504, 141);
  234.             this->textBox6->Name = L"textBox6";
  235.             this->textBox6->Size = System::Drawing::Size(100, 22);
  236.             this->textBox6->TabIndex = 15;
  237.             //
  238.             // button1
  239.             //
  240.             this->button1->Location = System::Drawing::Point(44, 396);
  241.             this->button1->Name = L"button1";
  242.             this->button1->Size = System::Drawing::Size(134, 60);
  243.             this->button1->TabIndex = 16;
  244.             this->button1->Text = L"РОЗВ\'ЯЗАТИ";
  245.             this->button1->UseVisualStyleBackColor = true;
  246.             this->button1->Click += gcnew System::EventHandler(this, &MyForm::button1_Click);
  247.             //
  248.             // button2
  249.             //
  250.             this->button2->Location = System::Drawing::Point(298, 396);
  251.             this->button2->Name = L"button2";
  252.             this->button2->Size = System::Drawing::Size(138, 60);
  253.             this->button2->TabIndex = 17;
  254.             this->button2->Text = L"ОЧИСТИТИ";
  255.             this->button2->UseVisualStyleBackColor = true;
  256.             this->button2->Click += gcnew System::EventHandler(this, &MyForm::button2_Click);
  257.             //
  258.             // button3
  259.             //
  260.             this->button3->Location = System::Drawing::Point(529, 396);
  261.             this->button3->Name = L"button3";
  262.             this->button3->Size = System::Drawing::Size(141, 60);
  263.             this->button3->TabIndex = 18;
  264.             this->button3->Text = L"ЗАВЕРШИТИ";
  265.             this->button3->UseVisualStyleBackColor = true;
  266.             this->button3->Click += gcnew System::EventHandler(this, &MyForm::button3_Click);
  267.             //
  268.             // comboBox1
  269.             //
  270.             this->comboBox1->FormattingEnabled = true;
  271.             this->comboBox1->Items->AddRange(gcnew cli::array< System::Object^  >(2) { L"Метод половинного ділення", L"Метод Ньютона" });
  272.             this->comboBox1->Location = System::Drawing::Point(23, 64);
  273.             this->comboBox1->Name = L"comboBox1";
  274.             this->comboBox1->Size = System::Drawing::Size(140, 24);
  275.             this->comboBox1->TabIndex = 19;
  276.             this->comboBox1->SelectedIndexChanged += gcnew System::EventHandler(this, &MyForm::comboBox1_SelectedIndexChanged);
  277.             //
  278.             // comboBox2
  279.             //
  280.             this->comboBox2->FormattingEnabled = true;
  281.             this->comboBox2->Items->AddRange(gcnew cli::array< System::Object^  >(2) { L"x*x - 4 = 0", L"3*x - 4 *log(x) - 5 = 0" });
  282.             this->comboBox2->Location = System::Drawing::Point(23, 206);
  283.             this->comboBox2->Name = L"comboBox2";
  284.             this->comboBox2->Size = System::Drawing::Size(140, 24);
  285.             this->comboBox2->TabIndex = 20;
  286.             //
  287.             // MyForm
  288.             //
  289.             this->AutoScaleDimensions = System::Drawing::SizeF(8, 16);
  290.             this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
  291.             this->ClientSize = System::Drawing::Size(682, 543);
  292.             this->Controls->Add(this->comboBox2);
  293.             this->Controls->Add(this->comboBox1);
  294.             this->Controls->Add(this->button3);
  295.             this->Controls->Add(this->button2);
  296.             this->Controls->Add(this->button1);
  297.             this->Controls->Add(this->textBox6);
  298.             this->Controls->Add(this->textBox5);
  299.             this->Controls->Add(this->textBox4);
  300.             this->Controls->Add(this->textBox3);
  301.             this->Controls->Add(this->textBox2);
  302.             this->Controls->Add(this->textBox1);
  303.             this->Controls->Add(this->label10);
  304.             this->Controls->Add(this->label9);
  305.             this->Controls->Add(this->label8);
  306.             this->Controls->Add(this->label7);
  307.             this->Controls->Add(this->label6);
  308.             this->Controls->Add(this->label5);
  309.             this->Controls->Add(this->label4);
  310.             this->Controls->Add(this->label3);
  311.             this->Controls->Add(this->label2);
  312.             this->Controls->Add(this->label1);
  313.             this->Name = L"MyForm";
  314.             this->Text = L"MyForm";
  315.             this->ResumeLayout(false);
  316.             this->PerformLayout();
  317.  
  318.         }
  319. #pragma endregion
  320.  
  321.  
  322.  
  323.         double f(double x, char k1) // Ліві частини рівнянь
  324.         {
  325.             switch (k1)
  326.             {
  327.             case 0: return x * x - 4; break;
  328.             case 1: return 3 * x - 4 * Math::Log(x) - 5; break;
  329.             }
  330.         }
  331.  
  332.  
  333.  
  334.  
  335.         double fp(double x, double d, char k1) // Перша похідна
  336.         {
  337.             return (f(x + d, k1) - f(x, k1)) / d;
  338.         }
  339.  
  340.  
  341.  
  342.         double f2p(double x, double d, char k1) // Друга похідна
  343.         {
  344.             return (f(x + d, k1) + f(x - d, k1) - 2 * f(x, k1)) / (d * d);
  345.         }
  346.  
  347.  
  348.  
  349.  
  350.         double MDP(double a, double b, double Eps, char k1, int &L)
  351.         {
  352.             double c, Fc;
  353.             while (b - a > Eps)
  354.             {
  355.                 c = 0.5 * (b - a) + a;
  356.                 L = L + 1;  // Лічильник кількості поділів інтервалу [a, b]
  357.                 Fc = f(c, k1);
  358.                 if (Math::Abs(Fc) < Eps)    // Перевірка, чи точка с не є поблизу кореня x*
  359.                     return c;   // Завершення роботи функції MDP
  360.                     if (f(a, k1) * Fc > 0) a = c;
  361.                     else b = c;
  362.             }
  363.             return c;   // Завершення роботи функції MDP
  364.         }
  365.  
  366.  
  367.         double MN(double a, double b, double Eps, char k1, int Kmax, int &L)
  368.         {
  369.             double x, Dx, D;
  370.             int i;
  371.             Dx = 0.0;
  372.             D = Eps / 100.0;
  373.             x = b;
  374.             if (f(x, k1) * f2p(x, D, k1) < 0) x = a;
  375.  
  376.             if (f(x, k1) * f2p(x, D, k1) < 0)
  377.                 MessageBox::Show("Для цього рівняння збіжність ітерацій не гарантована");
  378.             for (i = 1; i <= Kmax; i++)
  379.             {
  380.                 Dx = f(x, k1) / fp(x, D, k1);
  381.                 x = x - Dx;
  382.  
  383.                     if (Math::Abs(Dx) < Eps)
  384.                     {
  385.                         L = i;
  386.                         return x;   // Завершення роботи функції MN
  387.                     }
  388.             }
  389.             MessageBox::Show("За задану кількість ітерацій кореня не знайдено");
  390.             return -1000.0; // -1000.0 Це наша ознака цієї нестандартної ситуації
  391.         }
  392.  
  393.     private: System::Void comboBox1_SelectedIndexChanged(System::Object^  sender, System::EventArgs^  e) {
  394.         switch (comboBox1->SelectedIndex)
  395.         {
  396.  
  397.         case 0:
  398.         {
  399.             label7->Visible = false;    // Робимо невидимим вікно для введення Kmax
  400.             textBox4->Visible = false;
  401.             textBox1->Clear();
  402.             textBox2->Clear();
  403.         } break;
  404.         case 1:
  405.         {
  406.             label7->Visible = true; // Робимо видимим вікно для введення Kmax
  407.             textBox4->Visible = true;
  408.             textBox1->Clear();
  409.             textBox2->Clear();
  410.         } break;
  411.         }
  412.  
  413.     }
  414. private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
  415.     int L, k = -1, Kmax, m = -1; double D, Eps, a, b;
  416.     L = 0;
  417.     switch (comboBox1->SelectedIndex) // Вибір методу :
  418.     {
  419.     case  0: m = 0; break;  // метод ділення навпіл
  420.     case 1: // метод Ньютона
  421.     {
  422.         m = 1;
  423.         D = Eps / 100.0;
  424.         label7->Visible = true; // Робимо видимим вікно для введення Kmax
  425.         textBox4->Visible = true;
  426.         textBox4->Enabled = true;
  427.     } break;
  428.     }
  429.     if (m == -1)
  430.     {
  431.         MessageBox::Show("Оберіть метод !"); comboBox1->Focus();
  432.         return;
  433.     }
  434.     textBox1->Enabled = true; textBox2->Enabled = true;
  435.     switch (comboBox2->SelectedIndex) // Вибір нелінійного рівняння
  436.     {
  437.     case  0: k = 0; break;
  438.     case 1: k = 1; break;
  439.     }
  440.     if (k == -1)
  441.     {
  442.         MessageBox::Show("Оберіть рівняння !"); comboBox2->Focus();
  443.         return;
  444.     }
  445.     // Перевіряємо правильність введення вхідних даних
  446.     if (textBox1->Text == "")
  447.     {
  448.         MessageBox::Show("Введіть число в textBox1"); textBox1->Focus();
  449.         return;
  450.     }
  451.     a = Convert::ToDouble(textBox1->Text); textBox2->Enabled = true;
  452.     if (textBox2->Text == "")
  453.     {
  454.         MessageBox::Show("Введіть число в textBox2"); textBox2->Focus();
  455.         return;
  456.     }
  457.     b = Convert::ToDouble(textBox2->Text); if (a > b)
  458.  
  459.     {
  460.         D = a;
  461.  
  462.  
  463.  
  464.         a = b;
  465.         b = D; // Міняємо місцями a і b
  466.  
  467.         textBox1->Text = Convert::ToString(a); textBox2->Text = Convert::ToString(b);
  468.     }
  469.  
  470.     if (textBox3->Text == "")
  471.     {
  472.         MessageBox::Show("Введіть число в textBox3"); textBox1->Focus();
  473.  
  474.         return;
  475.     }
  476.     Eps = Convert::ToDouble(textBox3->Text);
  477.     if ((Eps > 1e-1) || (Eps <= 0)) // Похибка Eps не повинна бути більшою за 0.1 або меншою за 0
  478.     {
  479.         Eps = 1e-4; // Задаємо примусово Eps = 0.0001
  480.         textBox3->Text = Convert::ToString(Eps);
  481.     }
  482.     if (m == 0)
  483.         if ((f(a, k))* (f(b, k)) > 0)   // чи є корінь на інтервалі [a,b]
  484.         {
  485.             MessageBox::Show("Введіть правильний інтервал [a, b]!"); textBox1->Text = "";
  486.             textBox2->Text = "";
  487.             textBox1->Focus(); return;
  488.         }
  489.  
  490.     // Перевіряємо, чи межі інтервалу [a,b] не є поблизу кореня
  491.     if (Math::Abs(f(a, k)) < Eps)
  492.     {
  493.         textBox5->Text = Convert::ToString(a);
  494.         textBox6->Text = Convert::ToString(L);
  495.         return;
  496.     }
  497.     if (Math::Abs(f(b, k)) < Eps)
  498.     {
  499.         textBox5->Text = Convert::ToString(b);
  500.         textBox6->Text = Convert::ToString(L);
  501.         return;
  502.     }
  503.     switch (m)
  504.     {
  505.     case 0: // Виклик методу ділення навпіл MDP
  506.     {
  507.         textBox5->Text = Convert::ToString(MDP(a, b, Eps, k, L));
  508.         textBox6->Text = Convert::ToString(L);
  509.         label10->Text = "К-ть поділів =";
  510.  
  511.     } break;
  512.     case 1: // Виклик методу Ньютона
  513.     {
  514.         if (textBox4->Text == "")
  515.         {
  516.             MessageBox::Show("Введіть число в textBox4");
  517.             textBox4->Focus();
  518.             return;
  519.         }
  520.         Kmax = Convert::ToInt32(textBox4->Text);
  521.         textBox5->Text = Convert::ToString(MN(a, b, Eps, k, Kmax, L));
  522.         textBox6->Text = Convert::ToString(L);
  523.         label10->Text = "К-ть ітерац.=";
  524.     } break;
  525.     }
  526.  
  527. }
  528. private: System::Void button2_Click(System::Object^  sender, System::EventArgs^  e) {
  529.     textBox1->Clear(); textBox2->Clear();
  530.     textBox3->Clear(); textBox4->Clear();
  531.     textBox5->Clear(); textBox6->Clear();
  532.     switch (comboBox1->SelectedIndex)
  533.     {
  534.     case 0:
  535.     {
  536.         label7->Visible = false; // Робимо невидимим вікно для введення Kmax
  537.         textBox4->Visible = false;
  538.     } break; case 1:
  539.     {
  540.  
  541.         label7->Visible = true; // Робимо видимим вікно для введення Kmax
  542.         textBox4->Visible = true;
  543.     } break;
  544.     }
  545.  
  546. }
  547. private: System::Void button3_Click(System::Object^  sender, System::EventArgs^  e) {
  548.     Close();
  549. }
  550. };
  551. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement