fork(7) download
  1. Текст программы с комментриями
  2. #pragma endregion
  3. // минимизируемая функция
  4. private: double f(double x) {
  5. return 2 * Math::Pow(x - 3, 2) + Math::Exp(Math::Pow(x, 2) / 2);
  6.  
  7. //return 0.2 * x * Math::Log(x) + Math::Pow(x - 2.3, 2);
  8. }
  9.  
  10. // вычисление производной функции
  11. private: double df(double x) {
  12. double dx = 0.001;
  13.  
  14. return (f(x + dx) - f(x - dx)) / (2 * dx);
  15. }
  16.  
  17. // функция реализует метод наискорейшего спуска
  18. private: void descent(double x, double eps) {
  19. int k = 0;
  20.  
  21. double h = 0.7;
  22.  
  23. for (; ; ) {
  24. double y = df(x);
  25.  
  26. double z = Math::Sqrt(Math::Pow(y, 2));
  27.  
  28. if (z < eps) {
  29. // добавить строку в таблицу
  30. dataGridView1->Rows->Add();
  31.  
  32. dataGridView1->Rows[k - 1]->Cells[0]->Value = k.ToString();
  33.  
  34. // Math::Round() - округление, 2й аргумент - число знаков после запятой
  35. dataGridView1->Rows[k - 1]->Cells[1]->Value = Math::Round(x, 6).ToString();
  36. dataGridView1->Rows[k - 1]->Cells[2]->Value = Math::Round(f(x), 6).ToString();
  37.  
  38. // записать результаты в текстовые поля
  39. textBox3->Text = Math::Round(x, 6).ToString();
  40. textBox4->Text = Math::Round(f(x), 6).ToString();
  41.  
  42. break;
  43. } else {
  44. for (; ; ) {
  45. double f_1 = f(x);
  46.  
  47. double x_1 = x - h * y;
  48.  
  49. double f_2 = f(x_1);
  50.  
  51. if (f_2 < f_1) {
  52. k++;
  53.  
  54. x = x_1;
  55.  
  56. // добавить строку в таблицу
  57. dataGridView1->Rows->Add();
  58.  
  59. dataGridView1->Rows[k - 1]->Cells[0]->Value = k.ToString();
  60.  
  61.  
  62. dataGridView1->Rows[k - 1]->Cells[1]->Value = Math::Round(x, 6).ToString();
  63. dataGridView1->Rows[k - 1]->Cells[2]->Value = Math::Round(f(x), 6).ToString();
  64.  
  65. break;
  66. } else {
  67. h *= 0.7;
  68. }
  69. }
  70. }
  71. }
  72. }
  73.  
  74. // рисование фукции
  75. private: void draw(double a, double b) {
  76. // очистка поля
  77. chart1->Series[0]->Points->Clear();
  78.  
  79. double h = 0.001;
  80.  
  81. for (double x = a; x <= b; x += h) {
  82. // добавить точку на график
  83. chart1->Series[0]->Points->AddXY(x, f(x));
  84. }
  85. }
  86.  
  87. // обработка события при нажатии на кнопку
  88. private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
  89. double x, eps, a, b;
  90.  
  91. // обработка исключения
  92. try {
  93. // получения данных из полей
  94. x = Convert::ToDouble(textBox1->Text);
  95. eps = Convert::ToDouble(textBox2->Text);
  96.  
  97. a = Convert::ToDouble(textBox5->Text);
  98. b = Convert::ToDouble(textBox6->Text);
  99.  
  100. if (a >= b) {
  101. // сообщение об ошибке
  102. MessageBox::Show("Введите корректный интервал!");
  103. } else {
  104. // обнулить число строк
  105. dataGridView1->RowCount = 0;
  106.  
  107. // вызов функции
  108. descent(x, eps);
  109.  
  110. // нарисовать график функции
  111. draw(a, b);
  112. }
  113. } catch (...) {
  114. // сообщение об ошибке
  115. MessageBox::Show("Введите корректные данные!");
  116. }
  117. }
  118.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:1:1: error: stray '\320' in program
 Текст программы с комментриями
 ^
prog.cpp:1:1: error: stray '\242' in program
prog.cpp:1:1: error: stray '\320' in program
prog.cpp:1:1: error: stray '\265' in program
prog.cpp:1:1: error: stray '\320' in program
prog.cpp:1:1: error: stray '\272' in program
prog.cpp:1:1: error: stray '\321' in program
prog.cpp:1:1: error: stray '\201' in program
prog.cpp:1:1: error: stray '\321' in program
prog.cpp:1:1: error: stray '\202' in program
prog.cpp:1:1: error: stray '\320' in program
prog.cpp:1:1: error: stray '\277' in program
prog.cpp:1:1: error: stray '\321' in program
prog.cpp:1:1: error: stray '\200' in program
prog.cpp:1:1: error: stray '\320' in program
prog.cpp:1:1: error: stray '\276' in program
prog.cpp:1:1: error: stray '\320' in program
prog.cpp:1:1: error: stray '\263' in program
prog.cpp:1:1: error: stray '\321' in program
prog.cpp:1:1: error: stray '\200' in program
prog.cpp:1:1: error: stray '\320' in program
prog.cpp:1:1: error: stray '\260' in program
prog.cpp:1:1: error: stray '\320' in program
prog.cpp:1:1: error: stray '\274' in program
prog.cpp:1:1: error: stray '\320' in program
prog.cpp:1:1: error: stray '\274' in program
prog.cpp:1:1: error: stray '\321' in program
prog.cpp:1:1: error: stray '\213' in program
prog.cpp:1:1: error: stray '\321' in program
prog.cpp:1:1: error: stray '\201' in program
prog.cpp:1:1: error: stray '\320' in program
prog.cpp:1:1: error: stray '\272' in program
prog.cpp:1:1: error: stray '\320' in program
prog.cpp:1:1: error: stray '\276' in program
prog.cpp:1:1: error: stray '\320' in program
prog.cpp:1:1: error: stray '\274' in program
prog.cpp:1:1: error: stray '\320' in program
prog.cpp:1:1: error: stray '\274' in program
prog.cpp:1:1: error: stray '\320' in program
prog.cpp:1:1: error: stray '\265' in program
prog.cpp:1:1: error: stray '\320' in program
prog.cpp:1:1: error: stray '\275' in program
prog.cpp:1:1: error: stray '\321' in program
prog.cpp:1:1: error: stray '\202' in program
prog.cpp:1:1: error: stray '\321' in program
prog.cpp:1:1: error: stray '\200' in program
prog.cpp:1:1: error: stray '\320' in program
prog.cpp:1:1: error: stray '\270' in program
prog.cpp:1:1: error: stray '\321' in program
prog.cpp:1:1: error: stray '\217' in program
prog.cpp:1:1: error: stray '\320' in program
prog.cpp:1:1: error: stray '\274' in program
prog.cpp:1:1: error: stray '\320' in program
prog.cpp:1:1: error: stray '\270' in program
prog.cpp:4:2: error: expected unqualified-id before 'private'
  private: double f(double x) {
  ^
prog.cpp:11:2: error: expected unqualified-id before 'private'
  private: double df(double x) {
  ^
prog.cpp:18:2: error: expected unqualified-id before 'private'
  private: void descent(double x, double eps) {
  ^
stdout
Standard output is empty