简体   繁体   中英

How can I display two matrices side by side in c#?

I made a program that calculates multiplication of matrices in c#. Is it possible to show the result like this?

(Matrix1) * (Matrix2) = (Result)

My code :

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);

        }
    }
}

One way to do this using SetCursorPosition would be to modify your ShowMatrix method to first store the starting position of the cursor, and then reset the cursor to the original "left" value while incrementing the "top" value for each row. Note I'm also using a variable to track the column width, which I'm using to ensure that each column is the same width for each grid. This value may need to be increased if you use large numbers:

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
    }
}

Then, when you call the method, you can reset the cursor position back to the top and one space more to the right from the last grid that was shown (by multiplying the number of columns by the column width constant):

    // 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...");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM