Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++ I have 6 errors left. Can somone help me? The goal of assignment 3 is to reinforce the dynamic classes in C++. Specifically, the

C++

I have 6 errors left. Can somone help me?

The goal of assignment 3 is to reinforce the dynamic classes in C++. Specifically, the assignment is to implement the polynomial class from chapter 4.6 on page 212. But the additional requirement is to implement the class using a dynamic array to store the coefficients. The class needs to have a destructor, copy constructor, and an overloaded assignment operator. The following files are provided for your reference. You should start by modifying the header file first.

poly0.h: The header file for the preliminary polynomial class.

polytest0.cpp: A small interactive test program.

polyexam0.cpp: A small non-interactive test program

Remember, all those files are only work for static array implementation. You should modify them to make the dynamic array implementation work.

polytest0.cpp

______________________________________________________

// FILE: polytest0.cxx

#include // Provides toupper #include // Provides cout and cin #include // Provides EXIT_SUCCESS #include "polynomials.h" // Provides the polynomial class using namespace std; //using namespace main_savitch_3;

const unsigned int MANY = 3; // Number of polynomials allowed in this test program.

// PROTOTYPES for functions used by this test program: void print_menu(); // Postcondition: The menu has been written to cout.

size_t set_current( ); // Postcondition: Return value is index for a new current polynomial.

char get_command(); // Postcondition: The user has been prompted for a character. // The entered charatcer will be returned, translated to upper case.

void view(const polynomial& test); //Postcondition: The polynomial passed has been sent to cout.

void view_all(const polynomial a[]); //Postcondition: All polynomials has been written to cout.

void test_add(polynomial& test); // Postcondition: The user has been prompted for a coefficent and degree of // the term added. The resulting polynomial has been written to cout.

void test_assign(polynomial& test); // Postcondition: The user has been prompted for the degree and the coeffinient // to be set. The resulting polynomial has been written to cout.

void test_clear(polynomial& test); // Postcondition: test.clear( ) has been activated. // to be set. The resulting polynomial has been written to cout.

void test_gif(const polynomial& test); // Postcondition: The user has been prompted for the range of the gif file. // The gif file of the graph of the polynomial has been written to disk.

void test_eval(const polynomial& test); // Post conditon: The user has been prompted for the x value. The evaluation // of the polynomial is written to cout.

void test_np(const polynomial& test); // Post conditon: The user has been prompted for the e value. The // value of test.next_term(e) and test.previous_term(e) are written to cout.

int main() { polynomial p[MANY]; size_t current_index = 0; char command; size_t i;

cout << "Polynomials "; for (i = 0; i < MANY; ++i) cout << char('A' + i) << ' '; cout << "have all been initialized." << endl;

do { print_menu(); command = toupper(get_command());

switch(command) { case 'S': current_index = set_current( ); break; case '1': test_assign(p[current_index]); break; case '2': test_add(p[current_index]); break; case 'C': test_clear(p[current_index]); break; case 'V': cout << char(current_index + 'A') << ": "; view(p[current_index]); break; case 'A': view_all(p); break; // case 'D': // cout << char(current_index + 'A') << ".derivative: "; // view(p[current_index].derivative( )); // break; case 'E': test_eval(p[current_index]); break; // case 'G': test_gif(p[current_index]); // break; case 'N': test_np(p[current_index]); break; case '+': cout << "A + B: "; view(p[0] + p[1]); break; case '-': cout << "A - B: "; view(p[0] - p[1]); break; // case '*': // cout << "A * B: "; // view(p[0] * p[1]); // break; case 'Q': // Do nothing.. break; default: cout << "Invalid command." << endl; break; } } while(command != 'Q');

return (EXIT_SUCCESS); }

void print_menu() { cout << "----------------- The Commands -----------------" << endl; cout << "S - set the current Polynomial to work on" << endl; cout << " - - - - - - - - - - - -" << endl; cout << "1 - use the assign_coef function" << endl; cout << "2 - use the add_to_coef function" << endl; cout << "C - use the clear function" << endl; cout << "V - view the current polynomial by using <<" << endl; cout << "A - view all polynomials by using <<" << endl; cout << "D - view derivative of current polynomial" << endl; cout << "E - evaluate current polynomial by using () op" << endl; cout << "G - use the gif function" << endl; cout << "N - use the next_term and previous_term functions" << endl; cout << "+ - view A + B" << endl; cout << "- - view A - B" << endl; cout << "* - view A * B" << endl; cout << " - - - - - - - - - - - -" << endl; cout << "Q - quit this interactive test program" << endl; cout << "-------------------------------------------------" << endl; }

char get_command() { char command;

cout << ">"; cin >> command;

return(toupper(command)); }

void view(const polynomial& test) { cout << test << " (degree is " << test.degree( ) << ")" << endl; }

size_t set_current( ) { size_t i; char command; do { cout << "Polynomials "; for (i = 0; i < MANY; ++i) cout << char('A' + i) << ' '; cout << "." << endl; cout << "Enter the polynomial you want to work on: "; command = toupper(get_command()); } while ((command < 'A') || (command >= char('A' + MANY))); return command - 'A'; }

void test_add(polynomial& test) { double coefficient; unsigned int exponent;

cout << "Enter exponent: "; cin >> exponent; cout << "Enter coefficient: "; cin >> coefficient;

test.add_to_coef(coefficient, exponent); cout << "After adding: "; view(test); }

void test_assign(polynomial& test) { double coefficient; unsigned int exponent;

cout << "Enter exponent: "; cin >> exponent; cout << "Enter coefficient: "; cin >> coefficient;

test.assign_coef(coefficient, exponent); cout << "After assigning: "; view(test); }

//void test_gif(const polynomial& test) //{ // char file_name[20]; // double high_x, low_x, high_y, low_y; // // cout << "Enter file name to write: "; // cin >> file_name; // cout << "Enter upper x bound: "; // cin >> high_x; // cout << "Enter lower x bound: "; // cin >> low_x; // cout << "Enter upper y bound: "; // cin >> high_y; // cout << "Enter lower y bound: "; // cin >> low_y; // // make_gif(test, file_name, low_x, high_x, low_y, high_y); // cout << "The file has been written" << endl; //}

void test_eval(const polynomial& test) { double x_value; cout << "Enter the x value: "; cin >> x_value;

cout << "For the poly: "; view(test); cout << "The evaluation returned is " << test(x_value) << endl; }

void view_all(const polynomial p[]) { size_t i; cout << endl;

for (i = 0; i < MANY; ++i) { cout << char(i + 'A') << ": "; view(p[i]); } }

void test_clear(polynomial& test) { test.clear( ); cout << "After clearing: "; view(test); }

void test_np(const polynomial& test) { unsigned int exponent;

cout << "Enter exponent: "; cin >> exponent;

cout << "For polynomial: "; view(test); cout << "next_term(" << exponent << ") = " << test.next_term(exponent) << endl; cout << "previous_term(" << exponent << ") = " << test.previous_term(exponent) << endl; }

polynomials.cpp

__________________________________________________________

#include // Provides copy function #include // Provides assert function #include "polynomials.h" #include #include using namespace std; //using namespace main_savitch_3;

const polynomial::size_type polynomial::DEFAULT_CAPACITY;

polynomial::polynomial() { // CRF: Memory is allocated here with new. data = new value_type[DEFAULT_CAPACITY]; capacity = DEFAULT_CAPACITY; // CRF: Set all coefficients equal to 0. for (int i =0; i

polynomial::~polynomial( ) { delete [ ] data; } void polynomial::insert(const value_type& entry) { if (used == capacity) reserve(used+1); data[used] = entry; ++used; } double polynomial::assign_coef(double new_coefficient) { return new_coefficient; } void polynomial::reserve(size_type new_capacity) // Library facilities used: algorithm { value_type *larger_array;

if (new_capacity == capacity) return; // The allocated memory is already the right size.

if (new_capacity < used) new_capacity = used; // Cant allocate less than we are using.

larger_array = new value_type[new_capacity]; copy(data, data + used, larger_array); delete [ ] data; data = larger_array; capacity = new_capacity; } unsigned int degree(size_t used) { return used; } ostream& operator<<(ostream& outs, polynomial& source) { outs << " The equation = "; //for (int i= 0; i < source.capacity; i++) //{ // outs << "Coefficient " << i << ": " << source.data[i] << endl; //} for (int i= source.used-1; i >= 0; i--) { outs << source.data[i]; if (i !=0) { outs <<"x^"<< i << " + "; } } return outs; // CRF: outs << source.data[0]; // CRF: return outs; //{ // return outs << " the polynomial is " << source.data[0] << "X^0 + " << // source.data[i] << "^" << source.used << endl; //} }

_______polynomials.h____________________________________________________________

#ifndef POLYNOMIALS_H #define POLYNOMIALS_H #include #include // Provides size_t using namespace std; //using namespace main_savitch_3;

class polynomial { public: // TYPEDEFS and MEMBER CONSTANTS typedef int value_type; typedef std::size_t size_type; static const size_type DEFAULT_CAPACITY = 3; // CONSTRUCTORS and DESTRUCTOR polynomial(); //polynomial(double a0); polynomial(const polynomial& source); ~polynomial( ); // MODIFICATION MEMBER FUNCTIONS void reserve(size_type new_capacity); double assign_coef(double new_coefficient); void insert(const value_type& entry); double coefficient(unsigned int k) const; unsigned int degree( ) const; unsigned int next_term(unsigned int k) const; //new void add_to_coef(); void clear(); void operator =(const polynomial& source); // CONSTANT MEMBER FUNCTIONS void previous_term(); size_type size( ) const { return used; } size_type count(const value_type& target) const; // FRIEND FUNCTION friend ostream& operator<<(ostream& outs, polynomial& source); private: value_type *data; // Pointer to partially filled dynamic array size_type used; // How much of array is being used size_type capacity; // Current capacity of the bag };

// NONMEMBER FUNCTIONS for the bag class polynomial operator +(const polynomial& b1, const polynomial& b2); ostream& operator <<(ostream & outs, const polynomial& source);

#endif

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

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

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

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

Get Started

Students also viewed these Databases questions