فهرست منبع

Добавил class Matrix

Никита Кашлач 3 سال پیش
والد
کامیت
c5455d6279
2فایلهای تغییر یافته به همراه145 افزوده شده و 13 حذف شده
  1. 29 13
      MatrixXAMLOnly/MainWindow.xaml.cs
  2. 116 0
      MatrixXAMLOnly/Matrix.cs

+ 29 - 13
MatrixXAMLOnly/MainWindow.xaml.cs

@@ -6,6 +6,7 @@ using System.Windows.Controls.Primitives;
 using System.Windows.Input;
 using System.Windows.Media;
 using System.Windows.Media.Animation;
+using Matrix;
 
 namespace MatrixXAMLOnly
 {
@@ -292,8 +293,8 @@ namespace MatrixXAMLOnly
         #region Addition
         private void BtnAdditionCalculate_Click(object sender, RoutedEventArgs e)
         {
-            int rowCount = GridAdditionResult.RowDefinitions.Count;
-            int columnCount = GridAdditionResult.ColumnDefinitions.Count;
+            int rowCount = ((Grid)GridAdditionResult.Children[0]).RowDefinitions.Count;
+            int columnCount = ((Grid)GridAdditionResult.Children[0]).ColumnDefinitions.Count;
 
             double[,] frstTerm = new double[rowCount, columnCount];
             double[,] scndTerm = new double[rowCount, columnCount];
@@ -310,16 +311,20 @@ namespace MatrixXAMLOnly
                     catch
                     {
                         MessageBox.Show("Матрица указана некорректно!");
+                        return;
                     }
                 }
             }
 
-            double[,] result = new double[rowCount, columnCount];
+            MyMatrix matrixFrstTerm = new MyMatrix(frstTerm);
+            MyMatrix matrixScndTerm = new MyMatrix(scndTerm);
+
+            MyMatrix result = matrixFrstTerm + matrixScndTerm;
             for (int i = 0; i < rowCount; i++)
             {
                 for (int j = 0; j < columnCount; j++)
                 {
-                    ((TextBox)((Grid)GridAdditionResult.Children[0]).Children[(i * columnCount) + j]).Text = result[i, j].ToString();
+                    ((TextBox)((Grid)GridAdditionResult.Children[0]).Children[(i * columnCount) + j]).Text = result.data[i, j].ToString();
                 }
             }
         }
@@ -336,6 +341,7 @@ namespace MatrixXAMLOnly
             catch
             {
                 MessageBox.Show("Количество строк/столбцов указано некорректно!");
+                return;
             }
 
             GridAdditionFrstTerm.Children.Clear();
@@ -361,6 +367,7 @@ namespace MatrixXAMLOnly
             catch
             {
                 MessageBox.Show("Количество строк/столбцов указано некорректно!");
+                return;
             }
 
             GridDifferenceFrstTerm.Children.Clear();
@@ -374,8 +381,8 @@ namespace MatrixXAMLOnly
 
         private void BtnDifferenceCalculate_Click(object sender, RoutedEventArgs e)
         {
-            int rowCount = GridDifferenceResult.RowDefinitions.Count;
-            int columnCount = GridDifferenceResult.ColumnDefinitions.Count;
+            int rowCount = ((Grid)GridDifferenceResult.Children[0]).RowDefinitions.Count;
+            int columnCount = ((Grid)GridDifferenceResult.Children[0]).ColumnDefinitions.Count;
 
             double[,] frstTerm = new double[rowCount, columnCount];
             double[,] scndTerm = new double[rowCount, columnCount];
@@ -392,16 +399,20 @@ namespace MatrixXAMLOnly
                     catch
                     {
                         MessageBox.Show("Матрица указана некорректно!");
+                        return;
                     }
                 }
             }
 
-            double[,] result = new double[rowCount, columnCount];
+            MyMatrix matrixFrstTerm = new MyMatrix(frstTerm);
+            MyMatrix matrixScndTerm = new MyMatrix(scndTerm);
+
+            MyMatrix result = matrixFrstTerm - matrixScndTerm;
             for (int i = 0; i < rowCount; i++)
             {
                 for (int j = 0; j < columnCount; j++)
                 {
-                    ((TextBox)((Grid)GridDifferenceResult.Children[0]).Children[(i * columnCount) + j]).Text = result[i, j].ToString();
+                    ((TextBox)((Grid)GridDifferenceResult.Children[0]).Children[(i * columnCount) + j]).Text = result.data[i, j].ToString();
                 }
             }
         }
@@ -420,6 +431,7 @@ namespace MatrixXAMLOnly
             catch
             {
                 MessageBox.Show("Количество строк/столбцов указано некорректно!");
+                return;
             }
 
             GridMultiplicationOnScalarFrstTerm.Children.Clear();
@@ -432,18 +444,20 @@ namespace MatrixXAMLOnly
 
         private void BtnMultiplicationOnScalarCalculate_Click(object sender, RoutedEventArgs e)
         {
-            int rowCount = GridMultiplicationOnScalarResult.RowDefinitions.Count;
-            int columnCount = GridMultiplicationOnScalarResult.ColumnDefinitions.Count;
+            int rowCount = ((Grid)GridMultiplicationOnScalarResult.Children[0]).RowDefinitions.Count;
+            int columnCount = ((Grid)GridMultiplicationOnScalarResult.Children[0]).ColumnDefinitions.Count;
 
             double[,] frstTerm = new double[rowCount, columnCount];
 
+            double scndTerm = 0;
             try
             {
-                double scndTerm = Convert.ToDouble(TextBoxMultiplicationOnScalarScndTerm.Text);
+                scndTerm = Convert.ToDouble(TextBoxMultiplicationOnScalarScndTerm.Text);
             }
             catch
             {
                 MessageBox.Show("Скалярная величина указана некорректно!");
+                return;
             }
 
             for (int i = 0; i < rowCount; i++)
@@ -461,12 +475,14 @@ namespace MatrixXAMLOnly
                 }
             }
 
-            double[,] result = new double[rowCount, columnCount];
+            MyMatrix matrixFrstTerm = new MyMatrix(frstTerm);
+
+            MyMatrix result = matrixFrstTerm * scndTerm;
             for (int i = 0; i < rowCount; i++)
             {
                 for (int j = 0; j < columnCount; j++)
                 {
-                    ((TextBox)((Grid)GridMultiplicationOnScalarResult.Children[0]).Children[(i * columnCount) + j]).Text = result[i, j].ToString();
+                    ((TextBox)((Grid)GridMultiplicationOnScalarResult.Children[0]).Children[(i * columnCount) + j]).Text = result.data[i, j].ToString();
                 }
             }
         }

+ 116 - 0
MatrixXAMLOnly/Matrix.cs

@@ -0,0 +1,116 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using System.Windows;
+
+namespace Matrix
+{
+    class MyMatrix
+    {
+        public double[,] data; // 
+
+        public int Rows
+        {
+            get { return data.GetLength(0); }
+        }
+
+        public int Columns
+        {
+            get { return data.GetLength(1); }
+        }
+
+        public MyMatrix(double[,] data)
+        {
+            this.data = data;
+        }
+
+        public static MyMatrix operator +(MyMatrix matrix1, MyMatrix matrix2)
+        {
+            if (matrix1.Rows == matrix2.Rows && matrix1.Columns == matrix2.Columns)
+            {
+                double[,] array = new double[matrix1.Rows, matrix1.Columns];
+
+                for (int i = 0; i < matrix1.Rows; i++)
+                {
+                    for (int j = 0; j < matrix1.Columns; j++)
+                    {
+                        array[i, j] = matrix1.data[i, j] + matrix2.data[i, j];
+                    }
+                }
+
+                return new MyMatrix(array);
+            }
+            else
+            {
+                throw new Exception("Размер матриц должен совпадать");
+            }
+        }
+
+        public static MyMatrix operator -(MyMatrix matrix1, MyMatrix matrix2)
+        {
+            if (matrix1.Rows == matrix2.Rows && matrix1.Columns == matrix2.Columns)
+            {
+                double[,] array = new double[matrix1.Rows, matrix1.Columns];
+
+                for (int i = 0; i < matrix1.Rows; i++)
+                {
+                    for (int j = 0; j < matrix1.Columns; j++)
+                    {
+                        array[i, j] = matrix1.data[i, j] - matrix2.data[i, j];
+                    }
+                }
+
+                return new MyMatrix(array);
+            }
+            else
+            {
+                throw new Exception("Размер матриц должен совпадать");
+            }
+        }
+
+        public static MyMatrix operator *(MyMatrix matrix1, double num)
+        {
+            double[,] array = new double[matrix1.Rows, matrix1.Columns];
+
+            for (int i = 0; i < matrix1.Rows; i++)
+            {
+                for (int j = 0; j < matrix1.Columns; j++)
+                {
+                    array[i, j] = matrix1.data[i, j] * num;
+                }
+            }
+
+            return new MyMatrix(array);
+        }
+
+        public static MyMatrix operator *(double num, MyMatrix matrix1)
+        {
+            return matrix1 * num;
+        }
+
+        public override string ToString()
+        {
+            StringBuilder sb = new StringBuilder("", Rows * Columns * 2);
+
+            for (int i = 0; i < Rows; i++)
+            {
+                for (int j = 0; j < Columns; j++)
+                {
+                    if (j + 1 != Columns)
+                    {
+                        sb.Append($"{data[i, j]}, ");
+                    }
+                    else
+                    {
+                        sb.Append($"{data[i, j]}");
+                    }
+                }
+                sb.Append("\n");
+            }
+
+            return sb.ToString();
+        }
+    }
+}