Advertisement
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;
- using System.Threading.Tasks;
- namespace przeciazanie
- {
- class Matrix
- {
- private int[][] mtrx;
- private int x_size;
- private int y_size;
- static Random rnd = new Random();
- public Matrix(int x, int y)
- {
- x_size = x;
- y_size = y;
- mtrx = new int[x][];
- for(int i = 0; i < y; i++)
- {
- mtrx[i] = new int[y];
- }
- randomizeVal();
- }
- void randomizeVal()
- {
- for(int i = 0; i < x_size; i++)
- {
- for (int j = 0; j < y_size; j++)
- {
- mtrx[i][j] = rnd.Next(0, 11);
- }
- }
- }
- bool checkSizesAddSub(Matrix other)
- {
- if (this.x_size != other.x_size) return false;
- if (this.y_size != other.y_size) return false;
- return true;
- }
- bool checkSizesMul(Matrix other)
- {
- if (this.y_size != other.x_size) return false;
- return true;
- }
- public static Matrix operator +(Matrix op1, Matrix op2)
- {
- if (!op1.checkSizesAddSub(op2))
- {
- Console.WriteLine("Nie zgadzaja sie rozmiary");
- return op1;
- }
- Matrix ret = new Matrix(op1.x_size, op2.y_size);
- for (int i = 0; i < op1.x_size; i++)
- {
- for (int j = 0; j < op2.y_size; j++)
- {
- ret.mtrx[i][j] = op1.mtrx[i][j] + op2.mtrx[i][j];
- }
- }
- return ret;
- }
- public static Matrix operator -(Matrix op1, Matrix op2)
- {
- if (!op1.checkSizesAddSub(op2))
- {
- Console.WriteLine("Nie zgadzaja sie rozmiary");
- return op1;
- }
- Matrix ret = new Matrix(op1.x_size, op2.y_size);
- for (int i = 0; i < op1.x_size; i++)
- {
- for (int j = 0; j < op2.y_size; j++)
- {
- ret.mtrx[i][j] = op1.mtrx[i][j] - op2.mtrx[i][j];
- }
- }
- return ret;
- }
- public static Matrix operator *(Matrix op1, Matrix op2)
- {
- if (!op1.checkSizesAddSub(op2))
- {
- Console.WriteLine("Nie zgadzaja sie rozmiary");
- return op1;
- }
- Matrix ret = new Matrix(op1.y_size, op2.x_size);
- for (int i = 0; i < op1.x_size; i++)
- {
- for (int j = 0; j < op2.y_size; j++)
- {
- ret.mtrx[i][j] = 0;
- for(int k = 0; k < op1.y_size; k++)
- {
- ret.mtrx[i][j] += op1.mtrx[i][k] * op2.mtrx[k][j];
- }
- }
- }
- return ret;
- }
- public void showMat()
- {
- Console.WriteLine("Macierz: ");
- for (int i = 0; i < x_size; i++)
- {
- for (int j = 0; j < y_size; j++)
- {
- Console.Write(mtrx[i][j] + "\t");
- }
- Console.WriteLine();
- }
- }
- }
- class Overload
- {
- public void ovlDemo()
- {
- Console.WriteLine("brak parametrow");
- }
- public void ovlDemo(int a)
- {
- Console.WriteLine("Jeden param: " + a);
- }
- public int ovlDemo(int a, int b)
- {
- Console.WriteLine("dwa int param :" + a + " " + b);
- return a + b;
- }
- public double ovlDemo(double a, double b)
- {
- Console.WriteLine("dwa double param :" + a + " " + b);
- return a + b;
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- test1();
- Console.WriteLine();
- testmatrix();
- Console.WriteLine();
- Console.Read();
- }
- static void test1()
- {
- Overload ob = new Overload();
- int resI;
- double resD;
- ob.ovlDemo();
- Console.WriteLine();
- ob.ovlDemo(2);
- Console.WriteLine();
- resI = ob.ovlDemo(4,6);
- Console.WriteLine("Wynik ovlDemo int= " + resI);
- resD = ob.ovlDemo(1.1, 2.32);
- Console.WriteLine("Wynik ovlDemo doub= " + resD);
- }
- static void testmatrix()
- {
- Matrix a = new Matrix(3, 3);
- Matrix b = new Matrix(3, 3);
- a.showMat();
- b.showMat();
- Matrix c = a + b;
- c.showMat();
- Matrix d = a * b;
- d.showMat();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement