예제 #1
0
파일: Program.cs 프로젝트: DFirsa/itmo-OOP
        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());
        }
예제 #2
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));
        }
예제 #3
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);
        }
예제 #4
0
 public Polynomial(RationalSet coefsList)
 {
     coefs = coefsList;
 }