public void Sum(Polinom p) { var tempP = p.Head; while (tempP != null) { var temp = Head; while (temp != null) { if (temp.Deg == tempP.Deg)//Если такой уже есть, складываем коэф-ы { temp.Coef += tempP.Coef; break; } if (temp.Next == null)//Если дошли до конца и не нашли похожих, то просто добавляем в конец { temp.Next = new Element { Coef = tempP.Coef, Deg = tempP.Deg }; break; } temp = temp.Next; } tempP = tempP.Next; } }
/// <summary> /// /// </summary> /// <param name="pol1"></param> /// <param name="pol2"></param> /// <returns></returns> static public Polinom operator /(Polinom pol1, Polinom pol2) { Polinom polRes = new Polinom(); if (pol1.Items.Count == 0) { return(polRes); } KeyValuePair <int, float> firstPol1 = pol1.Items.First(); KeyValuePair <int, float> firstPol2 = pol2.Items.First(); int rankOff = firstPol1.Key - firstPol2.Key; float koefOff = firstPol1.Value / firstPol2.Value; if (rankOff >= 0) { polRes.Items.Add(rankOff, koefOff); Polinom pol4 = pol2 * polRes; Polinom pol5 = pol1 - pol4; Polinom pol6 = pol5 / pol2; polRes += pol6; } else { polRes = pol2; } return(polRes); }
/// <summary> /// /// </summary> /// <param name="pol1"></param> /// <param name="pol2"></param> /// <returns></returns> static public Polinom operator *(Polinom pol1, Polinom pol2) { Polinom polRes = new Polinom(); foreach (KeyValuePair <int, float> item1 in pol1.Items) { try { foreach (KeyValuePair <int, float> item2 in pol2.Items) { int rank = item1.Key + item2.Key; float koef = item1.Value * item2.Value; if (polRes.Items.ContainsKey(rank) == false) { polRes.Items.Add(rank, koef); } else { polRes.Items[rank] += koef; } } } catch (Exception ex) { Console.WriteLine($"{ex.Source}-{ex.Message}"); } } return(polRes); }
static void Main(string[] args) { //Создание списков из файлов var p1 = new Polinom("In1.txt"); Console.WriteLine(p1); Console.WriteLine(); var p2 = new Polinom("In2.txt"); Console.WriteLine(p2); Console.WriteLine(); //Возврат строкового представления полинома var str = p1.ToString(); Console.WriteLine(str); Console.WriteLine(); //Вставка монома в полином p1.Insert(2, 3); Console.WriteLine(p1); Console.WriteLine(); //Приведение подобных членов p1.Combine(); Console.WriteLine(p1); Console.WriteLine(); //Удалить эл-т с подобным показателем степени p1.Delete(3); Console.WriteLine(p1); Console.WriteLine(); //Сложить полиномы p1.Sum(p2); Console.WriteLine(p1); Console.WriteLine(); //Взять поизводную p1.Derivate(); Console.WriteLine(p1); Console.WriteLine(); //Вычислить значение полинома в точке x способом (схема Горнера) var value = p2.Value(2); Console.WriteLine(value); Console.WriteLine(); //Удалить из списка все эл-ты с нечётной степенью Console.WriteLine(p2); p2.DeleteOdd(); Console.WriteLine(p2); }
static public Polinom Sort(Polinom polinom) { var items = polinom.Items.OrderByDescending(p => p.Key); Polinom polSort = new Polinom(); foreach (KeyValuePair <int, float> keyVal in items) { polSort.Items.Add(keyVal.Key, keyVal.Value); } return(polSort); }
static void Main(string[] args) { if (args.Length != 2) { Console.WriteLine("polinom.exe <input.txt> <output.txt> "); Environment.Exit(0); } string pathInput = args[0]; string pathOutput = args[1]; if (File.Exists(pathInput) == false) { Console.WriteLine($"File not exist {pathInput}"); Environment.Exit(0); } List <Polinom> items = InputData(pathInput); if (items.Count >= 2) { Polinom polA = items[0]; Polinom polB = items[1]; List <Polinom> outItems = new List <Polinom>(); Polinom polC = Polinom.Sort(polA * polB); Polinom polD = Polinom.Sort(polC / polB); Polinom polE = Polinom.Sort(polC / polA); polA.Name = "a"; polB.Name = "b"; polC.Name = "c"; polD.Name = "d"; polE.Name = "e"; outItems.Add(polA); outItems.Add(polB); outItems.Add(polC); outItems.Add(polD); outItems.Add(polE); OutputData(pathOutput, outItems); } }
/// <summary> /// /// </summary> /// <param name="pol1"></param> /// <param name="pol2"></param> /// <returns></returns> static public Polinom operator -(Polinom pol1, Polinom pol2) { Polinom polRes = new Polinom(); foreach (KeyValuePair <int, float> item in pol1.Items) { try { polRes.Items.Add(item.Key, item.Value); if (pol2.Items.ContainsKey(item.Key)) { polRes.Items[item.Key] -= pol2.Items[item.Key]; if (polRes.Items[item.Key] == 0) { polRes.Items.Remove(item.Key); } pol2.Items.Remove(item.Key); } } catch (Exception ex) { Console.WriteLine($"{ex.Source}-{ex.Message}"); } } try { foreach (KeyValuePair <int, float> item in pol2.Items) { polRes.Items.Add(item.Key, -item.Value); } } catch (Exception ex) { Console.WriteLine($"{ex.Source}-{ex.Message}"); } return(polRes); }
public void SortByDescending() { Combine(); var result = new Polinom(); while (Head != null) { var min = int.MaxValue; var minEl = new Element(); var temp = Head; while (temp != null) { if (temp.Deg < min) { min = temp.Deg; minEl = temp; } temp = temp.Next; } result.Insert(minEl.Coef, minEl.Deg); Delete(minEl.Deg); } Head = result.Head; }
public static Polinom operator *(Polinom pol1, Polinom pol2) { int n = pol1._n + pol2._n - 1; double[] composition = new double[n]; double sum = 0; for (int k = 0; k < n; k++) { sum = 0; int i = 0; while ((i <= pol1._n - 1) && (i <= k)) { int j = 0; while ((i + j <= k) && (j <= pol2._n - 1)) { if (i + j == k) sum = sum + pol1._coef[i] * pol2._coef[j]; j++; } i++; } composition[k] = sum; } Polinom pol = new Polinom(composition, n); return pol; }
static void Main(string[] args) { int n = 4; double[] a = new double[10]; a[0] = 3; a[1] = 5; a[2] = 4; a[3] = 1; Polinom pol1 = new Polinom(a, n); int m = 7; double[] b = new double[10]; b[0] = 7; b[1] = 3; b[2] = 7; b[3] = 9; b[4] = 1; b[5] = 5; b[6] = 6; Polinom pol2 = new Polinom(b, m); Polinom pol3 = new Polinom(); pol1.Print(); pol2.Print(); pol3 = pol1 + pol2; pol3.Print(); pol3 = pol1 - pol2; pol3.Print(); pol3 = pol1 * pol2; pol3.Print(); pol3 = pol1 * 5; pol3.Print(); pol3 = pol1 / 2; pol3.Print(); pol3 = pol1 + 2; pol3.Print(); pol3 = pol1 - 6; pol3.Print(); PLagrange pl = new PLagrange(a, n); Console.WriteLine(pl.Calc(5)); Console.ReadLine(); }
public static Polinom operator /(Polinom pol, double divider) { double[] coef = pol.InitializeArr(); Polinom polinom = new Polinom(coef, pol._n); for (int i = 0; i < polinom._n; i++) polinom._coef[i] = polinom._coef[i] / divider; return polinom; }
public static Polinom operator -(Polinom pol, double subtrahend) { double[] coef = pol.InitializeArr(); Polinom polinom = new Polinom(coef, pol._n); polinom._coef[0] = polinom._coef[0] - subtrahend; return polinom; }
public static Polinom operator -(Polinom pol1, Polinom pol2) { int max = Math.Max(pol1._n, pol2._n); if (pol1._n > pol2._n) { for (int i = pol2._n; i <= pol1._n; i++) pol2._coef[i] = 0; } else { for (int i = pol1._n; i <= pol2._n; i++) pol1._coef[i] = 0; } double[] difference = new double[max]; for (int i = 0; i <= max - 1; i++) { difference[i] = pol1._coef[i] - pol2._coef[i]; } Polinom pol = new Polinom(difference, max); return pol; }
public static Polinom operator +(Polinom pol1, Polinom pol2) { int max = Math.Max(pol1._n, pol2._n); if (pol1._n > pol2._n) { for (int i = pol2._n; i < pol1._n; i++) pol2._coef[i] = 0; } else { for (int i = pol1._n; i < pol2._n; i++) pol1._coef[i] = 0; } double[] sum = new double[max]; for (int i = 0; i < max; i++) { sum[i] = pol1._coef[i] + pol2._coef[i]; } Polinom pol = new Polinom(sum, max); return pol; }
static private List <Polinom> InputData(string path) { bool bFirst = true; Polinom pol1 = new Polinom(); Polinom pol2 = new Polinom(); using (StreamReader st = new StreamReader(path)) { string line; while ((line = st.ReadLine()) != null) { if ((string.IsNullOrWhiteSpace(line)) && pol1.Items.Count != 0) { bFirst = false; } else { string[] str = line.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); if (str.Length == 2) { try { int rank = Int32.Parse(str[1]); float koef = float.Parse(str[0]); if (bFirst) { if (pol1.Items.ContainsKey(rank) == false) { pol1.Items.Add(rank, koef); } else { pol1.Items[rank] += koef; } } else { if (pol2.Items.ContainsKey(rank) == false) { pol2.Items.Add(rank, koef); } else { pol2.Items[rank] += koef; } } } catch (Exception ex) { Console.WriteLine($"{ex.Source}- {ex.Message}"); } } } } } Polinom pola = Polinom.Sort(pol1); Polinom polb = Polinom.Sort(pol2); return(new List <Polinom> { pola, polb }); }