Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- File: Polynomial.cpp
- Description: The implementation file for the Polynomial class.
- */
- #include <iostream>
- #include <string>
- #include <cstdlib>
- #include <cmath>
- #include "Polynomial.h"
- using namespace std;
- Polynomial::Polynomial() : poly(NULL)
- {
- }
- Polynomial::Polynomial(const Polynomial& original)
- {
- if (original.poly != NULL)
- {
- for (PolyPtr i = original.poly; i != NULL; i = i->node)
- {
- make_lists(i->coefficient, i->power);
- }
- }
- else
- {
- poly = NULL;
- }
- }
- Polynomial::Polynomial(int constant)
- {
- PolyPtr temp = new PolyNode;
- temp->power = 0;
- temp->coefficient = constant;
- poly = temp;
- }
- Polynomial::Polynomial(int constant, int exponent)
- {
- PolyPtr temp = new PolyNode;
- temp->power = exponent;
- temp->coefficient = constant;
- poly = temp;
- }
- Polynomial::~Polynomial()
- {
- if (poly != NULL) // if the linked list isn't empty...
- {
- for (PolyPtr i = poly; i->node != NULL; i = i->node)
- {
- remove_nodes(i);
- }
- }
- }
- void Polynomial::remove_nodes(PolyPtr parameter)
- {
- if(poly != NULL)
- {
- PolyPtr before = NULL, discard = NULL;
- for(PolyPtr i = poly; i != NULL; i = i->node)
- {
- if(i->node == parameter)
- {
- before = i;
- }
- else if(i == parameter)
- {
- if(i == poly)
- {
- discard = poly;
- poly = poly->node;
- delete discard;
- }
- else
- {
- discard = i;
- before->node = discard->node;
- delete discard;
- }
- }
- }
- }
- }
- void Polynomial::insert_polynomial()
- {
- string polynomial, current, symbol = "+";
- int value = 0, exp = 0;
- cout << "Type in the polynomial in the format ax^n + bx^(n-1) + ..." << endl;
- cout << "Write the # symbol right at the end of the polynomial. (Ex: 6x^3 + 4x^2 + 32#; 6x^2 + 3x + 32#)" << endl;
- getline(cin,polynomial);
- string temp = polynomial.substr(0); // Make a temporary substring that is equal to the polynomial received in the input.
- for (int i = 0; i < polynomial.length(); i++)
- {
- if (polynomial[i] == ' ') // If there is a blank space...
- {
- current = temp.substr(0, temp.find(" ")); // create a temporary substring up to the point of the space...
- temp = temp.substr(temp.find(" ") + 1); // and cut off that part of the string from the main substring.
- if (current == "+" || current == "-") // If the temporary substring is a symbol instead of a polynomial...
- {
- symbol = current;
- }
- else
- {
- value = calculate_coefficient(current, symbol); // Calls the function that will calculate the coefficient.
- if (string::npos != current.find("^")) // If the ^ character exists in the string (which means a power greater than 1 exists)...
- {
- exp = atoi(current.substr(current.find("^") + 1).c_str()); // Convert the string after the ^ character into an int.
- }
- else
- {
- exp = 1; // Set exponent equal to 1 if there is no ^ character existing in the string.
- }
- make_lists(value, exp); // Give the coefficient and power to a function that will place those values in a node.
- }
- }
- if (string::npos != temp.find("#") && i == polynomial.length() - 1) // If the # character exists in temp and the end of the polynomial is reached...
- {
- temp = temp.substr(0, temp.find("#")); // Create a substring that cuts off the # character.
- if (string::npos != temp.find("x")) // If the "x" character exists (aka not a constant)...
- {
- value = calculate_coefficient(temp, symbol);
- if (string::npos != temp.find("^"))
- {
- exp = atoi(temp.substr(temp.find("^") + 1).c_str());
- }
- else
- {
- exp = 1;
- }
- }
- else // If the value is a constant (aka no "x" character found)...
- {
- value = calculate_coefficient(temp, symbol);
- exp = 0;
- }
- make_lists(value, exp);
- }
- }
- cout << endl;
- }
- int Polynomial::calculate_coefficient(string& poly_temp, string symbol)
- {
- int string_to_int;
- // If the symbol before the polynomial is positive and a variable x exists...
- if (symbol == "+" && (string::npos != poly_temp.find("x")))
- {
- string_to_int = atoi(poly_temp.substr(0, poly_temp.find("x")).c_str()); // Cut off the part after the "x" character and convert that part from string to int.
- if (string_to_int == 0)
- {
- if (string::npos != poly_temp.find("-"))
- {
- string_to_int = -1;
- }
- else
- {
- string_to_int = 1;
- }
- }
- }
- // If the symbol before the polynomial is negative and a variable x exists...
- else if (symbol == "-" && (string::npos != poly_temp.find("x")))
- {
- string_to_int = atoi(poly_temp.substr(0, poly_temp.find("x")).c_str()) * -1; // Same as above if-statement, but this time, the int is multiplied by -1.
- if (string_to_int == 0)
- {
- string_to_int = -1;
- }
- }
- // Same thing but x doesn't exists (aka the value is a constant)...
- else if (symbol == "+" && (string::npos == poly_temp.find("x")))
- {
- string_to_int = atoi(poly_temp.c_str()); // Just convert the constant to an int.
- }
- // Same thing as above if-statement but the sign is different...
- else if (symbol == "-" && (string::npos == poly_temp.find("x")))
- {
- string_to_int = atoi(poly_temp.c_str()) * -1; // Same as above if-statement, but this time multiply by -1.
- }
- return string_to_int;
- }
- void Polynomial::make_lists(int constant, int exponent)
- {
- if (poly == NULL)
- {
- poly = new PolyNode;
- poly->coefficient = constant;
- poly->power = exponent;
- poly->node = NULL;
- }
- else
- {
- PolyPtr temp = new PolyNode;
- temp->coefficient = constant;
- temp->power = exponent;
- temp->node = NULL;
- PolyPtr i = poly;
- while (i->node != NULL && i->power > exponent)
- {
- i = i->node;
- }
- if (i->power == exponent)
- {
- i->coefficient += constant;
- if (i->coefficient == 0)
- {
- remove_nodes(i);
- }
- delete temp;
- }
- else if (i->power > exponent)
- {
- temp->node = i->node;
- i->node = temp;
- }
- else // exponent > i->power
- {
- if (i == poly)
- {
- temp->node = poly;
- poly = temp;
- }
- else
- {
- PolyPtr tmp = poly;
- while (tmp->node != NULL && tmp->node != i)
- {
- tmp = tmp->node;
- }
- temp->node = i;
- tmp->node = temp;
- }
- }
- }
- }
- void Polynomial::return_polynomial()
- {
- if (poly == NULL) // If there is nothing in the list...
- {
- cout << "The polynomial is 0.\n";
- }
- else
- {
- int count = 0, value, exp;
- cout << "The polynomial is ";
- for (PolyPtr i = poly; i != NULL; i = i->node)
- {
- value = i->coefficient;
- exp = i->power;
- if (count == 0)
- {
- if (exp > 1 && value != 0)
- {
- if (value == 1)
- {
- cout << "x^" << exp;
- }
- else if (value == -1)
- {
- cout << "-x^" << exp;
- }
- else
- {
- cout << value << "x^" << exp;
- }
- }
- else if (exp == 1 && value != 0)
- {
- if (value == 1)
- {
- cout << "x";
- }
- else if (value == -1)
- {
- cout << "-x";
- }
- else
- {
- cout << value << "x";
- }
- }
- else if (exp == 0 && value != 0)
- {
- cout << value;
- }
- else
- {
- cout << "0";
- }
- }
- else
- {
- if (value > 0 && i != NULL)
- {
- cout << " + ";
- }
- else if (value < 0 && i != NULL)
- {
- cout << " - ";
- value *= -1;
- }
- if (exp > 1 && value != 0)
- {
- if (value == 1)
- {
- cout << "x^" << exp;
- }
- else
- {
- cout << value << "x^" << exp;
- }
- }
- else if (exp == 1 && value != 0)
- {
- if (value == 1)
- {
- cout << "x";
- }
- else
- {
- cout << value << "x";
- }
- }
- else if (exp == 0 && value != 0)
- {
- cout << value;
- }
- else
- {
- cout << "0";
- }
- }
- count++;
- }
- cout << endl;
- }
- }
- int Polynomial::evaluate(int value)
- {
- int temp = 0;
- for (PolyPtr i = poly; i != NULL; i = i->node)
- {
- temp += i->coefficient * pow(value, i->power);
- }
- return temp;
- }
- Polynomial operator +(const Polynomial& left, const Polynomial& right)
- {
- Polynomial sum;
- for (PolyPtr i = left.poly; i != NULL; i = i->node)
- {
- sum.make_lists(i->coefficient, i->power);
- }
- for (PolyPtr i = right.poly; i != NULL; i = i->node)
- {
- sum.make_lists(i->coefficient, i->power);
- }
- return sum;
- }
- Polynomial Polynomial::operator =(const Polynomial& right)
- {
- Polynomial left;
- if(right.poly != NULL)
- {
- for(PolyPtr i = right.poly; i != NULL; i = i->node)
- {
- left.make_lists(i->coefficient, i->power);
- make_lists(i->coefficient, i->power);
- }
- }
- else
- {
- left.poly = NULL;
- poly = NULL;
- }
- return left;
- }
- Polynomial operator -(const Polynomial& left, const Polynomial& right)
- {
- Polynomial difference;
- for(PolyPtr i = left.poly; i != NULL; i = i->node)
- {
- difference.make_lists(i->coefficient, i->power);
- }
- for(PolyPtr i = right.poly; i != NULL; i = i->node)
- {
- difference.make_lists(-(i->coefficient), i->power);
- }
- return difference;
- }
- Polynomial operator *(const Polynomial& left, const Polynomial& right)
- {
- Polynomial product;
- for(PolyPtr i = left.poly; i != NULL; i = i->node)
- {
- for(PolyPtr k = right.poly; k != NULL; k = k->node)
- {
- product.make_lists(i->coefficient * k->coefficient, i->power + k->power);
- }
- }
- return product;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement