Write the definition of the function to overload the operator*(as a member function) for the class polynomialType to multiply two polynomials. Also, write a test program to test the operator

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Write the definition of the function to overload the operator*(as a member function) for the class polynomialType to multiply two polynomials. Also, write a test program to test the operator*

#include <ostream>
#include <istream>
#include <iostream>

using namespace std;
using ::polynomialType;

class polynomialType : public arrayListType<double>
{
    friend ostream& operator<<(ostream&, const polynomialType&); // Overloads the stream insertion operator
    friend istream& operator>>(istream&, polynomialType&);       // Overloads the stream extraction operator

public:
    polynomialType operator+(const polynomialType&);   // Overloads the operator
    polynomialType operator-(const polynomialType&); // Overloads the operator
    polynomialType operator*(const polynomialType&); // Overloads the operator
    double operator()(double x);                       // Overloads the operator 
    polynomialType(int size = 100);                     // constructor
    int min(int x, int y) const;                        // Function to return the smaller of x and y
    int max(int x, int y) const;  // Function to return the larger of x and y

    polynomialType(int size) : arrayListType<double>(size)
    {
        length = size;
        for (int i = 0; i < size; i++)
            list[i] = 0;
    }

    double operator()(double x)
    {
        double value = 0.0;
        for (int i = 0; i < length; i++)
        {
            if (list[i] != 0.0)
                value = value + list[i] * pow(x, i);
        }
        return value;
    }

    polynomialType operator+(const polynomialType& right)
    {
        int size = max(length, right.length);
        polynomialType temp(size); // polynomial to store the sum
        for (int i = 0; i < min(length, right.length); i++)
            temp.list[i] = list[i] + right.list[i];
        if (size == length)
            for (int i = min(length, right.length); i < length; i++)
                temp.list[i] = list[i];
        else
            for (int i = min(length, right.length); i < right.length; i++)
                temp.list[i] = right.list[i];
        return temp;
    }

    polynomialType operator-(const polynomialType& right)
    {
        int size = max(length, right.length);
        polynomialType temp(size); // polynomial to store the difference
        for (int i = 0; i < min(length, right.length); i++)
            temp.list[i] = list[i] - right.list[i];
        if (size == length)
            for (int i = min(length, right.length); i < length; i++)
                temp.list[i] = list[i];
        else
            for (int i = min(length, right.length); i < right.length; i++)
                temp.list[i] = -right.list[i];
        return temp;
    }

    int min(int x, int y) const
    {
        if (x <= y)
            return x;
        else
            return y;
    }
    int max(int x, int y) const
    {
        if (x >= y)
            return x;
        else
            return y;
    }

};

ostream& operator<<(ostream& os, const polynomialType& p)
{
    int indexFirstNonzeroCoeff = 0;
    for (int i = 0; i < p.length; i++) // determine the index of the first nonzero coefficient
        if (p.list[i] != 0.0)
        {
            indexFirstNonzeroCoeff = i;
            break;
        }
    if (indexFirstNonzeroCoeff < p.length)
    {
        if (indexFirstNonzeroCoeff == 0)
            os << p.list[indexFirstNonzeroCoeff] << " ";
        else
            os << p.list[indexFirstNonzeroCoeff] << "x^" << indexFirstNonzeroCoeff << " ";
        for (int i = indexFirstNonzeroCoeff + 1; i < p.length; i++)
        {
            if (p.list[i] != 0.0)
                if (p.list[i] >= 0.0)
                    os << "+ " << p.list[i] << "x^" << i << " ";
                else
                    os << "- " << -p.list[i] << "x^" << i << " ";
        }
    else 
        os << "0";
    return os;
    }

}

istream& operator>>(istream& is, polynomialType& p)
{
    cout << "The degree of this polynomial is: " << p.length - 1 << endl;
    for (int i = 0; i < p.length; i++)
    {
        cout << "Enter the coefficient of x^" << i << ": ";
        is >> p.list[i];
    }
    return is;
}

Expert Solution
Step 1

Given :- Write the definition of the function to overload the operator*(as a member function) for the class polynomialType to multiply two polynomials. Also, write a test program to test the operator*

trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 8 steps with 5 images

Blurred answer
Knowledge Booster
Reference Types in Function
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education