ellapt

T8.6.ClassMatrix

Jan 17th, 2013
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.61 KB | None | 0 0
  1. using System;
  2. using System.Text;
  3.  
  4. class ClasMatrix
  5. {
  6. public class NewMatrix
  7. {
  8. private int rows, cols;
  9. private int[,] aMatrix;
  10.  
  11. public NewMatrix(int r, int c)
  12. {
  13. rows = r;
  14. cols = c;
  15. aMatrix = new int[rows, cols];
  16. }
  17. public int this[int r, int c]
  18. {
  19. get
  20. {
  21. return aMatrix[r, c];
  22. }
  23. set
  24. {
  25. aMatrix[r, c] = value;
  26. }
  27. }
  28. public int getRows()
  29. {
  30. return rows;
  31. }
  32.  
  33. public int getCols()
  34. {
  35. return cols;
  36. }
  37.  
  38. public static NewMatrix operator +(NewMatrix operand1, NewMatrix operand2)
  39. {
  40. if (operand1.getRows() != operand2.getRows() || operand1.getCols() != operand2.getCols())
  41. {
  42. throw new Exception("Different dimensions");
  43. }
  44.  
  45. NewMatrix addResult = new NewMatrix(operand1.getRows(), operand1.getCols());
  46.  
  47. for (int i = 0; i < operand1.aMatrix.GetLength(0); i++)
  48. {
  49. for (int j = 0; j < operand2.aMatrix.GetLength(1); j++)
  50. {
  51. addResult[i, j] = operand1[i, j] + operand2[i, j];
  52. }
  53. }
  54. return addResult;
  55. }
  56.  
  57. public static NewMatrix operator -(NewMatrix operand1, NewMatrix operand2)
  58. {
  59. if (operand1.getRows() != operand2.getRows() || operand1.getCols() != operand2.getCols())
  60. {
  61. throw new Exception("Different dimensions");
  62. }
  63. NewMatrix result = new NewMatrix(operand1.getRows(), operand1.getCols());
  64.  
  65. for (int i = 0; i < operand1.aMatrix.GetLength(0); i++)
  66. {
  67. for (int j = 0; j < operand2.aMatrix.GetLength(1); j++)
  68. {
  69. result[i, j] = operand1[i, j] - operand2[i, j];
  70. }
  71. }
  72. return result;
  73. }
  74.  
  75. public static NewMatrix operator *(NewMatrix operand1, NewMatrix operand2)
  76. {
  77. NewMatrix result = new NewMatrix(operand1.getRows(), operand1.getCols());
  78.  
  79. for (int i = 0; i < operand1.getRows(); i++)
  80. {
  81. for (int j = 0; j < operand1.getRows(); j++)
  82. {
  83. int sum=0;
  84. for (int k = 0; k < operand1.getRows(); k++)
  85. {
  86. sum =sum + (operand1[i, k] * operand2[k, j]);
  87. }
  88. result[i, j] = sum;
  89. }
  90. }
  91. return result;
  92. }
  93.  
  94. public override string ToString()
  95. {
  96. StringBuilder aMatrixToString = new StringBuilder();
  97.  
  98. for (int i = 0; i < rows; i++)
  99. {
  100. for (int j = 0; j < cols; j++)
  101. {
  102. aMatrixToString.Append(String.Format("{0,4} ", aMatrix[i, j]));
  103. }
  104. aMatrixToString.Append("\n");
  105. }
  106. return aMatrixToString.ToString();
  107. }
  108.  
  109. }
  110.  
  111. static void Main()
  112. {
  113. Console.WriteLine("Write a class Matrix, to hold a matrix of integers.\nOverload the operators for matrices, \nindexer for accessing the matrix content and ToString()");
  114. int size=0;;
  115. do
  116. {
  117. Console.Write("Enter size of the matrix1 and matrix2: ");
  118. size = int.Parse(Console.ReadLine());
  119. } while (size <= 0);
  120.  
  121. int r1, c1, r2, c2;
  122. c1=r1=r2=c2=size;
  123. NewMatrix m1 = new NewMatrix(r1, c1);
  124. Console.WriteLine("Enter the elements of the matrix1: ");
  125. for (int i = 0; i < r1; i++)
  126. {
  127. for (int j = 0; j < c1; j++)
  128. {
  129. m1[i,j] = int.Parse(Console.ReadLine());
  130. }
  131. }
  132. NewMatrix m2 = new NewMatrix(r2, c2);
  133.  
  134. Console.WriteLine("Enter the elements of the matrix2: ");
  135. for (int i = 0; i < r2; i++)
  136. {
  137. for (int j = 0; j < c2; j++)
  138. {
  139. m2[i,j] = int.Parse(Console.ReadLine());
  140. }
  141. }
  142.  
  143. Console.WriteLine("Test matrices:");
  144. Console.WriteLine(m1);
  145. Console.WriteLine(m2);
  146.  
  147. Console.WriteLine("Addition:");
  148. Console.WriteLine(m1+m2);
  149.  
  150. Console.WriteLine("Subtraction:");
  151. Console.WriteLine(m1-m2);
  152.  
  153. Console.WriteLine("Multiplication:");
  154. Console.WriteLine(m1*m2);
  155.  
  156. }
  157. }
Advertisement
Add Comment
Please, Sign In to add comment