Advertisement
sashomaga

Override operators and to string

Jan 15th, 2013
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.11 KB | None | 0 0
  1. using System;
  2. using System.Text;
  3. //Write a class Matrix, to holds a matrix of integers. Overload the operators for adding, subtracting and multiplying
  4. //of matrices, indexer for accessing the matrix content and ToString().
  5. class Matrix
  6. {
  7.     public int rows;
  8.     public int cols;
  9.     private int[,] matrix;
  10.  
  11.     public Matrix(int x,int y)
  12.     {
  13.         matrix = new int[x, y];
  14.         rows = x;
  15.         cols = y;
  16.     }
  17.    
  18.     public int this[int x, int y]
  19.     {
  20.         get
  21.         {
  22.             return matrix[x, y];
  23.         }
  24.  
  25.         set
  26.         {
  27.             matrix[x, y] = value;
  28.         }
  29.     }
  30.  
  31.     public static Matrix operator +(Matrix matrix1, Matrix matrix2)
  32.     {
  33.         Matrix result = new Matrix(matrix1.rows, matrix1.cols);
  34.  
  35.         for (int x = 0; x < matrix1.rows ; x++)
  36.         {
  37.             for (int y = 0; y < matrix1.cols; y++)
  38.             {
  39.                 result[x, y] = matrix1[x, y] + matrix2[x, y];
  40.             }
  41.         }
  42.         return result;
  43.     }
  44.  
  45.     public static Matrix operator -(Matrix matrix1, Matrix matrix2)
  46.     {
  47.         Matrix result = new Matrix(matrix1.rows, matrix1.cols);
  48.  
  49.         for (int x = 0; x < matrix1.rows; x++)
  50.         {
  51.             for (int y = 0; y < matrix1.cols; y++)
  52.             {
  53.                 result[x, y] = matrix1[x, y] - matrix2[x, y];
  54.             }
  55.         }
  56.         return result;
  57.     }
  58.  
  59.     public static Matrix operator *(Matrix matrix1, Matrix matrix2)
  60.     {
  61.         Matrix result = new Matrix(matrix1.rows, matrix1.cols);
  62.  
  63.         for (int x = 0; x < matrix1.rows; x++)
  64.         {
  65.             for (int y = 0; y < matrix1.cols; y++)
  66.             {
  67.                 result[x, y] = matrix1[x, y] * matrix2[y, x];
  68.             }
  69.         }
  70.         return result;
  71.     }
  72.  
  73.     public override string ToString()
  74.     {
  75.         StringBuilder result = new StringBuilder();
  76.         for (int x = 0; x < this.rows; x++)
  77.         {
  78.             for (int y = 0 ;y  < this.cols;y ++)
  79.             {
  80.                 if (y == 0)
  81.                 {
  82.                     result.Append("\n{").Append(this[x, y]);
  83.                 }
  84.                 else
  85.                 {
  86.                     result.Append(",").Append(this[x, y]);
  87.                 }
  88.                 if (y == this.cols - 1)
  89.                 {
  90.                     result.Append("}");
  91.                 }
  92.             }
  93.         }
  94.         return result.ToString();
  95.     }
  96.  
  97. }
  98.  
  99. class Program
  100. {
  101.  
  102.     static void Main()
  103.     {
  104.  
  105.         Matrix m1 = new Matrix(5,5);
  106.         Matrix m2 = new Matrix(5,5);
  107.         Random generator = new Random();
  108.         for (int x = 0; x < m1.rows; x++)
  109.         {
  110.             for (int y = 0; y < m1.cols; y++)
  111.             {
  112.                 m1[x, y] = generator.Next(100);
  113.                 m2[x, y] = generator.Next(100);
  114.             }
  115.         }
  116.  
  117.         Console.Write("Adding:");
  118.         Console.WriteLine((m1+m2).ToString());
  119.  
  120.         Console.Write("Subtracting:");      
  121.         Console.WriteLine((m1 - m2).ToString());
  122.                
  123.         Console.Write("Multiplying:");
  124.         Console.WriteLine((m1 * m2).ToString());
  125.     }
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement