Advertisement
Guest User

Polynomial.cpp

a guest
Mar 27th, 2015
634
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 12.05 KB | None | 0 0
  1. /*
  2.     File: Polynomial.cpp
  3.     Description: The implementation file for the Polynomial class.
  4. */
  5.  
  6. #include <iostream>
  7. #include <string>
  8. #include <cstdlib>
  9. #include <cmath>
  10. #include "Polynomial.h"
  11. using namespace std;
  12.  
  13. Polynomial::Polynomial() : poly(NULL)
  14. {
  15. }
  16.  
  17. Polynomial::Polynomial(const Polynomial& original)
  18. {
  19.     if (original.poly != NULL)
  20.     {
  21.         for (PolyPtr i = original.poly; i != NULL; i = i->node)
  22.         {
  23.             make_lists(i->coefficient, i->power);  
  24.         }
  25.     }
  26.     else
  27.     {
  28.         poly = NULL;
  29.     }
  30. }
  31.  
  32. Polynomial::Polynomial(int constant)
  33. {
  34.     PolyPtr temp = new PolyNode;
  35.     temp->power = 0;
  36.     temp->coefficient = constant;
  37.     poly = temp;
  38. }
  39.  
  40. Polynomial::Polynomial(int constant, int exponent)
  41. {
  42.     PolyPtr temp = new PolyNode;
  43.     temp->power = exponent;
  44.     temp->coefficient = constant;
  45.     poly = temp;
  46. }
  47.  
  48. Polynomial::~Polynomial()
  49. {
  50.     if (poly != NULL) // if the linked list isn't empty...
  51.     {
  52.         for (PolyPtr i = poly; i->node != NULL; i = i->node)
  53.         {
  54.             remove_nodes(i);
  55.         }
  56.     }
  57. }
  58.  
  59. void Polynomial::remove_nodes(PolyPtr parameter)
  60. {
  61.     if(poly != NULL)
  62.     {
  63.         PolyPtr before = NULL, discard = NULL;
  64.         for(PolyPtr i = poly; i != NULL; i = i->node)
  65.         {
  66.             if(i->node == parameter)
  67.             {
  68.                 before = i;
  69.             }
  70.             else if(i == parameter)
  71.             {
  72.                 if(i == poly)
  73.                 {
  74.                     discard = poly;
  75.                     poly = poly->node;
  76.                     delete discard;
  77.                 }
  78.                 else
  79.                 {
  80.                     discard = i;
  81.                     before->node = discard->node;
  82.                     delete discard;
  83.                 }
  84.             }
  85.         }
  86.     }
  87. }
  88.  
  89. void Polynomial::insert_polynomial()
  90. {
  91.     string polynomial, current, symbol = "+";
  92.     int value = 0, exp = 0;
  93.     cout << "Type in the polynomial in the format ax^n + bx^(n-1) + ..." << endl;
  94.     cout << "Write the # symbol right at the end of the polynomial. (Ex: 6x^3 + 4x^2 + 32#; 6x^2 + 3x + 32#)" << endl;
  95.     getline(cin,polynomial);    
  96.     string temp = polynomial.substr(0); // Make a temporary substring that is equal to the polynomial received in the input.
  97.  
  98.     for (int i = 0; i < polynomial.length(); i++)
  99.     {
  100.         if (polynomial[i] == ' ') // If there is a blank space...
  101.         {
  102.             current = temp.substr(0, temp.find(" ")); // create a temporary substring up to the point of the space...
  103.             temp = temp.substr(temp.find(" ") + 1); // and cut off that part of the string from the main substring.
  104.             if (current == "+" || current == "-") // If the temporary substring is a symbol instead of a polynomial...
  105.             {
  106.                 symbol = current;
  107.             }
  108.             else
  109.             {
  110.                 value = calculate_coefficient(current, symbol); // Calls the function that will calculate the coefficient.
  111.                 if (string::npos != current.find("^")) // If the ^ character exists in the string (which means a power greater than 1 exists)...
  112.                 {
  113.                     exp = atoi(current.substr(current.find("^") + 1).c_str()); // Convert the string after the ^ character into an int.
  114.                 }
  115.                 else
  116.                 {
  117.                     exp = 1; // Set exponent equal to 1 if there is no ^ character existing in the string.
  118.                 }
  119.                 make_lists(value, exp); // Give the coefficient and power to a function that will place those values in a node.
  120.             }
  121.         }
  122.         if (string::npos != temp.find("#") && i == polynomial.length() - 1) // If the # character exists in temp and the end of the polynomial is reached...
  123.         {
  124.             temp = temp.substr(0, temp.find("#")); // Create a substring that cuts off the # character.
  125.             if (string::npos != temp.find("x")) // If the "x" character exists (aka not a constant)...
  126.             {
  127.                 value = calculate_coefficient(temp, symbol);
  128.                 if (string::npos != temp.find("^"))
  129.                 {
  130.                     exp = atoi(temp.substr(temp.find("^") + 1).c_str());
  131.                 }
  132.                 else
  133.                 {
  134.                     exp = 1;
  135.                 }
  136.             }
  137.             else // If the value is a constant (aka no "x" character found)...
  138.             {
  139.                 value = calculate_coefficient(temp, symbol);
  140.                 exp = 0;
  141.             }
  142.             make_lists(value, exp);
  143.         }
  144.     }
  145.     cout << endl;
  146. }
  147.  
  148. int Polynomial::calculate_coefficient(string& poly_temp, string symbol)
  149. {
  150.     int string_to_int;
  151.     // If the symbol before the polynomial is positive and a variable x exists...
  152.     if (symbol == "+" && (string::npos != poly_temp.find("x")))
  153.     {
  154.         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.
  155.         if (string_to_int == 0)
  156.         {
  157.             if (string::npos != poly_temp.find("-"))
  158.             {
  159.                 string_to_int = -1;
  160.             }
  161.             else
  162.             {
  163.                 string_to_int = 1;
  164.             }
  165.         }
  166.     }
  167.     // If the symbol before the polynomial is negative and a variable x exists...
  168.     else if (symbol == "-" && (string::npos != poly_temp.find("x")))
  169.     {
  170.         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.
  171.         if (string_to_int == 0)
  172.         {
  173.             string_to_int = -1;
  174.         }
  175.     }
  176.     // Same thing but x doesn't exists (aka the value is a constant)...
  177.     else if (symbol == "+" && (string::npos == poly_temp.find("x")))
  178.     {
  179.         string_to_int = atoi(poly_temp.c_str()); // Just convert the constant to an int.
  180.     }
  181.     // Same thing as above if-statement but the sign is different...
  182.     else if (symbol == "-" && (string::npos == poly_temp.find("x")))
  183.     {
  184.         string_to_int = atoi(poly_temp.c_str()) * -1; // Same as above if-statement, but this time multiply by -1.
  185.     }
  186.     return string_to_int;
  187. }
  188.  
  189. void Polynomial::make_lists(int constant, int exponent)
  190. {
  191.     if (poly == NULL)
  192.     {
  193.         poly = new PolyNode;
  194.         poly->coefficient = constant;
  195.         poly->power = exponent;
  196.         poly->node = NULL;
  197.     }
  198.     else
  199.     {
  200.         PolyPtr temp = new PolyNode;
  201.         temp->coefficient = constant;
  202.         temp->power = exponent;
  203.         temp->node = NULL;
  204.        
  205.         PolyPtr i = poly;
  206.         while (i->node != NULL && i->power > exponent)
  207.         {
  208.             i = i->node;
  209.         }
  210.         if (i->power == exponent)
  211.         {
  212.             i->coefficient += constant;
  213.             if (i->coefficient == 0)
  214.             {
  215.                 remove_nodes(i);
  216.             }
  217.             delete temp;
  218.         }
  219.         else if (i->power > exponent)
  220.         {
  221.             temp->node = i->node;
  222.             i->node = temp;
  223.         }
  224.         else // exponent > i->power
  225.         {
  226.             if (i == poly)
  227.             {
  228.                 temp->node = poly;
  229.                 poly = temp;
  230.             }
  231.             else
  232.             {
  233.                 PolyPtr tmp = poly;
  234.                 while (tmp->node != NULL && tmp->node != i)
  235.                 {
  236.                     tmp = tmp->node;
  237.                 }
  238.                 temp->node = i;
  239.                 tmp->node = temp;
  240.             }
  241.         }
  242.     }
  243. }
  244.  
  245. void Polynomial::return_polynomial()
  246. {
  247.     if (poly == NULL) // If there is nothing in the list...
  248.     {
  249.         cout << "The polynomial is 0.\n";
  250.     }
  251.     else
  252.     {
  253.         int count = 0, value, exp;
  254.         cout << "The polynomial is ";
  255.         for (PolyPtr i = poly; i != NULL; i = i->node)
  256.         {
  257.             value = i->coefficient;
  258.             exp = i->power;
  259.             if (count == 0)
  260.             {
  261.                 if (exp > 1 && value != 0)
  262.                 {
  263.                     if (value == 1)
  264.                     {
  265.                         cout << "x^" << exp;
  266.                     }
  267.                     else if (value == -1)
  268.                     {
  269.                         cout << "-x^" << exp;
  270.                     }
  271.                     else
  272.                     {
  273.                         cout << value << "x^" << exp;
  274.                     }
  275.                 }
  276.                 else if (exp == 1 && value != 0)
  277.                 {
  278.                     if (value == 1)
  279.                     {
  280.                         cout << "x";
  281.                     }
  282.                     else if (value == -1)
  283.                     {
  284.                         cout << "-x";
  285.                     }
  286.                     else
  287.                     {
  288.                         cout << value << "x";
  289.                     }
  290.                 }
  291.                 else if (exp == 0 && value != 0)
  292.                 {
  293.                     cout << value;
  294.                 }
  295.                 else
  296.                 {
  297.                     cout << "0";
  298.                 }
  299.             }
  300.             else
  301.             {
  302.                 if (value > 0 && i != NULL)
  303.                 {
  304.                     cout << " + ";
  305.                 }
  306.                 else if (value < 0 && i != NULL)
  307.                 {
  308.                     cout << " - ";
  309.                     value *= -1;
  310.                 }
  311.  
  312.                 if (exp > 1 && value != 0)
  313.                 {
  314.                     if (value == 1)
  315.                     {
  316.                         cout << "x^" << exp;
  317.                     }
  318.                     else
  319.                     {
  320.                         cout << value << "x^" << exp;
  321.                     }
  322.                 }
  323.                 else if (exp == 1 && value != 0)
  324.                 {
  325.                     if (value == 1)
  326.                     {
  327.                         cout << "x";
  328.                     }
  329.                     else
  330.                     {
  331.                         cout << value << "x";
  332.                     }
  333.                 }
  334.                 else if (exp == 0 && value != 0)
  335.                 {
  336.                     cout << value;
  337.                 }
  338.                 else
  339.                 {
  340.                     cout << "0";
  341.                 }
  342.             }
  343.             count++;
  344.         }
  345.         cout << endl;
  346.     }
  347. }
  348.  
  349. int Polynomial::evaluate(int value)
  350. {
  351.     int temp = 0;
  352.     for (PolyPtr i = poly; i != NULL; i = i->node)
  353.     {
  354.         temp += i->coefficient * pow(value, i->power);
  355.     }
  356.     return temp;
  357. }
  358.  
  359. Polynomial operator +(const Polynomial& left, const Polynomial& right)
  360. {
  361.     Polynomial sum;
  362.  
  363.     for (PolyPtr i = left.poly; i != NULL; i = i->node)
  364.     {
  365.         sum.make_lists(i->coefficient, i->power);
  366.     }
  367.  
  368.     for (PolyPtr i = right.poly; i != NULL; i = i->node)
  369.     {
  370.         sum.make_lists(i->coefficient, i->power);
  371.     }
  372.  
  373.     return sum;
  374. }
  375.  
  376. Polynomial Polynomial::operator =(const Polynomial& right)
  377. {
  378.     Polynomial left;
  379.  
  380.     if(right.poly != NULL)
  381.     {
  382.         for(PolyPtr i = right.poly; i != NULL; i = i->node)
  383.         {
  384.             left.make_lists(i->coefficient, i->power);
  385.             make_lists(i->coefficient, i->power);
  386.         }
  387.     }
  388.     else
  389.     {
  390.         left.poly = NULL;
  391.         poly = NULL;
  392.     }
  393.  
  394.     return left;
  395. }
  396.  
  397. Polynomial operator -(const Polynomial& left, const Polynomial& right)
  398. {
  399.     Polynomial difference;
  400.  
  401.     for(PolyPtr i = left.poly; i != NULL; i = i->node)
  402.     {
  403.         difference.make_lists(i->coefficient, i->power);
  404.     }
  405.  
  406.     for(PolyPtr i = right.poly; i != NULL; i = i->node)
  407.     {
  408.         difference.make_lists(-(i->coefficient), i->power);
  409.     }
  410.  
  411.     return difference;
  412. }
  413.  
  414. Polynomial operator *(const Polynomial& left, const Polynomial& right)
  415. {
  416.     Polynomial product;
  417.  
  418.     for(PolyPtr i = left.poly; i != NULL; i = i->node)
  419.     {
  420.         for(PolyPtr k = right.poly; k != NULL; k = k->node)
  421.         {
  422.             product.make_lists(i->coefficient * k->coefficient, i->power + k->power);
  423.         }
  424.     }
  425.  
  426.     return product;
  427. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement