public void PrintResult(Polynom sum, Polynom mul, Polynom sub, Polynom sumNum, int calculate) { Console.Write("Сумма полиномов: "); Console.WriteLine(sum.ToString()); Console.Write("Умножение полиномов: "); Console.WriteLine(mul.ToString()); Console.Write("Разница полиномов: ", sub.ToString()); Console.WriteLine(sub.ToString()); Console.Write("Cложение полинома с числом: ", sumNum.ToString()); Console.WriteLine(sumNum.ToString()); Console.Write("Расчет значения полинома: "); Console.WriteLine(calculate.ToString()); Console.ReadLine(); }
public Polynom Multiply(Polynom multiplier) { Polynom poly = new Polynom(); foreach (Part a in multiplier._polynom) { foreach (Part b in this._polynom) { Part mul = new Part(a.Power + b.Power, a.Coef * b.Coef); AddPart(poly._polynom, mul); } } return(poly); }
static void Main(string[] args) { do { string[] separators = { ",", " ", "\n" }; try { Console.WriteLine("Введите первый полином:"); string s = Console.ReadLine(); if (string.IsNullOrEmpty(s) == true) { break; } string[] poly1 = s.Split(separators, StringSplitOptions.RemoveEmptyEntries); Console.WriteLine("Введите второй полином:"); string[] poly2 = Console.ReadLine().Split(separators, StringSplitOptions.RemoveEmptyEntries); double[] input1, input2; input1 = new double[poly1.Length]; input2 = new double[poly2.Length]; for (int i = 0; i < poly1.Length; i++) { input1[i] = double.Parse(poly1[i]); } for (int i = 0; i < poly2.Length; i++) { input2[i] = double.Parse(poly2[i]); } Polynom p1 = new Polynom(input1); Polynom p2 = new Polynom(input2); Console.WriteLine("Сумма полиномов:{0} ", p1 + p2); Console.WriteLine("Разность полиномов:{0} ", p1 - p2); Console.WriteLine("Произведение полиномов:{0} ", p1 * p2); Console.WriteLine("Равенство полиномов:{0} ", p1 == p2); Console.ReadKey(); } catch (FormatException) { Console.WriteLine("Неправильно введены данные! Введите целое число!"); } } while (true); }
public Polynom Add(int number) { Polynom poly = new Polynom(this); Part part = poly._polynom.Find(x => x.Power == 0); if (part == null && number != 0) { poly._polynom.Add(new Part(0, number)); } else { part.Coef += number; } return(poly); }
public static Polynom ReadConsole() { int[] mas = new int[0]; int n = 0; Console.Write("Введите степень полинома: "); n = Convert.ToInt32(Console.ReadLine()); Console.Write("Введите коэфициенты полинома: "); mas = new int[n]; for (int i = 0; i < mas.Length; i++) { mas[i] = Convert.ToInt32(Console.ReadLine()); } Polynom polynom = new Polynom(mas); return(polynom); }
static void Main(string[] args) { //int[] a = { -3, 7, 2 }; // 2x^2 + 7x - 3 //int[] b = { 9, 7, 0, 4 }; // 4x^3 + 7x + 9 Polynom first = Polynom.ReadConsole(); Polynom second = Polynom.ReadConsole(); string s = Polynom.MyEquals(first, second); int x = 2; Polynom sum = first.Add(second); // 4x^3 + 2x^2 + 14x + 6 Polynom mul = first.Multiply(second); // 8 x^5 + 28 x^4 + 2 x^3 + 67 x^2 + 42 x - 27 Polynom sub = first.Subtract(second); // -4 x^3 + 2 x^2 - 12 Polynom sumNum = first.Add(x); // -4 x^3 + 2 x^2 - 12 int calculate = first.Calculate(x); first.PrintResult(sum, mul, sub, sumNum, calculate); ; }
public static string MyEquals(Polynom p, Polynom p1) { string str = ""; foreach (Part a in p._polynom) { foreach (Part b in p1._polynom) { if (a.Coef == b.Coef && a.Power == b.Power) { str = "равно"; } if (a.Coef != (b.Coef) || (a.Power != b.Power)) { str = "неравно "; } } } Console.WriteLine(str); return(str); }
public Polynom Subtract(Polynom polynom) { return(Add(polynom.Multiply(-1))); }