Question: Here is the code provided: Here is the sample input/output file: simplepoly.h file: #ifndef SIMPLEPOLY_H #define SIMPLEPOLY_H class SimplePoly { public: SimplePoly(); SimplePoly(const SimplePoly&); //(creates

 Here is the code provided: Here is the sample input/output file:

Here is the code provided:

Here is the sample input/output file:

simplepoly.h file: #ifndef SIMPLEPOLY_H #define SIMPLEPOLY_H class SimplePoly { public: SimplePoly(); SimplePoly(const

simplepoly.h file:

#ifndef SIMPLEPOLY_H

#define SIMPLEPOLY_H

class SimplePoly { public: SimplePoly(); SimplePoly(const SimplePoly&); //(creates a deep copy) ~SimplePoly(); SimplePoly& operator= (const SimplePoly&); //(creates a deep copy)

void read(); void write() const; // Write out the polynomial to look close to usual written form: // Terms are written out in increasing (or decreasing) order; each // term appears with its coefficient followed by the variable, which // is then followed by the exponent, with special cases appropriately // handled for positive and negative coefficients whether they appear // at the front or later, coeffients of 1 or -1, exponents that are // positive, negative, or zero printed as usually written. // All in all, polynomials look as they are usually written, with the // only exception being, the exponents, if any, are written NOT as // superscripts, but flush with the rest of the line after the variable. SimplePoly plus(const SimplePoly&) const; // Return the sum of the TWO polynomials SimplePoly minus(const SimplePoly&) const; // Return the difference of the TWO polynomials float evaluate(float) const; // Return the evaluation of the poly at the parameter value

private: struct term { int coeff; term(): coeff(0) {}; // only purpose is to facilitate array declaration. term(int c) : coeff(c) {}; char sign() const { return ((coeff = LargestExp+1 termptr terms; // Base pointer for the dynamic array of terms char variable; // The variable of the polynomial

void copy(const SimplePoly&); void free(); void InsertTerm(int c, int e); // Insert the term with coefficient c and exponent void expand(unsigned int); // Expand the terms array by SIZEINCR }; #endif

simplepolydr.cpp file

#include "simplepoly.h" #include

using namespace std;

const unsigned int MAX = 50; void main () { // Input is accepted in a case-insensitive manner! SimplePoly p[MAX]; char command; unsigned int i, j, k; float value; void printmenu ();

printmenu(); do { cin >> command; switch (command) { case '?': case 'H': case 'h': printmenu(); break; case 'R': case 'r': cin >> i; p[i].read(); break; case 'P': case 'p': cin >> i; p[i].write(); break; case '+': cin >> i >> j >> k; p[k] = p[i].plus(p[j]); break; case '-': cin >> i >> j >> k; p[k] = p[i].minus(p[j]); break; case 'E': case 'e': cin >> i >> value; cout > i >> j >> k; p[k] = p[i].multiply(p[j]); break; */ case 'Q': case 'q': break; default: cout

void printmenu () { // Post: Print a menu of choices. cout

simplepoly.cpp file

#include #include #include "simplepoly.h"

using namespace std;

const unsigned int SimplePoly::SIZEINCR = 5; // SIZEINCR is the size increment used in expanding the dynamic arrays as needed.

SimplePoly::SimplePoly() { // Post: A basic "zero" polynomial object is created // with zero terms. Variable is assumed to be x.

variable = 'x'; LargestExp = -1; terms = new term[CAPACITY = SIZEINCR]; };

void SimplePoly::copy (const SimplePoly & p) { // Pre: p is a valid polynomial. // Post: p is DEEP-COPIED into the implicit parameter. // *DO: FILL IN WITH AN APPROPRIATE IMPLEMENTATION };

SimplePoly::SimplePoly (const SimplePoly & p) //DEEP COPY SEMANTICS { copy (p); };

void SimplePoly::free (void) { // *DO: FILL IN WITH AN APPROPRIATE IMPLEMENTATION };

SimplePoly::~SimplePoly (void) { free (); };

SimplePoly & SimplePoly::operator= (const SimplePoly & p) //DEEP COPY SEMANTICS { // Pre: p is a valid polynomial.

if (this != &p) { free (); copy (p); }; return (*this); };

void SimplePoly::expand (unsigned int LargeExp) {

termptr p;

CAPACITY = (LargeExp / SIZEINCR + 1) * SIZEINCR; p = new term[CAPACITY]; for (int j=0; j

void SimplePoly::InsertTerm (int coef, int exp) { // Pre: Implicit parameter is a valid polynomial NOT // containing any term with the same exponent as exp. // Post: A new term corresponding to the given coefficient coef // and exponent exp is inserted into the implicit parameter polynomial.

if (coef) { assert((exp >= CAPACITY) || (terms[exp].coeff == 0));

if (exp >= CAPACITY) expand(exp);

terms[exp].coeff = coef; if ((coef != 0) && (exp > LargestExp)) LargestExp = exp; } };

void SimplePoly::read () { // Post: A new value is read into the implicit parameter polynomial, per // instructions as given out first. If needed, the old value is destroyed.

SimplePoly temp; int coeff; int exp; cout > temp.variable; do { cin >> coeff; if (coeff) { cin >> exp; if (exp >= 0) temp.InsertTerm (coeff, exp); } else while (cin && (cin.peek() != ' ')) cin. ignore(); } while (coeff && (exp >= 0)); *this = temp; // The assignment operator is being called here! };

void SimplePoly::write() const { // Pre: The implicit parameter is a valid polynomial (possibly zero). // Post: The polynomial represented by the implicit parameter is // printed out on the standard output. The variable is used as stored. // *DO: FILL IN WITH AN APPROPRIATE IMPLEMENTATION };

SimplePoly SimplePoly::plus (const SimplePoly & right) const { // Pre: The implicit parameter and the parameter right are valid // polynomials.

// *DO: FILL IN WITH AN APPROPRIATE IMPLEMENTATION };

SimplePoly SimplePoly::minus (const SimplePoly & right) const { // Pre: The implicit parameter and the parameter right are valid // polynomials. // Post: The difference of the two parameters is returned by minus. // The polynomial right is subtracted from the implicit parameter. // *DO: FILL IN WITH AN APPROPRIATE IMPLEMENTATION };

float SimplePoly::evaluate (float value) const { // Pre: The implicit parameter is a valid polynomial. // Post: The value of the polynomial with the value substituted for the variable is returned.

// *DO: FILL IN WITH AN APPROPRIATE IMPLEMENTATION };

In this project, you will complete a C++ class (partly written for you) that performs polynomial arithmetic. The file simplepoly.h is the interface specification file, that is, the header file for your polynomial class. You may not change anything in the public portion of the specification given to you, and shouldn't need to change the private section, either. The file simplepoly.cpp, although partially completed, is a somewhat incomplete implementation of the class in question, which you are to complete. Study the file carefully, and you will note some comments starting with *DO intended to draw your specific attention. The file simplepolydr.cpp is a "driver" (which lets you test the completed class implementation). You may add more functions in the implementation file if you wish, so they can be used by the other functions in the implementation. Keep in mind, however, that since they are not part of the public portion of the specification (and since you may not change the public part), they will not be accessible to any application that uses your package. A file showing a sample run, P2out.pdf is also supplied, and contains some highlighting, study it carefully to understand what the program is expected to do. The lines appearing in red are the user input, and lines highlighted in a yellow background are computed output from the program. The rest of the lines are also output by the program but are literal text (as opposed to computed), and mostly (although not always) serve as prompts to the user. Any help would be appreciated! C++ only as well. Pn: En V: ro Available commands: ?: to print this menu H: to print this menu Rn: to read a value for polynomial n to print the value of polynomial n + i j k: to add polynomials i and j and store result in k - ij k: to subtract polynomials j from i and store result in k to evaluate polynomial n with its variable set to value v Q: to quit Input a polynomial by specifying the variable and all terms in any order. Each term is specified by an integer coefficient and a non-negative integer exponent. Indicate END by specifying a dummy term with a zero coefficient and/or a negative exponent. x 3 2 -4 3 5 0 0 -4x3 +3x2 +5 r1 Input a polynomial by specifying the variable and all terms in any order. Each term is specified by an integer coefficient and a non-negative integer exponent. Indicate END by specifying a dummy term with a zero coefficient and/or a negative exponent. x 2 4 -3 2 -4 1 2 0 -5 -2 P1 2x4 -3x2 - 4x +2 + 0 1 2 p2 2x4 - 4x3 - 4x +7 - 01 3 P3 -2x4 - 4x3 +6x2 + 4x +3 - 104 p4 2x4 +4x3 -6x2 - 4x - 3 + 3 4 5 p5 0 +3 3 6 p6 -4x4 -8x3 +12x2 +8x +6 R7 Input a polynomial by specifying the variable and all terms in any order. Each term is specified by an integer coefficient and a non-negative integer exponent. Indicate END by specifying a dummy term with a zero coefficient and/or a negative exponent. Y 4 3 2 1 0 p7 4y3 +2y + 1 7 8 Sorry, this program can only add polynomials of the same variable. E 6 2.0

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!