Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace _06ClassMatrix
- {
- class Matrix
- {
- public const int rows = 3;
- public const int cols = 3;
- private double[,] matrix = new double[rows, cols];
- public double this[int rows, int cols]
- {
- get { return matrix[rows, cols]; }
- set { matrix[rows, cols] = value; }
- }
- public static Matrix operator +(Matrix matrix1, Matrix matrix2)
- {
- Matrix finalMatrix = new Matrix();
- for (int i = 0; i < rows ; i++)
- {
- for (int j = 0; j < cols; j++)
- {
- finalMatrix[i, j] = matrix1[i, j] + matrix2[i, j];
- }
- }
- return finalMatrix;
- }
- public static Matrix operator *(Matrix matrix1, Matrix matrix2)
- {
- Matrix finalMatrix = new Matrix();
- for (int i = 0; i < rows; i++)
- {
- for (int j = 0; j < cols; j++)
- {
- finalMatrix[i, j] = 0;
- for (int p = 0; p < rows;p++ )//less than "rows" cause it's 3x3 matrix...and it doesn't metter rows or cols
- finalMatrix[i, j] += matrix1[i, p] * matrix2[p, j];
- }
- }
- return finalMatrix;
- }
- public static Matrix operator -(Matrix matrix1, Matrix matrix2)
- {
- Matrix finalMatrix = new Matrix();
- for (int i = 0; i < rows; i++)
- {
- for (int j = 0; j < cols; j++)
- {
- finalMatrix[i, j] = matrix1[i, j] - matrix2[i, j];
- }
- }
- return finalMatrix;
- }
- }
- class ClassMatrix
- {
- static void Main(string[] args)
- {
- Matrix firstMatrix = new Matrix();
- Matrix secondMatrix = new Matrix();
- Console.WriteLine("--1st--");
- InitMatrix(firstMatrix);
- Console.WriteLine("--2nd--");
- InitMatrix(secondMatrix);
- Matrix matrixFinal = new Matrix();
- Console.Write("Choose an operation between them:(+,-,*) ");
- string operation = Console.ReadLine();
- switch (operation)
- {
- case ("+"):
- {
- matrixFinal = firstMatrix + secondMatrix;
- PrintMatrix(firstMatrix);
- Console.WriteLine(".......+.......");
- PrintMatrix(secondMatrix);
- Console.WriteLine(".......=.......");
- PrintMatrix(matrixFinal);
- }
- break;
- case ("-"):
- {
- matrixFinal = firstMatrix - secondMatrix;
- PrintMatrix(firstMatrix);
- Console.WriteLine(".......-.......");
- PrintMatrix(secondMatrix);
- Console.WriteLine(".......=.......");
- PrintMatrix(matrixFinal);
- }
- break;
- case("*"):
- {
- matrixFinal = firstMatrix * secondMatrix;
- PrintMatrix(firstMatrix);
- Console.WriteLine(".......*.......");
- PrintMatrix(secondMatrix);
- Console.WriteLine(".......=.......");
- PrintMatrix(matrixFinal);
- }
- break;
- default:
- Console.WriteLine("Invalid choose...try again!");
- break;
- }
- }
- public static void InitMatrix(Matrix matrix)
- {
- Console.WriteLine("Now enter the matrix' elements");
- for (int i = 0; i < Matrix.rows; i++)
- {
- for (int j = 0; j < Matrix.cols; j++)
- {
- Console.Write("[{0},{1}] = ", i, j);
- matrix[i, j] = double.Parse(Console.ReadLine());
- }
- }
- }
- public static void PrintMatrix(Matrix matrix)
- {
- for (int i = 0; i < Matrix.rows; i++)
- {
- for (int j = 0; j < Matrix.cols; j++)
- {
- Console.Write("{0} ", matrix[i, j]);
- }
- Console.WriteLine();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment