Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * This program will...
- * 1) Recieve information from the user on the degree of a polynomail, followed by the coefficients of each term
- * 2) Interpret the information and display the polynomial in standard form
- * 3) Evaluate the polynomial at a user-given value
- * 4) Evaluate the first and then second derivative of the polynomial at user-given value
- * 5) Receive a second polynomial from the user and then compare the second polynomial to the first derivative of the first polynomial
- * Course: csc1253 Section 001
- * Programming Project #: 5
- * Instructor: Dr. Duncan
- * @author BLAKE LALONDE
- * @since 11 / 21 / 2019
- * @version 2
- */
- #include <iostream>
- #include <iomanip>
- #include <string>
- #include <cmath>
- #include <sstream>
- using namespace std;
- class Polynomial
- {
- private:
- double* poly;
- public:
- // Creates the zero polynomial //
- Polynomial()
- {
- poly = new double[2];
- poly[0] = 0;
- poly[1] = 0;
- }
- // Creates a polynomial using the contents of the degCoeffs array //
- Polynomial(double degCoeffs[])
- {
- int i;
- double size = degCoeffs[0] + 2;
- poly = new double[size];
- for (i = 0; i <= size; i++)
- {
- poly[i] = degCoeffs[i];
- }
- }
- // Used to deallocate the memory associated with this polynomial //
- ~Polynomial()
- {
- }
- // Evaluates the polynomial at the specified value //
- double eval(double x) const
- {
- int i;
- int power = poly[0];
- double sum = poly[1];
- int size = power + 2;
- for (i = 2; i < size; i++)
- {
- sum = sum * x + poly[i];
- }
- if (sum == -0)
- return 0;
- return sum;
- }
- // Gives a string representation of this polynomial in standard form where zero terms, coefficeints 1 or -1, and exponents 1 are not displayed //
- string str() const
- {
- stringstream sout;
- int power = poly[0];
- int i;
- int powerChanger = power - 1;
- int size = power + 2;
- if (size < 2)
- {
- sout << "0";
- string exception = sout.str();
- return exception;
- }
- else if (poly[1] == 1)
- sout << "x^" << power;
- else if (poly[1] == -1)
- sout << "-x^" << power;
- else if (poly[1] == 0)
- sout << "";
- else
- {
- if (power == 1)
- {
- if (poly[1] == 1)
- sout << "x";
- else if (poly[1] == -1)
- sout << " - x";
- else
- sout << poly[1] << "x";
- }
- else if (power > 1)
- sout << poly[1] << "x^" << power;
- }
- if (poly[0] == 0)
- sout << poly[1];
- for (i = 2; i <= power + 2 && powerChanger >= 0; i++, powerChanger--)
- {
- if (powerChanger == 0)
- {
- if (poly[power + 1] > 0)
- sout << " + " << poly[power + 1];
- else if (poly[power + 1] < 0)
- sout << " - " << abs(poly[power + 1]);
- }
- else if (powerChanger == 1)
- {
- if (poly[i] < -1)
- sout << " - " << abs(poly[i]) << "x";
- else if (poly[i] == -1)
- sout << " - x";
- else if (poly[i] == 0)
- sout << "";
- else if (poly[i] == 1)
- sout << " + x";
- else
- sout << " + " << poly[i] << "x";
- }
- else
- {
- if (poly[i] == 1)
- sout << " + x^" << powerChanger;
- else if (poly[i] == -1)
- sout << " - x^" << powerChanger;
- else if (poly[i] == 0)
- sout << "";
- else
- {
- if (poly[i] > 0)
- sout << " + " << poly[i] << "x^" << powerChanger;
- else if (poly[i] < 0)
- sout << " - " << abs(poly[i]) << "x^" << powerChanger;
- }
- }
- }
- string polynomial = sout.str();
- return polynomial;
- }
- // Determines whether this polynomial is equivalent to the specified polynomial //
- bool equals(const Polynomial& other) const
- {
- int i;
- double size1 = poly[0] + 2;
- double size2 = other.poly[0] + 2;
- if (size1 != size2)
- return false;
- for (i = 0; i < size1; i++)
- {
- if (poly[i] != other.poly[i])
- return false;
- }
- return true;
- }
- // Generates a polynomial that represents the derivative of the given polynomial //
- Polynomial differentiate() const
- {
- double size = poly[0] + 2;
- double degree = poly[0];
- poly[0] = degree - 1;
- if (size <= 1)
- {
- double polyException[] = { 0, 0 };
- return Polynomial();
- }
- else
- for (int i = 1; i <= size; i++)
- {
- poly[i] = degree * poly[i];
- degree--;
- }
- return Polynomial(poly);
- }
- };
- int main()
- {
- int i;
- double polyDegree;
- double polySize;
- cout << "Enter the degree of the polynomial -> ";
- cin >> polyDegree;
- if (polyDegree < 0)
- {
- cout << "Please enter a positive degree." << endl;
- return 0;
- }
- double* polyList = new double[polyDegree + 2];
- polyList[0] = polyDegree;
- polySize = polyDegree + 2;
- cout << "Enter the coefficients -> ";
- for (i = 1; i < polySize; i++)
- cin >> polyList[i];
- Polynomial original = Polynomial(polyList);
- cout << endl << "f(x) = " << original.str() << endl;
- cout << "Enter a value at which to evaluate this polynomial -> ";
- double x;
- cin >> x;
- cout << "f(" << x << ") = " << original.eval(x) << endl << endl;
- Polynomial firstDeriv = Polynomial(polyList);
- cout << "f'(x) = " << firstDeriv.differentiate().str() << endl;
- cout << "Enter a value at which to evaluate the first derivative -> ";
- cin >> x;
- cout << "f'(" << x << ") = " << firstDeriv.eval(x) << endl << endl;
- Polynomial secondDeriv = Polynomial(polyList);
- cout << "f'(x) = " << secondDeriv.differentiate().differentiate().str() << endl;
- cout << "Enter a value at which to evaluate the second derivative -> ";
- cin >> x;
- cout << "f\"(" << x << ") = " << secondDeriv.differentiate().eval(x) << endl << endl;
- double newPolyDegree;
- double newPolySize;
- cout << "Enter the degree of the second polynomial -> ";
- cin >> newPolyDegree;
- double* newPolyList = new double[newPolyDegree + 2];
- newPolyList[0] = newPolyDegree;
- newPolySize = newPolyDegree + 2;
- cout << "Enter the coefficents -> ";
- for (i = 1; i < newPolySize; i++)
- cin >> newPolyList[i];
- Polynomial newPolynomial = Polynomial(newPolyList);
- cout << endl << "g(x) = " << newPolynomial.str() << endl;
- if (firstDeriv.equals(newPolynomial))
- cout << "g(x) = f'(x)";
- else
- cout << "g(x) <> f'(x)";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement