Beispiel #1
0
        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));
        }
Beispiel #2
0
        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);
        }