TodorMitev

matrixCalculator

Jan 4th, 2012
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.66 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace _06ClassMatrix
  7. {
  8.     class Matrix
  9.     {
  10.         public const int rows = 3;
  11.         public const int cols = 3;
  12.         private double[,] matrix = new double[rows, cols];
  13.         public double this[int rows, int cols]
  14.         {
  15.             get { return matrix[rows, cols]; }
  16.             set { matrix[rows, cols] = value; }
  17.         }
  18.         public static Matrix operator +(Matrix matrix1, Matrix matrix2)
  19.         {
  20.             Matrix finalMatrix = new Matrix();
  21.  
  22.             for (int i = 0; i < rows ; i++)
  23.             {
  24.                 for (int j = 0; j < cols; j++)
  25.                 {
  26.                     finalMatrix[i, j] = matrix1[i, j] + matrix2[i, j];
  27.                 }
  28.             }
  29.             return finalMatrix;
  30.         }
  31.         public static Matrix operator *(Matrix matrix1, Matrix matrix2)
  32.         {
  33.             Matrix finalMatrix = new Matrix();
  34.             for (int i = 0; i < rows; i++)
  35.             {
  36.                
  37.                 for (int j = 0; j < cols; j++)
  38.                 {
  39.                     finalMatrix[i, j] = 0;
  40.                     for (int p = 0; p < rows;p++ )//less than "rows" cause it's 3x3 matrix...and it doesn't metter rows or cols
  41.                         finalMatrix[i, j] += matrix1[i, p] * matrix2[p, j];
  42.                 }
  43.             }
  44.             return finalMatrix;
  45.         }
  46.         public static Matrix operator -(Matrix matrix1, Matrix matrix2)
  47.         {
  48.             Matrix finalMatrix = new Matrix();
  49.             for (int i = 0; i < rows; i++)
  50.             {
  51.                 for (int j = 0; j < cols; j++)
  52.                 {
  53.                     finalMatrix[i, j] = matrix1[i, j] - matrix2[i, j];
  54.                 }
  55.             }
  56.             return finalMatrix;
  57.         }
  58.     }
  59.  
  60.     class ClassMatrix
  61.     {
  62.         static void Main(string[] args)
  63.         {
  64.             Matrix firstMatrix = new Matrix();
  65.             Matrix secondMatrix = new Matrix();
  66.  
  67.             Console.WriteLine("--1st--");
  68.             InitMatrix(firstMatrix);
  69.             Console.WriteLine("--2nd--");
  70.             InitMatrix(secondMatrix);
  71.  
  72.             Matrix matrixFinal = new Matrix();
  73.             Console.Write("Choose an operation between them:(+,-,*) ");
  74.             string operation = Console.ReadLine();
  75.             switch (operation)
  76.             {
  77.                 case ("+"):
  78.                     {
  79.                         matrixFinal = firstMatrix + secondMatrix;
  80.                         PrintMatrix(firstMatrix);
  81.                         Console.WriteLine(".......+.......");
  82.                         PrintMatrix(secondMatrix);
  83.                         Console.WriteLine(".......=.......");
  84.                         PrintMatrix(matrixFinal);
  85.                     }
  86.                     break;
  87.                 case ("-"):
  88.                     {
  89.                         matrixFinal = firstMatrix - secondMatrix;
  90.                         PrintMatrix(firstMatrix);
  91.                         Console.WriteLine(".......-.......");
  92.                         PrintMatrix(secondMatrix);
  93.                         Console.WriteLine(".......=.......");
  94.                         PrintMatrix(matrixFinal);
  95.                     }
  96.                     break;
  97.                 case("*"):
  98.                     {
  99.                         matrixFinal = firstMatrix * secondMatrix;
  100.                         PrintMatrix(firstMatrix);
  101.                         Console.WriteLine(".......*.......");
  102.                         PrintMatrix(secondMatrix);
  103.                         Console.WriteLine(".......=.......");
  104.                         PrintMatrix(matrixFinal);
  105.                     }
  106.                     break;
  107.                 default:
  108.                     Console.WriteLine("Invalid choose...try again!");
  109.                     break;
  110.             }
  111.            
  112.  
  113.            
  114.  
  115.         }
  116.         public static void InitMatrix(Matrix matrix)
  117.         {
  118.             Console.WriteLine("Now enter the matrix' elements");
  119.             for (int i = 0; i < Matrix.rows; i++)
  120.             {
  121.                 for (int j = 0; j < Matrix.cols; j++)
  122.                 {
  123.                     Console.Write("[{0},{1}] = ", i, j);
  124.                     matrix[i, j] = double.Parse(Console.ReadLine());
  125.                 }
  126.             }
  127.         }
  128.         public static void PrintMatrix(Matrix matrix)
  129.         {
  130.             for (int i = 0; i < Matrix.rows; i++)
  131.             {
  132.                 for (int j = 0; j < Matrix.cols; j++)
  133.                 {
  134.                     Console.Write("{0} ", matrix[i, j]);
  135.                 }
  136.                 Console.WriteLine();
  137.             }
  138.         }
  139.     }
  140. }
Advertisement
Add Comment
Please, Sign In to add comment