예제 #1
0
        static void Main(string[] args)
        {
            double[]     test2dArray        = { 2, -4, 5 };
            double[]     testAnother2DArray = { 3, 5, 7, 2 };
            MyPolynomial poly1 = new MyPolynomial(test2dArray);
            MyPolynomial poly2 = new MyPolynomial(testAnother2DArray);

            //Show the first polynomial
            Console.WriteLine("The 1st polynomial: ");
            poly1.ToString();
            Console.WriteLine($"Degree: {poly1.GetDegree()} \n");

            //Show the second polynomial
            Console.WriteLine("The 2nd polynomial: ");
            poly2.ToString();
            Console.WriteLine($"Degree: {poly2.GetDegree()} \n");

            //Polynomial 1 + polynomial 2
            Console.WriteLine("Polynomial 1 + polynomial 2");
            poly1.Add(poly2);
            Console.WriteLine();

            //Polynomial 1 * polynomial 2
            Console.WriteLine("Polynomial 1 * polynomial 2");
            poly1.Multiply(poly2);
            Console.WriteLine();

            Console.ReadLine();
        }
예제 #2
0
        public MyPolynomial Multiply(MyPolynomial another)
        {
            //Find the size of the new polynomial
            int size = this._coeffs.Length + another._coeffs.Length - 1;

            double[]     array  = new double[size];
            MyPolynomial result = new MyPolynomial(array);

            //Multiply each of them
            for (int i = this._coeffs.Length - 1; i >= 0; i--)
            {
                //Mutyply the current position array
                //With a line of another
                for (int j = another._coeffs.Length - 1; j >= 0; j--)
                {
                    array[i + j] += this._coeffs[i] * another._coeffs[j];
                }
            }



            //Transfer the array to result
            for (int i = (result._coeffs.Length - 1); i >= 0; i--)
            {
                result._coeffs[i] = array[i];
            }

            string resultAnother = "({0}*x^{1})";

            resultAnother = string.Format(resultAnother, result._coeffs[result._coeffs.Length - 1], result._coeffs.Length - 1);
            for (int i = (result._coeffs.Length - 2); i >= 0; i--)
            {
                resultAnother = string.Concat(resultAnother, " + (" + result._coeffs[i] + "*x^" + (i) + ")");
            }
            Console.WriteLine(resultAnother);

            return(result);
        }
예제 #3
0
        public MyPolynomial Add(MyPolynomial another)
        {
            //Find the size of the new polynomial, this way I dont have to do 2 if statements
            int size = (int)Max(this._coeffs.Length, another._coeffs.Length);

            double[]     array  = new double[size];
            MyPolynomial result = new MyPolynomial(array);

            //Put the original array in
            for (int i = this._coeffs.Length - 1; i >= 0; i--)
            {
                array[i] = this._coeffs[i];
            }

            //Then add another array
            for (int i = another._coeffs.Length - 1; i >= 0; i--)
            {
                array[i] += another._coeffs[i];
            }


            //Transfer the array to result
            for (int i = (result._coeffs.Length - 1); i >= 0; i--)
            {
                result._coeffs[i] = array[i];
            }

            string resultAnother = "({0}*x^{1})";

            resultAnother = string.Format(resultAnother, result._coeffs[result._coeffs.Length - 1], result._coeffs.Length - 1);
            for (int i = (result._coeffs.Length - 2); i >= 0; i--)
            {
                resultAnother = string.Concat(resultAnother, " + (" + result._coeffs[i] + "*x^" + (i) + ")");
            }
            Console.WriteLine(resultAnother);

            return(result);
        }