Advertisement
Guest User

Untitled

a guest
Apr 24th, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.02 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace przeciazanie
  8. {
  9. class Matrix
  10. {
  11. private int[][] mtrx;
  12. private int x_size;
  13. private int y_size;
  14.  
  15. static Random rnd = new Random();
  16.  
  17. public Matrix(int x, int y)
  18. {
  19. x_size = x;
  20. y_size = y;
  21.  
  22. mtrx = new int[x][];
  23. for(int i = 0; i < y; i++)
  24. {
  25. mtrx[i] = new int[y];
  26. }
  27.  
  28. randomizeVal();
  29. }
  30.  
  31. void randomizeVal()
  32. {
  33. for(int i = 0; i < x_size; i++)
  34. {
  35. for (int j = 0; j < y_size; j++)
  36. {
  37. mtrx[i][j] = rnd.Next(0, 11);
  38. }
  39. }
  40. }
  41.  
  42. bool checkSizesAddSub(Matrix other)
  43. {
  44. if (this.x_size != other.x_size) return false;
  45. if (this.y_size != other.y_size) return false;
  46. return true;
  47. }
  48.  
  49. bool checkSizesMul(Matrix other)
  50. {
  51. if (this.y_size != other.x_size) return false;
  52. return true;
  53. }
  54.  
  55. public static Matrix operator +(Matrix op1, Matrix op2)
  56. {
  57. if (!op1.checkSizesAddSub(op2))
  58. {
  59. Console.WriteLine("Nie zgadzaja sie rozmiary");
  60. return op1;
  61. }
  62.  
  63. Matrix ret = new Matrix(op1.x_size, op2.y_size);
  64.  
  65. for (int i = 0; i < op1.x_size; i++)
  66. {
  67. for (int j = 0; j < op2.y_size; j++)
  68. {
  69. ret.mtrx[i][j] = op1.mtrx[i][j] + op2.mtrx[i][j];
  70. }
  71. }
  72. return ret;
  73. }
  74.  
  75. public static Matrix operator -(Matrix op1, Matrix op2)
  76. {
  77. if (!op1.checkSizesAddSub(op2))
  78. {
  79. Console.WriteLine("Nie zgadzaja sie rozmiary");
  80. return op1;
  81. }
  82.  
  83. Matrix ret = new Matrix(op1.x_size, op2.y_size);
  84.  
  85. for (int i = 0; i < op1.x_size; i++)
  86. {
  87. for (int j = 0; j < op2.y_size; j++)
  88. {
  89. ret.mtrx[i][j] = op1.mtrx[i][j] - op2.mtrx[i][j];
  90. }
  91. }
  92. return ret;
  93. }
  94.  
  95. public static Matrix operator *(Matrix op1, Matrix op2)
  96. {
  97. if (!op1.checkSizesAddSub(op2))
  98. {
  99. Console.WriteLine("Nie zgadzaja sie rozmiary");
  100. return op1;
  101. }
  102.  
  103. Matrix ret = new Matrix(op1.y_size, op2.x_size);
  104. for (int i = 0; i < op1.x_size; i++)
  105. {
  106. for (int j = 0; j < op2.y_size; j++)
  107. {
  108. ret.mtrx[i][j] = 0;
  109. for(int k = 0; k < op1.y_size; k++)
  110. {
  111. ret.mtrx[i][j] += op1.mtrx[i][k] * op2.mtrx[k][j];
  112. }
  113. }
  114. }
  115. return ret;
  116. }
  117.  
  118. public void showMat()
  119. {
  120. Console.WriteLine("Macierz: ");
  121. for (int i = 0; i < x_size; i++)
  122. {
  123. for (int j = 0; j < y_size; j++)
  124. {
  125. Console.Write(mtrx[i][j] + "\t");
  126. }
  127. Console.WriteLine();
  128. }
  129. }
  130. }
  131.  
  132. class Overload
  133. {
  134. public void ovlDemo()
  135. {
  136. Console.WriteLine("brak parametrow");
  137. }
  138. public void ovlDemo(int a)
  139. {
  140. Console.WriteLine("Jeden param: " + a);
  141. }
  142.  
  143. public int ovlDemo(int a, int b)
  144. {
  145. Console.WriteLine("dwa int param :" + a + " " + b);
  146. return a + b;
  147. }
  148.  
  149. public double ovlDemo(double a, double b)
  150. {
  151. Console.WriteLine("dwa double param :" + a + " " + b);
  152. return a + b;
  153. }
  154. }
  155.  
  156. class Program
  157. {
  158. static void Main(string[] args)
  159. {
  160. test1();
  161. Console.WriteLine();
  162.  
  163. testmatrix();
  164. Console.WriteLine();
  165.  
  166. Console.Read();
  167. }
  168.  
  169. static void test1()
  170. {
  171. Overload ob = new Overload();
  172. int resI;
  173. double resD;
  174. ob.ovlDemo();
  175. Console.WriteLine();
  176. ob.ovlDemo(2);
  177. Console.WriteLine();
  178. resI = ob.ovlDemo(4,6);
  179. Console.WriteLine("Wynik ovlDemo int= " + resI);
  180.  
  181. resD = ob.ovlDemo(1.1, 2.32);
  182. Console.WriteLine("Wynik ovlDemo doub= " + resD);
  183. }
  184.  
  185. static void testmatrix()
  186. {
  187. Matrix a = new Matrix(3, 3);
  188. Matrix b = new Matrix(3, 3);
  189. a.showMat();
  190. b.showMat();
  191.  
  192. Matrix c = a + b;
  193. c.showMat();
  194.  
  195. Matrix d = a * b;
  196. d.showMat();
  197. }
  198. }
  199. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement