public static void Main(string[] args) { RationalSet set = RationalSet.Load("..\\..\\Fractions.txt"); try { Console.Write("Max: "); Console.WriteLine(set.GetMax().ToString()); Console.Write("Min: "); Console.WriteLine(set.GetMin().ToString()); } catch (InvalidOperationException) { Console.WriteLine("SET IS EMPTY"); } Rational rat = new Rational(1, 2); Console.WriteLine($"Count fractions more then {rat.ToString()} : {set.CountMoreThen(rat)}"); Console.WriteLine($"Count fractions less then {rat.ToString()} : {set.CountLessThen(rat)}"); Polynomial pol = new Polynomial(set); Console.WriteLine("Polynomial:\n" + pol.ToString()); Console.WriteLine("sum"); Console.WriteLine("Polynomial:\n" + (pol + pol).ToString()); }
public static Polynomial operator +(Polynomial firstPol, Polynomial secondPol) { RationalSet resultCoefs = new RationalSet(); for (int i = 0; i < Math.Min(firstPol.coefs.set.Count, secondPol.coefs.set.Count); i++) { resultCoefs.Add(firstPol.coefs.set[i] + secondPol.coefs.set[i]); } if (firstPol.coefs.set.Count < secondPol.coefs.set.Count) { for (int i = firstPol.coefs.set.Count; i < secondPol.coefs.set.Count; i++) { resultCoefs.Add(secondPol.coefs.set[i]); } } else { for (int i = secondPol.coefs.set.Count; i < firstPol.coefs.set.Count; i++) { resultCoefs.Add(firstPol.coefs.set[i]); } } return(new Polynomial(resultCoefs)); }
public static RationalSet Load(string path) { RationalSet set = new RationalSet(); using (StreamReader reader = new StreamReader(path)) { string line; while ((line = reader.ReadLine()) != null) { string[] lines = line.Split('/'); if (lines.Length != 2) { continue; } int num, denum; if (!Int32.TryParse(lines[0].Trim(), out num) || !Int32.TryParse(lines[1].Trim(), out denum)) { continue; } try { set.Add(new Rational(num, denum)); } catch (FormatException) { Console.WriteLine($"Invalid fraction format: num = {num} , denum = {denum}"); } } } return(set); }
public Polynomial(RationalSet coefsList) { coefs = coefsList; }