public static Polinom operator *(Polinom a, Polinom b) { if (a is null || b is null) { throw new ArgumentNullException(); } Polinom c = new Polinom(); int key = 0, val = 0; foreach (var itemA in a.Data) { foreach (var itemB in b.Data) { key = itemA.Key + itemB.Key; val = itemA.Value * itemB.Value; if (c.Data.Keys.Contains(key)) { c.Data[key] += val; } else { c.Data.Add(key, val); } } } return(c); }
public Polinom(Polinom p) { if (p is null) { throw new ArgumentNullException(); } Data = new SortedDictionary <int, int>(p.Data); }
static void Main(string[] args) { int[] exp = new int[] { 11, 2, 5, 7, 3 }; int[] coef = new int[] { -11, 2, 4, 0, 4 }; int[] exp1 = new int[] { 1, 2, 5, 7, 8 }; int[] coef1 = new int[] { -1, -2, 4, 8, 4 }; try { /* Polinom p = new Polinom(exp, coef); * Console.WriteLine(p); * * Polinom p1 = new Polinom(exp1, coef1); * Console.WriteLine(p1); * Console.WriteLine(p+p1); * Console.WriteLine(p - p1); * * Polinom p2 = new Polinom(new int[] { 1, 0 }, new int[] { 2, 5 }); * Console.WriteLine(p2); * Console.WriteLine(p2*p2);*/ Polinom c = null; Polinom a = new Polinom(c); a.GetCoefficient(5); } catch (Exception ex) { WriteLine(ex.Message); Console.WriteLine("hi"); } /* * SortedDictionary<int, int> d = new SortedDictionary<int, int>(); * d.Add(1, 1); * d.Add(4, 6); * d.Add(8, 2); * d.Add(6, 1); * Console.WriteLine(d); * foreach (var itm in d) * { * Console.WriteLine($"{itm.Key} {itm.Value}"); * } * d.Add(6, 7); * foreach (var itm in d) * { * Console.WriteLine($"{itm.Key} {itm.Value}"); * } * */ ReadKey(); }
public static Polinom operator -(Polinom a, Polinom b) { if (a is null || b is null) { throw new ArgumentNullException(); } Polinom c = new Polinom(a); foreach (var item in b.Data) { if (c.Data.Keys.Contains(item.Key)) { c.Data[item.Key] -= item.Value; } else { c.Data.Add(item.Key, -item.Value); } } return(c); }