向量上的乘法矩阵

时间:2021-03-31 23:39:10

标签: c# matrix operator-overloading matrix-multiplication

我需要将向量上的矩阵相乘并打印结果。有整数向量和整数矩阵类。输出时,只显示第一个元素。你能告诉我错误是什么吗?下面我展示了我制作的 * 运算符的重载、显示矩阵的方法以及我如何显示它。 Here is an example

public class VectorInt//class VectorInt
        {
            protected int[] IntArray;
            protected uint size;
            protected int codeError;
            protected static uint num_vec;
            public VectorInt()
            {
                codeError = 1;
                size = 1;
                IntArray = new int[size];
                IntArray[0] = 0;
                num_vec++;
            }
            public VectorInt(uint n)//конструктор з одним параметром
            {
                codeError = 1;
                size = n;
                IntArray = new int[size];
                for (int i = 0; i < n; i++)
                {
                    IntArray[i] = 0;
                }
                num_vec++;
            }
            public VectorInt(uint n, int b)
            {
                codeError = 1;
                size = n;
                IntArray = new int[size];
                for (int i = 0; i < n; i++)
                {
                    IntArray[i] = b;
                }
                num_vec++;
            }

            ~VectorInt()
            {
                num_vec--;
                Console.WriteLine("Dipsosed");
            }

            public void Input()
            {
                Console.WriteLine("\nSize of vector: ");
                size = uint.Parse(Console.ReadLine());
                IntArray = new int[size];

                for (int i = 0; i < size; i++)
                {
                    Console.WriteLine("Element {0}: ", i);
                    Console.WriteLine(" v [ {0} ] = {1} ", i, IntArray[i] = int.Parse(Console.ReadLine()));
                }
            }

    public void Output()
                {
                    Console.WriteLine("\nMatrix");
                    for (int i = 0; i < n; i++)//n-amount of rows
                    {
                        for (int j = 0; j < m; j++)//m-amount of columns
                        {
                            Console.Write(" m [ {0}{1} ] = {2} ", i, j, IntArray[i, j]);
                        }
                        Console.WriteLine();
                    }
                }
public uint Size
            {
                get
                {
                    return (uint)IntArray.Length;
                }
            }
public int[] NewIntArray
            {
                get => IntArray;
            }
////////////////////////////////////////////////////////
public class MatrixInt//class MatrixInt
        {
            protected int[,] IntArray;
            protected int n, m;
            protected int codeError;
            protected static int num_m;

            public MatrixInt()
            {
                n = 1; m = 1;
                IntArray = new int[n, m];
                IntArray[0, 0] = 0;
                num_m++;
            }
            public MatrixInt(int r, int c)
            {
                n = r; m = c;
                IntArray = new int[n, m];
                for (int i = 0; i < n; i++)
                {
                    for (int j = 0; j < m; j++)
                        IntArray[i, j] = 0;
                }
                num_m++;
            }
            public MatrixInt(int r, int c, int b)
            {
                n = r; m = c;
                IntArray = new int[n, m];
                for (int i = 0; i < n; i++)
                {
                    for (int j = 0; j < m; j++)
                        IntArray[i, j] = b;
                }
                num_m++;
            }
            ~MatrixInt()
            {
                num_m--;
                Console.WriteLine("Disposed");
            }

            public void Input()
            {
                Console.WriteLine("Number of rows: ");
                n = int.Parse(Console.ReadLine());
                Console.WriteLine("Number of columns: ");
                m = int.Parse(Console.ReadLine());
                IntArray = new int[n, m];

                for (int i = 0; i < n; i++)
                {
                    for (int j = 0; j < m; j++)
                    {
                        Console.WriteLine("Element {0}{1}: ", i, j);
                        Console.WriteLine(" m [ {0}{1} ] = {2} ", i, j, IntArray[i, j] = int.Parse(Console.ReadLine()));
                    }
                }
            }

            public void Output()
            {
                Console.WriteLine("\nMatrix");
                for (int i = 0; i < n; i++)
                {
                    for (int j = 0; j < m; j++)
                    {
                        Console.Write(" m [ {0}{1} ] = {2} ", i, j, IntArray[i, j]);
                    }
                    Console.WriteLine();
                }
            }
public int Rows
        {
            get
            {
                return n;
            }
        }
        public int Columns
        {
            get
            {
                return m;
            }
        }    

  
/////////////////////////////////////////////////////////////

public static MatrixInt operator *(MatrixInt obj, VectorInt obj1)
                    {
                        MatrixInt obj3 = new MatrixInt();
                         if (obj.Columns != obj1.NewIntArray.Length)
                        {
                                throw new Exception("Error!");
                        }
                        obj3.IntArray = new int[obj.Rows, 1];
                        for (int i = 0; i < obj.Rows; i++)
                        {
                            obj3.IntArray[i, 0] = 0;
                            for (int j = 0; j < obj.Columns; j++)
                            {
                                obj3.IntArray[i, 0] += obj.IntArray[i, j] * obj1.NewIntArray[j];
                                //Console.WriteLine(" m [ {0}{1} ] = {2} ", i, j, obj3.IntArray[i, 0]);
                            }
                        }
                        return obj3;
                    }
////////////////////////////////////////////////////////
                      MatrixInt obj = new MatrixInt();
                                    VectorInt obj1 = new VectorInt();
                                    obj.Input();
                                    obj.Output();
                                    obj1.Input();
                                    obj1.Output();
                                    obj *= obj1;
                                    obj.Output();

0 个答案:

没有答案