Please help this. It has only few tasks to fill out. It looks long but it is not
ID: 660121 • Letter: P
Question
Please help this. It has only few tasks to fill out. It looks long but it is not long. Thank you.
I HAVE REUPLOADED MY QUESTION 10 OR SO TIMES. SO IF YOU DO HELP ME IT IS CERTAIN YOULL GET ALL THOSE POINTS
#ifndef POLY_H
#define POLY_H
#include <iostream> // Provides ostream
#include <vector> // Coefficients are stored in a vector
namespace csc161
{
// Polynomial Exception
class Polynomial_E
{
private:
string errorMessage;
public:
Polynomial_E(string p_error_message);
void handleError();
};
class Polynomial_T
{
public:
// CONSTANTS
static const unsigned int MAX_DEGREE = 30;
// CONSTRUCTORS and DESTRUCTOR
Polynomial_T();
Polynomial_T(const Polynomial_T& source); // Copy constructor
~Polynomial_T( );
// MODIFICATION MEMBER FUNCTIONS
void fill();
void clear( );
void operator =(const Polynomial_T& source);
void setCoefficient(double p_coef, unsigned int p_exp);
// CONSTANT MEMBER FUNCTIONS
double getCoefficient(unsigned int p_exponent) const;
unsigned int getDegree( ) const { return currentDegree; }
Polynomial_T firstDerivative( ) const;
Polynomial_T secondDerivative() const;
double eval(double p_x) const;
double evalHornerMethod(double p_x) const;
unsigned int hash(string p_word, double p_x, unsigned int p_dictionary_size);
void addCoefs(double p_coef, unsigned int p_exp);
unsigned int nextTerm(unsigned int p_e) const; // Helpers, might not be needed, if trivial
unsigned int previousTerm(unsigned int p_e) const; // Helper, might not be needed, if trivial
// CONSTANT OPERATORS
double operator( ) (double x) const { return eval(x); }
// PRIVATE MEMBERS
private:
vector<double> coef; // Vector to store coefs (if you decide on vector)
unsigned int currentDegree; // Current degree of the polynomial
};
// NON-MEMBER BINARY OPERATORS
Polynomial_T operator +(const Polynomial_T& p1, const Polynomial_T& p2);
Polynomial_T operator -(const Polynomial_T& p1, const Polynomial_T& p2);
Polynomial_T operator *(const Polynomial_T& p1, const Polynomial_T& p2) throw (Polynomial_E);
// NON-MEMBER OUTPUT FUNCTIONS
std::ostream& operator << (std::ostream& out, const Polynomial_T& p);
std::istream& operator >> (std::istream& ins, Polynomial_T& p);
}
#endif
// FILE: polynomial_csc161.cpp
// CLASS IMPLEMENTED: polynomial (see polynomial_vector.h for documentation)
// INVARIANT for the polynomial class:
// 1. coef is a vector of doubles
// 2. For each k < size, the coefficient of the x^k term is stored in coef[k].
// 3. The degree of the polynomial is stored in current_degree
// (using zero for the case of all zero coefficients).
using namespace std;
#include <algorithm> // Provides fill_n and copy functions
#include <climits> // Provides std::UINT_MAX
#include <cmath> // Provides pow
#include <iostream> // Provides ostream
#include <string> // Provides String operations
#include "polynomial_csc161.h"
namespace csc161
{
Polynomial_T::Polynomial_T() {
// Reserve memory and initialize to 0
coef.insert(coef.begin(), MAX_DEGREE, 0.0);
currentDegree = 0;
};
Polynomial_T::Polynomial_T(const Polynomial_T& source)
{
//////////////// EXAM TASK [10 points]
// Implement a copy - constructor mimicking the behavior of the standard default copy - constructor
}
Polynomial_T::~Polynomial_T()
{
}
void Polynomial_T::operator =(const Polynomial_T& source)
{
if (this == &source)
return;
this->clear();
coef = source.coef;
currentDegree = source.currentDegree;
}
void Polynomial_T::addCoefs(double p_coef, unsigned int p_exp)
{
setCoefficient(getCoefficient(p_exp) + p_coef, p_exp);
}
void Polynomial_T::setCoefficient(double p_coef, unsigned int p_exp)
{
//////////////// TASK 1
// Implement the "setCoefficient" method, which will provide the following independent pieces of functionality :
// 1. Throw Polynomial_E exception, if the exponent p_exp is greater than MAX_DEGREE
// 2. Set the coefficient for a given exponent
// 3. Set the "currentDegree" value. Please note that resetting a coefficient on a given term may affect the "currentDegree"
}
void Polynomial_T::clear()
{
fill_n(coef.begin(), coef.size(), 0.0);
currentDegree = 0;
}
double Polynomial_T::getCoefficient(unsigned int exponent) const
{
if (exponent <= currentDegree)
return coef[exponent];
return 0.0;
}
Polynomial_T Polynomial_T::firstDerivative() const
{
unsigned int i;
Polynomial_T answer;
for (i = 1; i != 0; i = nextTerm(i))
answer.setCoefficient(i * getCoefficient(i), i-1);
return answer;
}
Polynomial_T Polynomial_T::secondDerivative() const
{
//////////////// TASK 2
// Implement the "secondDerivative" method.It should / could leverage the invocation of the "firstDerivative" method,
// which has been provided above
return Polynomial_T(); // only so the project compiles, replace with some sensible value
}
double Polynomial_T::eval(double x) const
{
//////////////// TASK 3
// Implement the "eval" method.You can either iterate through all of its terms,
// or skip "zero" terms by utilizing the "nextTerm" function. Make sure to return a "double"
return 0; // only so the project compiles, replace with some sensible value
/////////////// TASK 4
// What is the complexity measure of this "eval" algorithm in the Big - O notation ?
}
double Polynomial_T::evalHornerMethod(double p_x) const {
unsigned int i = 0; // An exponent
double power_of_x; // x raised to the i power
double answer = getCoefficient(currentDegree); // Sum of the polynomial's terms
for (i = currentDegree; i != 0; i--)
{
answer = answer * p_x + getCoefficient(i-1);
}
return answer;
}
unsigned int Polynomial_T::hash(string p_word, double p_x, unsigned int p_dictionary_size) {
Polynomial_T poly;
/////////////// TASK 5
// The polynomial - based hash function can be used to implement a dictionary application.It is an effective way
// to map words to dictionary indexes.The characters of a word become coefficients of a polynomial, and its degree is
// equal to the length of the word string.Such a polynomial is then evaluated for an random X, and mapped to an index
// in the dictionary table holding the words.Good results with a minimal number of conflicts are seen for X = 31.
// The "hash" method provides the logic for the above.Fill in the code to set the coefficients to the characters in the string.
// Order is not important.
/////////////////// END
int a = poly.evalHornerMethod(p_x);
return (a % p_dictionary_size);
}
unsigned int Polynomial_T::nextTerm(unsigned int e) const
{
// Move e up until there is a non-zero term above it:
while ((e < getDegree( )) && (getCoefficient(e+1) == 0.0))
++e;
// If e hit degree, then no term was found and we return 0:
return (e >= getDegree( )) ? 0 : e+1;
}
unsigned int Polynomial_T::previousTerm(unsigned int e) const
{
// Move e down until there is a non-zero term below it:
while ((e > 0) && (getCoefficient(e-1) == 0.0))
--e;
// If e hit zero, then no term was found and we return UINT_MAX:
return (e == 0) ? UINT_MAX : e-1;
}
Polynomial_T operator +(const Polynomial_T& p1, const Polynomial_T& p2)
{
Polynomial_T answer = p1;
unsigned int i = 0;
do
{
answer.addCoefs(p2.getCoefficient(i), i);
i = p2.nextTerm(i);
} while (i != 0);
return answer;
}
Polynomial_T operator -(const Polynomial_T& p1, const Polynomial_T& p2)
{
Polynomial_T answer = p1;
unsigned int i = 0;
do
{
answer.addCoefs(-p2.getCoefficient(i), i);
i = p2.nextTerm(i);
} while (i != 0);
return answer;
}
Polynomial_T operator *(const Polynomial_T& p1, const Polynomial_T& p2)
{
Polynomial_T answer; // Will be set to p1 * p2
unsigned int i, j; // Exponent for a term of p1 or p2
/////////////////////TASK 6
// The operation of multiplying(*) two polynomials has high risk of producing a polynomials of a degree exceeding MAX_DEGREE.
// Enhance the implementation of this overloaded operator to throw Polynomial_E exception in that case ritgh away
///////////////////// END
i = 0;
do
{
j = 0;
do
{
answer.addCoefs(p1.getCoefficient(i) * p2.getCoefficient(j), i+j);
j = p2.nextTerm(j);
} while (j != 0);
i = p1.nextTerm(i);
} while (i != 0);
return answer;
}
ostream& operator << (ostream& out, const Polynomial_T& p)
{
unsigned int i = p.getDegree( );
double number;
// Exit, if empty polynomial
if (p.getDegree() == 0)
return out;
// Blank line for readibility
out << endl;
// Each iteration of this loop prints one term of the polynomial:
do
{
// Get the coefficient:
number = p.getCoefficient(i);
// Print a sign
if (number < 0)
{
out << ((i == p.getDegree( )) ? "-" : " - ");
number = -number;
}
else if (i < p.getDegree( ))
out << " + ";
// Print the coefficient, variable x, and exponent
if ((number != 1.0) || (i == 0))
out << number;
if (i > 0)
out << 'x';
if (i > 1)
out << '^' << i;
// Move to the next lowest term:
i = p.previousTerm(i);
} while (i != UINT_MAX);
out << endl;
return out; //return the output stream
}
std::istream& operator >> (std::istream& ins, Polynomial_T& p) throw (Polynomial_E)
{
double p_coef = -1;
unsigned int p_exp = 0;
cout << endl << "Enter coefficient and exponent values, separated by wthispace. ";
cout << endl << "Hit Enter after every pair. ";
cout << endl << "A pair of two zeros concludes the input." << endl;
cin >> p_coef >> p_exp;
while (p_coef != 0 && p_exp >= 0 && p_exp <= p.MAX_DEGREE) {
p.setCoefficient(p_coef, p_exp);
cin >> p_coef >> p_exp;
}
ins.clear();
return ins;
}
Polynomial_E::Polynomial_E(string p_error_message) {
errorMessage = p_error_message;
};
void Polynomial_E::handleError() {
////////////////////// TASK 7
// Enhance the implementation of the Polynomial_E exception handler to print "Exception:" in front of the message text
cout << errorMessage << endl;
}
}
// *********************************************************************
// Polynomial CSC 161
// *********************************************************************
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
#include "polynomial_csc161.h"
using namespace csc161;
///////////////////// FUNCTIONS PROTOTYPES
///////////////////// FUNCTIONS IMLEMENTATION
// ======================
// main
// ======================
int main()
{
enum menu_options {
SETUP = 0, EVALUATE = 1, EVALUATE_HORNER = 2, HASH_FUNCTION = 3,
FIRST_DERIVATIVE = 4, SECOND_DERIVATIVE = 5, PRINT = 6, BYE = 9 };
// Working variables
int menu_option = -1;
Polynomial_T poly;
// Welcome a User
cout << endl << "****** Welcome to CSC 161 Polynomial Delight! ******" << endl;
// Main Menu
while (menu_option != 9) {
//Display menu options
cout << endl;
cout << "Main Menu:" << endl;
cout << " 0) Set up a Polynomial" << endl;
cout << " 1) Evaluate for X" << endl;
cout << " 2) Evaluate for X with Horner's Method" << endl;
cout << " 3) Hash a Word" << endl;
cout << " 4) Calcualte First Derivative" << endl;
cout << " 5) Calculate Second Derivative" << endl;
cout << " 6) Print" << endl;
cout << " 9) Exit" << endl;
//prompt user to select operation
cout << "Select an option: ";
cin >> menu_option;
switch (menu_option) {
case SETUP:
{
// Takes user input to set the coefficients of a polynomial.
// A user provides the pairs of values: coefficient and exponent, separated by whitespace and hits Enter after each pair
//////////////////// Task 8
// Setting up a polynomial uses the >> operator. The implementation of that operator invokes the "setCoefficient" method,
// which can throw an exception, if a user enters an exponent exceeding MAX_DEGREE.
// Put this section of code in the try / catch block in order to account for the Polynomial_E exception
// that might bubble up from the dungeons of the >> operator
cin >> poly; // User enteres pairs of cofficient and exponent
////////////////// END TASK
// Print what user entered
cout << poly;
}
break;
///////////////////// TASK 9
// Both menu options to evaluate a polynomial are hardcoded to evaluate for X = 2.
// Enhance the code, so the user is asked to enter X
case EVALUATE:
// Ask user to enter X. Can be a function defined/implemented in the appropriate sections above
cout << endl << "Evaluate for x=2 :" << poly.eval(2) << endl;
break;
case EVALUATE_HORNER:
// Ask user to enter X. Can be a function defined/implemented in the appropriate sections above
cout << endl << "Evaluate for x=2 :" << poly.evalHornerMethod(2) << endl;
break;
case HASH_FUNCTION: {
string p_word;
cout << endl << "Enter a word:";
cin >> p_word;
cout << endl << "Hash of " << p_word << ": " << poly.hash(p_word, 31, 500) << endl;
}
break;
case FIRST_DERIVATIVE:
cout << endl << "First Derivative: " << poly.firstDerivative() << endl;
break;
case SECOND_DERIVATIVE:
cout << endl << "Second Derivative: " << poly.secondDerivative() << endl;
break;
case PRINT:
cout << endl << "Printing:" << endl << poly << endl;
break;
default:
break;
}
}
system("pause");
return 0;
}
Explanation / Answer
Getting Same question again and again
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.