예제 #1
0
        static void Main(string[] args)
        {
            int[] koef1 = { 1, 2, 3 };
            int[] koef2 = { 4, 5, 6, 7 };

            Mnogochlen A = new Mnogochlen(koef1);
            Mnogochlen B = new Mnogochlen(koef2);


            //вывод многочленов
            Console.Write("A= ");
            A.show();
            Console.WriteLine();

            Console.Write("B= ");
            B.show();
            Console.WriteLine();

            try
            {
                Mnogochlen C = A + B;
                Console.Write("A+B= ");
                C.show();
                Console.WriteLine();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }


            try
            {
                Mnogochlen D = A - B;
                Console.Write("A-B= ");
                D.show();
                Console.WriteLine();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }

            try
            {
                Mnogochlen E = A * B;
                Console.Write("A*B= ");
                E.show();
                Console.ReadLine();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
        //вычитание полиномов
        public static Mnogochlen operator -(Mnogochlen A, Mnogochlen B)
        {
            if (A == null || B == null)
            {
                throw new NullReferenceException();
            }

            int resultSize;

            if (A.koef.Length >= B.koef.Length)
            {
                resultSize = A.koef.Length;
            }
            else
            {
                resultSize = B.koef.Length;
            }

            int[] newKoef = new int[resultSize];

            Mnogochlen C = new Mnogochlen(newKoef);

            for (int i = 0; i < C.koef.Length; i++)
            {
                int a;
                if (A.koef.Length <= i)
                {
                    a = 0;
                }
                else
                {
                    a = A.koef[i];
                }

                int b;
                if (B.koef.Length <= i)
                {
                    b = 0;
                }
                else
                {
                    b = B.koef[i];
                }

                C.koef[i] = a - b;
            }
            return(C);
        }
        public static Mnogochlen operator *(Mnogochlen A, Mnogochlen B)
        {
            if (A == null || B == null)
            {
                throw new NullReferenceException();
            }


            int size = A.koef.Length + B.koef.Length - 1;

            int[]      koef   = new int[size];
            Mnogochlen result = new Mnogochlen(koef);

            for (int i = 0; i < A.koef.Length; ++i)
            {
                for (int j = 0; j < B.koef.Length; ++j)
                {
                    koef[i + j] += A.koef[i] * B.koef[j];
                }
            }
            return(result);
        }