Parcourir la source

Перенёс вычисление ответа в отдельный метод

Вадим Королёв il y a 2 ans
Parent
commit
d6650c4a71
2 fichiers modifiés avec 36 ajouts et 13 suppressions
  1. 35 12
      2 Linear algorythms/MainForm.cs
  2. 1 1
      2 Linear algorythms/calculator.csproj

+ 35 - 12
2 Linear algorythms/MainForm.cs

@@ -16,6 +16,33 @@ namespace calculator
         {
             InitializeComponent();
         }
+
+        /// <summary>
+        /// Вычисляет ответ задания
+        /// </summary>
+        /// <param name="x">Переменная x</param>
+        /// <param name="y">Переменная y</param>
+        /// <param name="z">Переменная z</param>
+        /// <param name="success">Указатель на переменную успеха</param>
+        /// <returns>Ответ задания</returns>
+        public double CalculateAnswer(double x, double y, double z, ref bool success)
+        {
+            Console.Write(y);
+            double output = 0.00d;
+            try
+            {
+                output += Math.Pow(2, Math.Pow(y, x));
+                output += Math.Pow(Math.Pow(3, x), y);
+                output -= (y * (Math.Atan(z) - Math.PI / 6)) / (Math.Abs(x) + 1 / (y * y - 1));
+                success = true;
+            }
+            catch (DivideByZeroException)
+            {
+                success = false;
+            }
+            return output;
+        }
+
         /// <summary>
         /// Клик кнопки, вычисляющей ответ
         /// </summary>
@@ -37,21 +64,17 @@ namespace calculator
             }
 
             // Вычисляем ответ
-            double Output = 0.00d;
-            try
+            bool success = false;
+            double answer = CalculateAnswer(x, y, z, ref success);
+            if (success)
             {
-                Output += Math.Pow(2, Math.Pow(y, x));
-                Output += Math.Pow(Math.Pow(3, x), y);
-                Output -= (y * (Math.Atan(z) - Math.PI / 6)) / (Math.Abs(x) + 1 / (y * y + 1));
-            } catch (DivideByZeroException)
+                // Выводим ответ в lblAnswer
+                lblAnswer.Visible = true;
+                lblAnswer.Text = Convert.ToString(answer);
+            } else
             {
-                MessageBox.Show("При вычислении ответа необходимо делить на 0!");
-                return;
+                MessageBox.Show("При вычислении ответа произошла ошибка!");
             }
-
-            // Выводим ответ в lblAnswer
-            lblAnswer.Visible = true;
-            lblAnswer.Text = Convert.ToString(Output);
         }
         /// <summary>
         /// Метод загрузки главной формы

+ 1 - 1
2 Linear algorythms/calculator.csproj

@@ -2,7 +2,7 @@
 
   <PropertyGroup>
     <OutputType>WinExe</OutputType>
-    <TargetFramework>net5.0-windows</TargetFramework>
+    <TargetFramework>netcoreapp3.1</TargetFramework>
     <UseWindowsForms>true</UseWindowsForms>
   </PropertyGroup>