Question

C++ problem: Make a program that the user enter the degree and coefficients of a polynomial....

C++ problem:

Make a program that the user enter the degree and coefficients of a polynomial. Use the overload operators mentioned in the class below:

#include <iostream>

#include <stdlib.h>

using namespace std;

class Polynomial{

public:

  

Polynomial(); //Default constructor: it creates an Polynomial of degree 99

Polynomial(int dg); //Special constructor: it creates an Polynomial of degree dg

Polynomial(const Polynomial &Original); //Copy Constructor

~Polynomial(); //Destructor: deallocate memory

void readPolynomial(); //readPolynomial Method: Reads all positions of the Polynomial

void printPolynomial(); //printPolynomial Method: Prints all positions of the Polynomial

void printPolynomial(int n); //printPolynomial Method: Prints the n first positions of the Polynomial

int getDegree(); //getSize Method: return the Polynomial degree

void generatePolynomial(); //generatePolynomial Method: Generates the elements for the Polynomial

void operator+=(const Polynomial &B); //Overload the Operator += for using with Polynomial

Polynomial *operator+(const Polynomial &B); //Overload the Operator + for using with Polynomial

void operator=(const Polynomial &B);

void initPolynomial(int i);

Polynomial *operator *(const Polynomial &B); //Overloading * Operator for Polynomial*Polynomial

void operator*=(const Polynomial &B);//Overloading *= Operator for Polynomial*Polynomial

Polynomial *operator*(int val);//Overloading * Operator for Polynomial*int

void operator*=(int val);//Overloading *= Operator for Polynomial*int

  

  

private:

double *Coef;

int degree;

};

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <iostream>
#include <stdlib.h>
#include <cmath>

using namespace std;
class Polynomial{
public:
    Polynomial(); //Default constructor: it creates an Polynomial of degree 99
    Polynomial(int dg); //Special constructor: it creates an Polynomial of degree dg
    Polynomial(const Polynomial &Original); //Copy Constructor
    ~Polynomial(); //Destructor: deallocate memory
    void readPolynomial(); //readPolynomial Method: Reads all positions of the Polynomial
    void printPolynomial(); //printPolynomial Method: Prints all positions of the Polynomial
    void printPolynomial(int n); //printPolynomial Method: Prints the n first positions of the Polynomial
    int getDegree(); //getSize Method: return the Polynomial degree
    void generatePolynomial(); //generatePolynomial Method: Generates the elements for the Polynomial
    void operator+=(const Polynomial &B); //Overload the Operator += for using with Polynomial
    Polynomial *operator+(const Polynomial &B); //Overload the Operator + for using with Polynomial
    void operator=(const Polynomial &B);
    void initPolynomial(int i);
    Polynomial *operator *(const Polynomial &B); //Overloading * Operator for Polynomial*Polynomial
    void operator*=(const Polynomial &B);//Overloading *= Operator for Polynomial*Polynomial
    Polynomial *operator*(int val);//Overloading * Operator for Polynomial*int
    void operator*=(int val);//Overloading *= Operator for Polynomial*int
private:
    double *Coef;
    int degree;
};

Polynomial::Polynomial() {
    this->degree = 99;
}

Polynomial::Polynomial(int dg) {
    this->degree = dg;
}

Polynomial::Polynomial(const Polynomial &Original) {
    degree = Original.degree;
    Coef = new double[degree];
    for (int i = 0; i < degree; ++i) {
        Coef[i] = Original.Coef[i];
    }

}

Polynomial::~Polynomial() {
    delete(this->Coef);
}

void Polynomial::readPolynomial() {
    Coef = new double[this->degree];
    cout << "Enter coefficient of polynomial? " << endl;
    double coeff;
    for (int i = 0; i < this->degree; ++i) {
        cin >> coeff;
        Coef[i] = coeff;
    }
}

void Polynomial::printPolynomial() {
    for (int i = 0; i < this->degree; ++i) {
        cout << this->Coef[i] << "\t";
    }
    cout << endl;
}

int Polynomial::getDegree() {
    return this->degree;
}

void Polynomial::printPolynomial(int n) {
    if (n <= this->degree) {
        for (int i = 0; i < n; ++i) {
            cout << this->Coef[i] << "\t";
        }
        cout << endl;
    } else{
        cout << "Not enough coefficient to print\n";
    }
}

void Polynomial::generatePolynomial() {
    string polynomial = "";
    int deg = this->degree;
    for (int i = 0; i < this->degree; ++i) {
        if (i < this->degree -1) {
            polynomial += to_string(this->Coef[i]) + "a^" + to_string(deg - i) + " + ";
        } else{
            polynomial += to_string(this->Coef[i]) + "a";
        }
    }
    cout << polynomial << endl;
}

void Polynomial::operator+=(const Polynomial &B) {
    //degree = B.degree;
    for (int i = 0; i < degree; ++i) {
        Coef[i] = Coef[i] + B.Coef[i];
    }
}

Polynomial *Polynomial::operator+(const Polynomial &B) {
    degree = B.degree;
    for (int i = 0; i < B.degree; ++i) {
        Coef[i] += B.Coef[i];
    }
    return this;
}

void Polynomial::operator=(const Polynomial &B) {
    degree = B.degree;
    for (int i = 0; i < degree; ++i) {
        Coef[i] = B.Coef[i];
    }
}

Polynomial *Polynomial::operator*(const Polynomial &B) {

    degree += B.degree;
    for (int i = 0; i < B.degree; ++i) {
        Coef[i] *= B.Coef[i];
    }
    return this;
}

void Polynomial::initPolynomial(int i) {
    // Not sure what you want to ask here?
}

void Polynomial::operator*=(const Polynomial &B) {
    //degree = degree * B.degree;
    for (int i = 0; i < degree; ++i) {
        Coef[i] = Coef[i] * B.Coef[i];
    }
}

Polynomial *Polynomial::operator*(int val) {
    for (int i = 0; i < degree; ++i) {
        Coef[i] *= val;
    }
    return this;
}

void Polynomial::operator*=(int val) {
    for (int i = 0; i < this->degree; ++i) {
        Coef[i] = Coef[i] * val;
    }
}


//Driver function
int main() {
    Polynomial p(3);
    p.readPolynomial();
    p.printPolynomial();
    p.generatePolynomial();
    Polynomial p1 = p;
    p1.printPolynomial();
    p1.generatePolynomial();
    Polynomial p2 = *(p + p1);
    p2.printPolynomial();
    p2.generatePolynomial();
    p2 *= 2;
    p2.printPolynomial();
    Polynomial p3 = p2;
    p2.printPolynomial();
}
Add a comment
Know the answer?
Add Answer to:
C++ problem: Make a program that the user enter the degree and coefficients of a polynomial....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C++ NEED AS SOON AS POSSIBLE! BigInt class is used for the mathematical operations that involve...

    C++ NEED AS SOON AS POSSIBLE! BigInt class is used for the mathematical operations that involve very big integer calculations that are outside the limit of all available primitive data types. For example, factorial of 100 contains 158 digits in it so we can’t store it in any primitive data type available. We can store as large Integer as we want in it. Your goal is to overload the operators for a generic “BigInt” class. You will need to write...

  • In C++: Implement a class Polynomial that uses a dynamic array of doubles to store the...

    In C++: Implement a class Polynomial that uses a dynamic array of doubles to store the coefficients for a polynomial. This class does not need the method the overloaded += operator. Your class should have the following methods: Write one constructor that takes an integer n for the degree of a term and a double coefficient c for the coefficient of the term and creates the polynomial c x^n. (This constructor can be given default arguments easily to have a...

  • C++ LinkedList I need the code for copy constructor and assignment operator #include <iostream> #include <string> using namespace std; typedef string ItemType; struct Node {    ItemType va...

    C++ LinkedList I need the code for copy constructor and assignment operator #include <iostream> #include <string> using namespace std; typedef string ItemType; struct Node {    ItemType value;    Node *next; }; class LinkedList { private:    Node *head;    // You may add whatever private data members or private member functions you want to this class.    void printReverseRecursiveHelper(Node *temp) const; public:    // default constructor    LinkedList() : head(nullptr) { }    // copy constructor    LinkedList(const LinkedList& rhs);    // Destroys all the dynamically allocated memory    //...

  • Finish the class Matrix.h: class Matrix { public: Matrix(); //default constructor ~Matrix(); //destructor...

    Finish the class Matrix.h: class Matrix { public: Matrix(); //default constructor ~Matrix(); //destructor Matrix(const Matrix &); //copy constror Matrix(int row, int col); Matrix operator+(const Matrix &)const; //overload operator“+” Matrix& operator=(const Matrix &); //overload operator“=” Matrix transpose()const; //matrix transposition void display()const; //display the data private: int row; //row int col; //column int** mat; //to save the matrix }; //main.cpp #include <iostream> #include"Matrix.h" using namespace std; int main() { int row, col; cout << "input the row and the col for Matrix...

  • Finish the class Matrix.h: class Matrix { public: Matrix(); //default constructor ~Matrix(); //destructor Matrix(const Matrix &);...

    Finish the class Matrix.h: class Matrix { public: Matrix(); //default constructor ~Matrix(); //destructor Matrix(const Matrix &); //copy constror Matrix(int row, int col); Matrix operator+(const Matrix &)const; //overload operator“+” Matrix& operator=(const Matrix &); //overload operator“=” Matrix transpose()const; //matrix transposition void display()const; //display the data private: int row; //row int col; //column int** mat; //to save the matrix }; //main.cpp #include <iostream> #include"Matrix.h" using namespace std; int main() { int row, col; cout << "input the row and the col for Matrix...

  • C++ Implement a class template for a LinkedList.(doubly linked). Also, your class will have a tailPtr...

    C++ Implement a class template for a LinkedList.(doubly linked). Also, your class will have a tailPtr in addition to a headPtr along with methods to get, set, insert and remove values at either end of the list. Call these getFirst, getLast, setFirst, setLast, insertFirst, insertLast, removeFirst, removeLast. Don't forget, you also need a copy constructor and destructor plus getLength, isEmpty and clear methods. Overload the stream insertion operator as a friend function which outputs the list in format { 1,...

  • Need to implement this function, mostly confused withcopy constructor , operator overloading and deleteNode#ifndef TREE_H #define...

    Need to implement this function, mostly confused withcopy constructor , operator overloading and deleteNode#ifndef TREE_H #define TREE_H #include <iostream> #include <cstdlib> // necessary in order to use NULL class TreeNode { public: TreeNode() : left(NULL), right(NULL) {}    TreeNode* left; TreeNode* right; int value; }; class Tree { public: // Default constructor Tree();    // Copy constructor Tree(const Tree& other);    //Destructor ~Tree();    // overloaded Assignment Operator Tree& operator=(const Tree& other);    // Similar to insert function we discussed...

  • Please show me how to overload the operators << and >> #ifndef LINK_LIST #define LINK_LIST #include...

    Please show me how to overload the operators << and >> #ifndef LINK_LIST #define LINK_LIST #include <iostream> using namespace std; template <typename T> struct Int_Node {    T value;    Int_Node<T> *pre, *next; }; template <typename T> class Link_List {    template <typename U>    friend ostream &operator<<(ostream &, const Link_List<U> &);// print all integers in the list    template <typename U>    friend istream &operator>>(istream &, Link_List<U> &);// input a value at the back of the list, like insert_node(val);...

  • Greetings, everybody I already started working in this program. I just need someone to help me...

    Greetings, everybody I already started working in this program. I just need someone to help me complete this part of the assignment (my program has 4 parts of code: main.cpp; Student.cpp; Student.h; StudentGrades.cpp; StudentGrades.h) Just Modify only the StudentGrades.h and StudentGrades.cpp files to correct all defect you will need to provide implementation for the copy constructor, assignment operator, and destructor for the StudentGrades class. Here are my files: (1) Main.cpp #include <iostream> #include <string> #include "StudentGrades.h" using namespace std; int...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT