internal Polynome Substitute(Polynome p2) { int maxLevel = this.maxLevel * p2.maxLevel; int minLevel = this.minLevel * p2.minLevel; var res = new Polynome(maxLevel, minLevel); Polynome tempConstant = p2.MultiplyByPolynome(new Polynome(new Coeficient[] { new Coeficient(0, 1) }));; //get equal polynome for (int i = 0; i < this.coefs.Count; i++) { var thisC = this.coefs[i]; int pow; Polynome temp = p2.MultiplyByPolynome(new Polynome(new Coeficient[] { new Coeficient(0, 1) })); //get equal polynome if (thisC.level >= 1) { for (int l = 1; l < thisC.level; l++) { temp = temp.MultiplyByPolynome(tempConstant); } } else { temp = Polynome.FromStringArray(new string[] { "1" }); } for (int l = 0; l < temp.coefs.Count; l++) { var tempC = temp.coefs[l]; pow = tempC.level; if (thisC.level == 0) { } int c = thisC.coeficient * tempC.coeficient; res.coefs[maxLevel - pow] = res.coefs[maxLevel - pow].AddCoeficient(c); } } res.RemoveUnnecessary(); return(res); }
static void Main(string[] args) { string line; string[] lineParts; Polynome p = null; while ((line = Console.ReadLine()) != null) { lineParts = line.Split(' ', StringSplitOptions.RemoveEmptyEntries); if (lineParts.Length == 0) { Console.WriteLine("Syntax Error"); } else { if (Coeficient.IsValid(lineParts[0])) { p = Polynome.FromStringArray(lineParts); if (p == null) { Console.WriteLine("Syntax Error"); } Console.WriteLine(p); continue; } string[] temp; Polynome p2; switch (lineParts[0]) { case "+": temp = new string[lineParts.Length - 1]; Array.Copy(lineParts, 1, temp, 0, lineParts.Length - 1); p2 = Polynome.FromStringArray(temp); if (p2 == null || p == null) { Console.WriteLine("Syntax Error"); continue; } p = p.AddPolynome(p2); Console.WriteLine(p); break; case "-": temp = new string[lineParts.Length - 1]; Array.Copy(lineParts, 1, temp, 0, lineParts.Length - 1); p2 = Polynome.FromStringArray(temp); if (p2 == null || p == null) { Console.WriteLine("Syntax Error"); continue; } p = p.SubtractPolynome(p2); Console.WriteLine(p); break; case "*": temp = new string[lineParts.Length - 1]; Array.Copy(lineParts, 1, temp, 0, lineParts.Length - 1); p2 = Polynome.FromStringArray(temp); if (p2 == null || p == null) { Console.WriteLine("Syntax Error"); continue; } p = p.MultiplyByPolynome(p2); Console.WriteLine(p); break; case "e": int val; if (p == null || lineParts.Length != 2 || !int.TryParse(lineParts[1], out val)) { Console.WriteLine("Syntax Error"); continue; } Console.WriteLine(p.Eval(val)); break; case "d": if (p == null) { Console.WriteLine("Syntax Error"); continue; } p = p.Derive(); Console.WriteLine(p); break; case "s": temp = new string[lineParts.Length - 1]; Array.Copy(lineParts, 1, temp, 0, lineParts.Length - 1); p2 = Polynome.FromStringArray(temp); if (p2 == null || p == null) { Console.WriteLine("Syntax Error"); continue; } p = p.Substitute(p2); Console.WriteLine(p); break; default: Console.WriteLine("Syntax Error"); break; } } } }