1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
|
/*
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;
}
|