static void Main(string[] args) { StreamWriter wr = new StreamWriter("result.txt"); Polynom scl = new Polynom(); LinearPolynom scl1 = new LinearPolynom(); int n = 0; while (n != 4) { n = Visualisation.Choice(); if (n != 4) { switch (n) { case 1: { StreamReader rd = new StreamReader("polynom.txt"); Visualisation.Read(ref scl, rd); Visualisation.Read(ref scl1); wr.WriteLine("Polynom:"); Visualisation.Write(scl, wr); wr.WriteLine("Linear Polynom:"); Visualisation.Write(scl1, wr); Polynom t = scl * scl1; wr.WriteLine("Result:"); Visualisation.Write(t, wr); } break; case 2: { StreamReader rd = new StreamReader("polynom.txt"); Visualisation.Read(ref scl, rd); Visualisation.Read(ref scl1); wr.WriteLine("Polynom:"); Visualisation.Write(scl, wr); wr.WriteLine("Linear Polynom:"); Visualisation.Write(scl1, wr); Polynom s = scl / scl1; wr.WriteLine("Result:"); Visualisation.Write(s, wr); } break; case 3: { StreamReader rd = new StreamReader("polynom1.txt"); Visualisation.Read(ref scl, rd); scl.Collect(); wr.WriteLine("Result:"); Visualisation.Write(scl, wr); } break; } } } wr.Close(); }
public static void Read(ref Polynom sc, StreamReader rd) { string s = rd.ReadToEnd(); string[] t = s.Split(' '); for (int i = 0; i < t.Length / 2; i++) { sc.degrees.Add(int.Parse(t[2 * i])); sc.koeff.Add(double.Parse(t[2 * i + 1])); } }
public static Polynom operator /(Polynom s1, LinearPolynom s2) { Polynom s = new Polynom(); if (s2.koeff[1] == 0) { if (s2.koeff[0] == 0) { Console.WriteLine("Divided by zero."); return null; } for (int i = 0; i < s1.degrees.Count; i++) { s.degrees.Add(s1.degrees[i]); s.koeff.Add(s1.koeff[i] / s2.koeff[0]); } return s; } if (s2.koeff[0] == 0) { if (s1.degrees[0] > 0) { s.degrees.Add(s1.degrees[0] - 1); s.koeff.Add(s1.koeff[0] / s2.koeff[1]); } for (int i = 1; i < s1.degrees.Count; i++) { s.degrees.Add(s1.degrees[i] - 1); s.koeff.Add(s1.koeff[i] / s2.koeff[1]); } return s; } double x = - s2.koeff[0] / s2.koeff[1]; s.degrees.Add(s1.degrees[s1.degrees.Count - 1] - 1); s.koeff.Add(s1.koeff[s1.koeff.Count - 1]); for (int i = s1.degrees[s1.degrees.Count - 1] - 1, j = s1.degrees.Count - 1; i > 0; i--) { for (;(j >= 0) && (s1.degrees[j] > i); j--) ; if ((j >= 0) && (s1.degrees[j] == i)) { if (s.koeff[s.koeff.Count - 1] * x + s1.koeff[j] != 0) { if (s.degrees[s.degrees.Count - 1] == i) { s.degrees.Add(i - 1); s.koeff.Add(s.koeff[s.koeff.Count - 1] * x + s1.koeff[j]); } else { s.degrees.Add(i - 1); s.koeff.Add(s1.koeff[j]); } } } else { if (s.degrees[s.degrees.Count - 1] == i) { s.degrees.Add(i - 1); s.koeff.Add(s.koeff[s.koeff.Count - 1] * x); } } } s.degrees.Reverse(); s.koeff.Reverse(); for (int i = 0; i < s.koeff.Count; i++) s.koeff[i] /= s2.koeff[1]; return s; }
public static Polynom operator *(Polynom s1, LinearPolynom s2) { Polynom s = new Polynom(); Polynom t1 = new Polynom(); Polynom t2 = new Polynom(); for (int i = 0; i < s1.degrees.Count; i++) { if (s1.koeff[i] * s2.koeff[0] != 0) { t1.degrees.Add(s1.degrees[i]); t1.koeff.Add(s1.koeff[i] * s2.koeff[0]); } if (s1.koeff[i] * s2.koeff[1] != 0) { t2.degrees.Add(s1.degrees[i] + 1); t2.koeff.Add(s1.koeff[i] * s2.koeff[1]); } } int k = 0, j = 0; for (; (k < t1.degrees.Count) && (j < t2.degrees.Count); ) { if (t1.degrees[k] == t2.degrees[j]) { s.degrees.Add(t1.degrees[k]); s.koeff.Add(t2.koeff[j] + t1.koeff[k]); k++; j++; } else { if (t1.degrees[k] > t2.degrees[j]) { s.degrees.Add(t2.degrees[j]); s.koeff.Add(t2.koeff[j]); j++; } else { if (t1.degrees[k] < t2.degrees[j]) { s.degrees.Add(t1.degrees[k]); s.koeff.Add(t1.koeff[k]); k++; } } } } for (; k < t1.degrees.Count; ) { s.degrees.Add(t1.degrees[k]); s.koeff.Add(t1.koeff[k]); k++; } for (; j < t2.degrees.Count; ) { s.degrees.Add(t2.degrees[j]); s.koeff.Add(t2.koeff[j]); j++; } return s; }
public static void Write(Polynom sc, StreamWriter wr) { wr.WriteLine("Koefficients of polynom:\r\n" + sc.ToString()); }
public static Polynom operator /(Polynom s1, LinearPolynom s2) { Polynom s = new Polynom(); if (s2.koeff[1] == 0) { if (s2.koeff[0] == 0) { Console.WriteLine("Divided by zero."); return(null); } for (int i = 0; i < s1.degrees.Count; i++) { s.degrees.Add(s1.degrees[i]); s.koeff.Add(s1.koeff[i] / s2.koeff[0]); } return(s); } if (s2.koeff[0] == 0) { if (s1.degrees[0] > 0) { s.degrees.Add(s1.degrees[0] - 1); s.koeff.Add(s1.koeff[0] / s2.koeff[1]); } for (int i = 1; i < s1.degrees.Count; i++) { s.degrees.Add(s1.degrees[i] - 1); s.koeff.Add(s1.koeff[i] / s2.koeff[1]); } return(s); } double x = -s2.koeff[0] / s2.koeff[1]; s.degrees.Add(s1.degrees[s1.degrees.Count - 1] - 1); s.koeff.Add(s1.koeff[s1.koeff.Count - 1]); for (int i = s1.degrees[s1.degrees.Count - 1] - 1, j = s1.degrees.Count - 1; i > 0; i--) { for (; (j >= 0) && (s1.degrees[j] > i); j--) { ; } if ((j >= 0) && (s1.degrees[j] == i)) { if (s.koeff[s.koeff.Count - 1] * x + s1.koeff[j] != 0) { if (s.degrees[s.degrees.Count - 1] == i) { s.degrees.Add(i - 1); s.koeff.Add(s.koeff[s.koeff.Count - 1] * x + s1.koeff[j]); } else { s.degrees.Add(i - 1); s.koeff.Add(s1.koeff[j]); } } } else { if (s.degrees[s.degrees.Count - 1] == i) { s.degrees.Add(i - 1); s.koeff.Add(s.koeff[s.koeff.Count - 1] * x); } } } s.degrees.Reverse(); s.koeff.Reverse(); for (int i = 0; i < s.koeff.Count; i++) { s.koeff[i] /= s2.koeff[1]; } return(s); }
public static Polynom operator *(Polynom s1, LinearPolynom s2) { Polynom s = new Polynom(); Polynom t1 = new Polynom(); Polynom t2 = new Polynom(); for (int i = 0; i < s1.degrees.Count; i++) { if (s1.koeff[i] * s2.koeff[0] != 0) { t1.degrees.Add(s1.degrees[i]); t1.koeff.Add(s1.koeff[i] * s2.koeff[0]); } if (s1.koeff[i] * s2.koeff[1] != 0) { t2.degrees.Add(s1.degrees[i] + 1); t2.koeff.Add(s1.koeff[i] * s2.koeff[1]); } } int k = 0, j = 0; for (; (k < t1.degrees.Count) && (j < t2.degrees.Count);) { if (t1.degrees[k] == t2.degrees[j]) { s.degrees.Add(t1.degrees[k]); s.koeff.Add(t2.koeff[j] + t1.koeff[k]); k++; j++; } else { if (t1.degrees[k] > t2.degrees[j]) { s.degrees.Add(t2.degrees[j]); s.koeff.Add(t2.koeff[j]); j++; } else { if (t1.degrees[k] < t2.degrees[j]) { s.degrees.Add(t1.degrees[k]); s.koeff.Add(t1.koeff[k]); k++; } } } } for (; k < t1.degrees.Count;) { s.degrees.Add(t1.degrees[k]); s.koeff.Add(t1.koeff[k]); k++; } for (; j < t2.degrees.Count;) { s.degrees.Add(t2.degrees[j]); s.koeff.Add(t2.koeff[j]); j++; } return(s); }
public static void Write(Polynom sc, StreamWriter wr) { wr.WriteLine("Koefficients of polynom:\r\n" + sc.ToString()); }