private static void canIMultiply(Macierz a, Macierz b) { if (a.n != b.m) { throw new InvalidOperationException("It is not possible to multipy those matrices, make sure you input correct matrices."); } }
private static void canIAddorSubtract(Macierz a, Macierz b) { if (a.m != b.m || a.n != b.n) { throw new InvalidOperationException("It is not possible to subtract those matrices, make sure you input two matrices of the same dimensions."); } }
//Multication public static Macierz operator *(Macierz a, Macierz b) { try { canIMultiply(a, b); Macierz newValues = new Macierz(a.n, b.m); for (int i = 0; i < a.n; i++) { for (int j = 0; j < newValues.GetLength(0); j++) { float w = 0; for (int k = 0; k < newValues.GetLength(1); k++) { w += a[i, k] * b[k, j]; } newValues[i, j] = w; } } return(newValues); } catch (InvalidOperationException e) { Console.WriteLine(); Console.WriteLine(e.Message); return(new Macierz(0, 0)); } }
public void pomnoz(Macierz B) { if (this.liczba_w != B.liczba_k) { Console.WriteLine("\n_!_ Ilosc wierszy macierzy 1 musi się równać liczbie kolumn w 2 _!_"); return; } Console.WriteLine(); this.wyswietl(); Console.WriteLine(); B.wyswietl(); Console.WriteLine("\nMnożenie przez macierz: "); int suma = 0; for (int w = 0; w < this.liczba_w; w++) { for (int k = 0; k < B.liczba_k; k++) { suma = 0; for (int i = 0; i < this.liczba_k; i++) { //Console.WriteLine(this.macierz[w, i] + " * " + B.macierz[i, k]); suma += this.macierz[w, i] * B.macierz[i, k]; } Console.Write(suma + " "); //macierzA[i, 0] * macierzB[0, i]; } Console.WriteLine(); } }
static void Main(string[] args) { Console.WriteLine("Program tworzy, dodaje, mnoży i dokonuje transpozycji macierzy.\n" + "= = = = = = = = = = ="); Console.WriteLine("\n\nNacisnij Enter, aby zacząć."); Console.ReadLine(); Macierz macierz = new Macierz(); macierz.StworzMacierze(); }
static public void Menu() { bool koniec = false; Dictionary <string, Macierz> Macierze = new Dictionary <string, Macierz>(); string nazwa; do { Instrukcja(); switch (Console.ReadKey(true).Key) { case ConsoleKey.D1: var macierz = new Macierz(); Macierze[macierz.nazwa] = macierz; break; case ConsoleKey.D2: szukaj(Macierze, "0").wypelnij(); break; case ConsoleKey.D3: szukaj(Macierze, "0").wypelnijLosowymi(); break; case ConsoleKey.D4: szukaj(Macierze, "0").wyswietl(); break; case ConsoleKey.D5: operacje(Macierze); break; case ConsoleKey.K: koniec = true; return; default: Console.WriteLine("___Wybierz opcje z menu!___"); break; } Console.WriteLine("\n_>_ Wciśnij dowolny przycisk aby kontynuować _<_"); Console.ReadKey(); Console.Clear(); } while (koniec == false); }
//Subtracting public static Macierz operator -(Macierz a, Macierz b) { try { canIAddorSubtract(a, b); Macierz newValues = new Macierz(a.m, a.n); for (int i = 0; i < a.m; i++) { for (int j = 0; j < a.n; j++) { newValues[i, j] = a.values[i, j] - b.values[i, j]; } } return(newValues); } catch (Exception e) { Console.WriteLine(); Console.WriteLine(e.Message); return(new Macierz(0, 0)); } }
public void odejmij(Macierz B) { Console.WriteLine(); this.wyswietl(); Console.WriteLine(); B.wyswietl(); if (this.liczba_w != B.liczba_w || this.liczba_k != B.liczba_k) { Console.WriteLine("\n_!_ Ilosc wierszy i kolumn w obu macierzach musi się zgadzać _!_"); return; } Console.WriteLine("\nRóznica: "); for (int w = 0; w < this.liczba_w; w++) { for (int k = 0; k < this.liczba_k; k++) { Console.Write((this.macierz[w, k] + B.macierz[w, k]).ToString() + " "); } Console.WriteLine(); } }