Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Need help with the write, free, and copy functions Main as follows #include #include #include simplepoly.h using namespace std; const unsigned int SimplePoly::SIZEINCR = 5;

Need help with the write, free, and copy functions

Main as follows

#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() { // Pre: None. // 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 { // Pre: p is a valid polynomial. // Post: p is copied into the implicit parameter // using "deep copy semantics."

copy (p); };

void SimplePoly::free (void) { // Pre: The implicit parameter has been allocated. // Post: Any necessary deallocation is done.

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

SimplePoly::~SimplePoly (void) { // Pre: The implicit parameter has been allocated. // Post: Any necessary deallocation is done.

free (); };

SimplePoly & SimplePoly::operator= (const SimplePoly & p) //DEEP COPY SEMANTICS { // Pre: p is a valid polynomial. // Post: The value of p is assigned to the implicit parameter // by "deep copy semantics." Any necessary deallocation is // done along the way.

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

void SimplePoly::expand (unsigned int LargeExp) { // Pre: Implicit parameter is a valid polynomial. LargeExp is // a new exponent that needs to be accommpdated through expansion. // post: The size of the polynomial store in the implicit // parameter is expanded to the lowest multiple of SIZEINCR that // can accommodate a term with the LargeExp, while keeping the // polynomial value the same.

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. // In the process, the polynomial is "expanded" if necessary.

if (coef) { assert((exp >= CAPACITY) || (terms[exp].coeff == 0)); // There is no term with the same exponent and a non-zero coefficient // already present.

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

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

void SimplePoly::read () { // Pre: None. // Post: A new value is read into the implicit paramter 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 // ploynomials. // Post: The sum of the two parameters is returned by plus.

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

SimplePoly SimplePoly::minus (const SimplePoly & right) const { // Pre: The implicit parameter and the parameter right are valid // ploynomials. // 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 };

Header file

#ifndef SIMPLEPOLY_H #define SIMPLEPOLY_H

class SimplePoly { public: SimplePoly(); // the default constructor SimplePoly(const SimplePoly&); // the copy constructor (creates a deep copy) ~SimplePoly(); // the destructor SimplePoly& operator= (const SimplePoly&); // the assignment operator (creates a deep copy)

void read(); // Read a value for the polynomial per preset convention 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&); // used by the copy constructor/assignment operator void free(); // used by the destructor / assignment operator void InsertTerm(int c, int e); // Insert the term with coefficient c and exponent e, expanding the dynamic // "terms" array, if necessary. void expand(unsigned int); // Expand the terms array by SIZEINCR }; #endif

Sample output:

image text in transcribedimage text in transcribed

Available commands: ?: to print this menu H: to print this menu Rn: to read a value for polynomial n Pn: to print the value of polynomial n + ij k: to add polynomials i and j and store result in k ijk: to subtract polynomials j from i and store result in k En v: to evaluate polynomial n with its variable set to value v Q: to quit ro 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 24 - 3 2 -4 1 2 0 -5 -2 P1 2x4 - 3x2 - 4x +2 + 0 1 2 p 2 2x4 - 4x3 - 4x +7 - 0 1 3 P3 -2x4 -4x3 +6x2 +4x +3 - 1 0 4 p4 2x4 +4x3 - 6x2 - 4x -3 + 3 4 5 p5 0 +3 36 p6 - 4x4 -8x3 +12x2 +8x +6 R 7 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 -58 En V: e 7 -1.5 -16.5 ? Available commands: ?: to print this menu H: to print this menu Rn: to read a value for polynomial n Pn: 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 h Available commands: ?: to print this menu H: to print this menu Rn: to read a value for polynomial n Pn: 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 Env: to evaluate polynomial n with its variable set to value v Q: to quit q Press any key to continue Available commands: ?: to print this menu H: to print this menu Rn: to read a value for polynomial n Pn: to print the value of polynomial n + ij k: to add polynomials i and j and store result in k ijk: to subtract polynomials j from i and store result in k En v: to evaluate polynomial n with its variable set to value v Q: to quit ro 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 24 - 3 2 -4 1 2 0 -5 -2 P1 2x4 - 3x2 - 4x +2 + 0 1 2 p 2 2x4 - 4x3 - 4x +7 - 0 1 3 P3 -2x4 -4x3 +6x2 +4x +3 - 1 0 4 p4 2x4 +4x3 - 6x2 - 4x -3 + 3 4 5 p5 0 +3 36 p6 - 4x4 -8x3 +12x2 +8x +6 R 7 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 -58 En V: e 7 -1.5 -16.5 ? Available commands: ?: to print this menu H: to print this menu Rn: to read a value for polynomial n Pn: 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 h Available commands: ?: to print this menu H: to print this menu Rn: to read a value for polynomial n Pn: 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 Env: to evaluate polynomial n with its variable set to value v Q: to quit q Press any key to continue

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Intelligent Information And Database Systems Third International Conference Achids 2011 Daegu Korea April 2011 Proceedings Part 2 Lnai 6592

Authors: Ngoc Thanh Nguyen ,Chong-Gun Kim ,Adam Janiak

2011th Edition

3642200419, 978-3642200410

More Books

Students also viewed these Databases questions

Question

Name elements that make for "fine dining."

Answered: 1 week ago

Question

an element of formality in the workplace between different levels;

Answered: 1 week ago