如何在C#中并排显示两个矩阵?

时间:2018-10-01 23:39:37

标签: c# matrix

我制作了一个程序,用于计算c#中矩阵的乘法。可以显示这样的结果吗?

(Matrix1)*(Matrix2)=(结果)

我的代码:

using System;

namespace MatrixMultiplication;
{
    class MainClass
    {
        static void ShowMatrix(int [,] m){

            for (int i = 0; i < m.GetLength(0); i++) {
                for (int j = 0; j < m.GetLength(1); j++) {
                    Console.Write(m[i,j]+"    ");
                }
                Console.WriteLine ();
            }
        }

        static int[,] MultiplyMatrices(int [,] a , int [,] b){
            int[,] result = new int[a.GetLength(0),a.GetLength(1)];
            for (int i = 0; i < a.GetLength(0);i++) {
                for (int j = 0; j < a.GetLength(0); j++) {
                    for (int k = 0; k < a.GetLength(1); k++) {
                        result [i, k] += a [i, j] * b [j, k]; 
                    }
                }
            }
            return result;
        }


        public static void Main (string[] args)
        {
            int rows,columns;
            Console.WriteLine ("Rows : ");
            rows = Convert.ToInt32 (Console.ReadLine());
            Console.WriteLine ("Columns : ");
            columns = Convert.ToInt32(Console.ReadLine());
            int [,] lhsm = new int[rows,columns];
            int [,] rhsm = new int[rows,columns];
            int [,] result = new int[rows,columns];

            Console.WriteLine ("Enter Elements of the First matrix : ");
            for (int i = 0; i < rows; i++) {
                for (int j = 0; j < columns; j++) {
                    Console.WriteLine ("F[{0}][{1}] : ",i,j);
                    lhsm [i,j] = Convert.ToInt32 (Console.ReadLine ());
                }
            }

            Console.WriteLine ("Enter Elements of the Second matrix : ");

            for (int i = 0; i < rows; i++) {
                for (int j = 0; j < columns; j++) {
                    Console.WriteLine ("L[{0}][{1}] : ",i,j);
                    rhsm [i,j] = Convert.ToInt32 (Console.ReadLine ());
                }
            }

            result = MultiplyMatrices (lhsm, rhsm);
            Console.Clear ();
            Console.WriteLine ("Matrix 1 : ");
            ShowMatrix (rhsm);
            Console.WriteLine ("Matrix 2 : ");
            ShowMatrix (lhsm);
            Console.WriteLine ("Result : ");
            ShowMatrix (result);

        }
    }
}

1 个答案:

答案 0 :(得分:0)

使用SetCursorPosition进行此操作的一种方法是修改ShowMatrix方法,以首先存储光标的起始位置,然后将光标重置为原始的“ left”值,同时增加每行的“顶部”值。注意我还使用了一个变量来跟踪列的宽度,我正在使用该变量来确保每个列的宽度与每个网格的宽度相同。如果您使用大量数字,则可能需要增加该值:

const int maxColumnWidth = 5;

static void ShowMatrix(int[,] matrix, string name)
{
    var startColumn = Console.CursorLeft;
    var startRow = Console.CursorTop;

    Console.SetCursorPosition(startColumn, startRow);
    Console.Write($"{name}:");
    Console.SetCursorPosition(startColumn, ++startRow); // <-- Note this increments the row

    for (int i = 0; i < matrix.GetLength(0); i++)
    {
        for (int j = 0; j < matrix.GetLength(1); j++)
        {
            Console.Write(matrix[i, j].ToString().PadRight(maxColumnWidth));
        }

        Console.SetCursorPosition(startColumn, ++startRow); // <-- increment row again
    }
}

然后,当您调用该方法时,可以将光标位置重设为顶部,并从显示的最后一个网格向右重设一个空格(通过将列数乘以列宽常量): / p>

    // code to get matrix values omitted

    result = MultiplyMatrices(lhsm, rhsm);
    Console.Clear();

    // Capture initial cursor values
    var cursorTop = Console.CursorTop;
    var cursorLeft = Console.CursorLeft;

    ShowMatrix(lhsm, "Matrix 1");

    // Move cursor just to the right of the previous matrix, and at the same top
    cursorLeft += lhsm.GetLength(1) * maxColumnWidth + 1;
    Console.SetCursorPosition(cursorLeft, cursorTop);

    ShowMatrix(rhsm, "Matrix 2");

    // Move cursor just to the right of the previous matrix, and at the same top
    cursorLeft += rhsm.GetLength(1) * maxColumnWidth + 1;
    Console.SetCursorPosition(cursorLeft, cursorTop);

    ShowMatrix(result, "Result");

    // Move cursor back to the beginning, and just underneath the previous matrix
    Console.SetCursorPosition(0, cursorTop + result.GetLength(0) + 1);

    GetKeyFromUser("\nDone! Press any key to exit...");
}