Question

This is C++ programming. Use separate compilation to implement a polynomial ADT that manipulates polynomials in...

This is C++ programming.

Use separate compilation to implement a polynomial ADT that manipulates polynomials in a single variable x (e.g., p = 4 x^5 + 7 x^3 – x^2 + 9 ). For this problem, consider only polynomials whose exponents are non-negative integers. You are required to identify a proper data representation schema to store such polynomials and hide such data from external users of this ADT. Additionally, your ADT will at least include the following member functions:

One default constructor, which creates a polynomial with no terms.

Ex: Polynomial p; // This should create a polynomial with no terms.

One method allowing one to get an entire polynomial by interacting with the user (by writing to cout and reading from cin) to obtain the degree and coefficient of each term in a polynomial. If the user enters nonsense, your program should not crash! It is up to you how to handle this case. The method can look something like this when called:

myPolynomial.readFromUser(); // prompts the user for the degree and
// coefficients and stores the
// results in myPolynomial

degree() // Returns the degree of a polynomial, which is the highest power of a term with a nonzero coefficient.

  E.g: int deg = myPolynomial.degree();

coefficient(power) // Returns the coefficient of the x p o w e r term.

changeCoefficient(newCoefficient, power) // Replaces the coefficient of the x p o w e r term with newCoefficient. (Note: This method should handle the invalid case too. what happens if the power is out of range?? )

A method that multiplies a polynomial by an integer (make this a normal named method - do not overload the multiplication (*) operator).

A method that adds two polynomials ( overload the addition(+) operator as a standalone function).

Overload the division operator (/) as a member function to multiple divide a polynomial by a scalar variable.

A method that prints out a polynomial. Ex: cout<<p; // should print 4 x^5 + 7 x^3 – x^2 + 9 (Here you have to overload the "<<" put operator as a friend function. Because you cannot overload it as a member function)

Overload the subtraction operator(-) as a member function to subtract two polynomials.

Example: p1 = 3 x^3 - 2 x^2 - 3; p2 = 4 x^4 - x^2 + 3 x^1 - 5;

p3 = p1 - p2; // p3 should result in: - 4 x^4 + 3 x^3 - x^2 - 3 x + 2;

Overload the negation operator (-) as a member function to negate a polynomial. (NOTE: This is different from the subtraction operator! The subtraction operator takes an argument - the polynomial you are subtracting - whereas the negation operator takes no arguments.)

Example: p1 =  3 x^3 - 2 x^2 - 3; // -p1 should result in - 3 x^3 + 2 x^2 +3;

Homework Answers

Answer #1

file containing adtpolyomial class.

adtpolynomial.h

#include<iostream>
#include<string.h>
#define max_degree 100000
using namespace std;
class Polynomial{
    int coff[max_degree];
    int deg;

inline bool isInteger(const std::string & s)
{
   if(s.empty() || ((!isdigit(s[0])) && (s[0] != '-') && (s[0] != '+'))) return false ;

   char * p ;
   strtol(s.c_str(), &p, 10) ;

   return (*p == 0) ;
}
public:
    Polynomial (){
    memset (coff, 0, sizeof(coff));
    deg = 0;
    }

    void readFromUser(){
    cout<<"enter deg : "<<endl;
    char s[5000];
    cin>>s;
    while(!isInteger(s)||atoi(s)<0){
        cout<<"invalid deg, Try again."<<endl;
        cout<<"enter deg : "<<endl;
        cin>>s;
    }
        deg = atoi(s);
    for(int i=0;i<=deg;i++){
        cout<<"enter coefficient of x power "<<i<<endl;
        cin>>s;
        while(!isInteger(s)){
            cout<<"Invalid Coefficient, try again"<<endl;
            cout<<"enter coefficient of x power "<<i<<endl;
            cin>>s;
        }
        coff[i]=atoi(s);
    }
    }
    int degree(){
        return deg;
        }
    int coefficient(int power){
    return coff[power];
    }
    void changeCoefficient(int newCoefficient,int power){
        if(power>deg) cout<<"That term does not exist."<<endl;
        else coff[power] = newCoefficient;
    }
    void multiply(int a){
    for(int i=0;i<=deg;i++){
        coff[i] = coff[i]*a;}
    }
    Polynomial operator+(const Polynomial& b) {
         Polynomial a;
         a.deg = max(this->deg,b.deg);
         for(int i=0;i<=a.deg;i++){
            a.coff[i] = this->coff[i] + b.coff[i];
         }
         return a;
      }
       Polynomial operator-(const Polynomial& b) {
         Polynomial a;
         a.deg = max(this->deg,b.deg);
         for(int i=0;i<=a.deg;i++){
            a.coff[i] = this->coff[i] - b.coff[i];
         }
         while(a.coff[a.deg]==0&& a.deg>0) a.deg--;
         return a;
      }
      Polynomial operator-() {
         Polynomial a;
         a.deg = this->deg;
         for(int i=0;i<=a.deg;i++){
            a.coff[i] = -this->coff[i] ;
         }
         return a;
      }

      Polynomial operator/(int b) {
         Polynomial a;
         a.deg = this->deg;
         for(int i=0;i<=a.deg;i++){
            a.coff[i] = this->coff[i]/b;
         }
         return a;
      }
    friend ostream & operator << (ostream &out, Polynomial p);

};

ostream & operator << (ostream &out,Polynomial p)
{   int j=0;
    for(int i=p.degree();i>0;i--){
        if(p.coff[i]!=0){
        if(j!=0&&p.coff[i]>0){out<<"+";}
        if(p.coff[i]==-1){ out<<"-x";}
        else if(p.coff[i]==1){out<<"x";}
        else {out<<p.coff[i]<<"x";}
        if(i==1){}
        else {out<<"^"<<i;}
        j=1;
        }}
        if(p.coff[0]!=0){
        if(j!=0&&p.coff[0]>0){out<<"+";}
        cout<<p.coff[0]<<endl;
        }

    out<<endl;
    return out;
}

/////////////////////////////////////////////////////////////////////////////////////////////////////adtpolynomial.h ends here /////////

file for testing adtpolynomial implementation

test.cpp

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include "adtpolynomial.h"
using namespace std;

int main(void) {
    Polynomial p1;
    Polynomial p2;
    Polynomial p3;
    p1.readFromUser();
    cout<<p1;
    p2.readFromUser();
    cout<<p2;
    p3=p1+p2;
    cout<<p3;
    p3=p1-p2;
    cout<<p3;
    p3=-p1;
    cout<<p3;
    p3.multiply(5);
    cout<<p3;
    p3 = p3/5;
    cout<<p3;
    cout<<p3.degree()<<endl;
    p3.changeCoefficient(7,9);
    p3.changeCoefficient(4,0);
    cout<<p3;
return 0;

}


///////////////////////////////////// test.cpp ends here/////////////////////////////////////////////////

screentshot for sample case p1=x^2+1 , p2 = x-7.

Know the answer?
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
Similar Questions
Implementing Polynomials using Singly Linked List in C++ The Term Class Create a class to represent...
Implementing Polynomials using Singly Linked List in C++ The Term Class Create a class to represent a term in an algebraic expression. As defined here, a term consists of an integer coefficient and a nonnegative integer exponent. E.g. • in the term 4X2, the coefficient is 4 and the exponent 2 • in -6X8, the coefficient is -6 and the exponent 8 Your class will have a constructor that creates a Term object with a coefficient and exponent passed as...
IN C++ - most of this is done it's just missing the bolded part... Write a...
IN C++ - most of this is done it's just missing the bolded part... Write a program that creates a class hierarchy for simple geometry. Start with a Point class to hold x and y values of a point. Overload the << operator to print point values, and the + and – operators to add and subtract point coordinates (Hint: keep x and y separate in the calculation). Create a pure abstract base class Shape, which will form the basis...
Programming Exercise 2: implement the member function moveToNth(...) that removes the item marked by the cursor...
Programming Exercise 2: implement the member function moveToNth(...) that removes the item marked by the cursor and inserts it as the nth element of the list; test your implementation by turning the flag LAB3_TEST2 from 0 to 1 in config.h; - Programming Exercise 3: implement the ListArray member function find(...) that searches for the element given as a parameter; the search starts at the cursor and stops when it finds the element or at the end of the list; the...
1.)Find T5(x), the degree 5 Taylor polynomial of the function f(x)=cos⁡(x) at a=0. T5(x)=   Find all...
1.)Find T5(x), the degree 5 Taylor polynomial of the function f(x)=cos⁡(x) at a=0. T5(x)=   Find all values of x for which this approximation is within 0.003452 of the right answer. Assume for simplicity that we limit ourselves to |x|≤1. |x|≤ 2.) (1 point) Use substitution to find the Taylor series of (e^(−5x)) at the point a=0. Your answers should not include the variable x. Finally, determine the general term an in (e^(−5x))=∑n=0∞ (an(x^n)) e^(−5x)=  +  x +  x^2 +  x^3 + ... = ∑∞n=0...
You are asked to implement a C++ class to model a sorted array of unsigned integers....
You are asked to implement a C++ class to model a sorted array of unsigned integers. The class is to be used in an embedded application that cannot assume the presence of the STL. The array has to be dynamically allocated in such a way that allows programmers using it to specify the required size. Your class should should: (1) provide the appropriate constructors and destructor; (2) provide methods for updating, and showing numbers in/to the array (e.g., to be...
In this assignment, you’ll make an inventory system for a store’s items, including produce and books....
In this assignment, you’ll make an inventory system for a store’s items, including produce and books. The starter program is an inventory system for only produce. 1. Include the price of an item by adding to the Item class the protected data member int priceInDollars that stores the price in dollars of an individual item. Write a public function SetPrice with a single int parameter prcInDllrs and returns nothing. SetPrice assigns the value of prcInDllrs to priceInDollars. Modify the AddItemToInventory...
The decimal values of the Roman numerals are: M D C L X V I 1000...
The decimal values of the Roman numerals are: M D C L X V I 1000 500 100 50 10 5 1 Remember, a larger numeral preceding a smaller numeral means addition, so LX is 60. A smaller numeral preceding a larger numeral means subtraction, so XL is 40. Assignment: Begin by creating a new project in the IDE of your choice. Your project should contain the following: Write a class romanType. An object of romanType should have the following...
Code Example 8-1 1. int count = 1; 2. int item_total = 0; 3. int item...
Code Example 8-1 1. int count = 1; 2. int item_total = 0; 3. int item = 0; 4. while (count < 4) { 5.      cout << "Enter item cost: "; 6.      cin >> item; 7.      item_total += item; 8.      ++count; 9. } 10. int average_cost = round(item_total / count); 11. cout << "Total cost: " << item_total << "\nAverage cost: " << average_cost; (Refer to Code Example 8-1.) If the user enters 5, 10, and 15 at the prompts, the output is: Total...
This will be my third time submitting this question. THERE SHOULD BE NO USE OF CSS...
This will be my third time submitting this question. THERE SHOULD BE NO USE OF CSS OR SWITCH STATEMENTS IN THE JAVASCRIPT. Even though there is stylesheet in the HTML file do no create a new one. Project Standards: Students will use click events to capture user input. Students will use variables to store information needed by their application and keep track of their program’s state. Students will use conditionals to control project flow. Project Task You will be building...
Important Instructions: (1) λ is typed as lambda. (2) Use hyperbolic trig functions cosh(x) and sinh(x)...
Important Instructions: (1) λ is typed as lambda. (2) Use hyperbolic trig functions cosh(x) and sinh(x) instead of ex and e−x. (3) Write the functions alphabetically, so that if the solutions involve cos and sin, your answer would be Acos(x)+Bsin(x). (4) For polynomials use arbitrary constants in alphabetical order starting with highest power of x, for example, Ax2+Bx. (5) Write differential equations with leading term positive, so X′′−2X=0 rather than −X′′+2X=0. (6) Finally you need to simplify arbitrary constants. For...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT