Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <sstream>
- using namespace std;
- class Node{
- private:
- int coefficient;
- char variable;
- int exponent;
- Node * next;
- friend class linkedList;
- public:
- //Constructor 1
- Node (int coeff, int exp)
- {
- this->coefficient = coeff;
- this->exponent = exp;
- if (exp == 0)
- {
- this->variable = NULL;
- }
- else
- {
- this->variable = 'x';
- }
- }
- //Constructor 2
- Node (int coeff, int exp, Node* next)
- {
- this->coefficient = coeff;
- this->exponent = exp;
- this->next = next;
- if (exp == 0)
- {
- this->variable = NULL;
- }
- else
- {
- this->variable = 'x';
- }
- }
- ~Node()
- {
- }
- };
- class linkedList
- {
- private:
- Node *listHead;
- public:
- //Constructor
- linkedList()
- {
- listHead = new Node (-9999,0,NULL);
- }
- //Function to find the spot of where to insert the node into the list.
- Node* findSpot (int coefficient, int exponent)
- {
- Node *spot = listHead;
- while(spot->next != NULL && spot->next->exponent >= exponent)
- {
- spot = spot->next;
- }
- return spot;
- }
- //Function to insert into the linked list
- void listInsert(int coefficient, int exponent)
- {
- Node *spot = findSpot (coefficient, exponent);
- if(spot->exponent == exponent )
- {
- int temp;
- temp = spot->coefficient + coefficient;
- spot->coefficient = temp;
- }
- else
- {
- Node* newNode = new Node(coefficient, exponent);
- newNode->next = spot->next;
- spot->next = newNode;
- }
- }
- //Function used to insert into the linked list but subtracting like powers.
- void listInsertSubtraction(int coefficient, int exponent)
- {
- Node *spot = findSpot (coefficient, exponent);
- if(spot->exponent == exponent )
- {
- int temp;
- temp = spot->coefficient - coefficient;
- spot->coefficient = temp;
- }
- else
- {
- int tempcoeff;
- tempcoeff = -coefficient;
- Node* newNode = new Node(coefficient, exponent);
- newNode->next = spot->next;
- spot->next = newNode;
- }
- }
- //Linked List Print function
- void printList(ofstream& out)
- {
- Node* spot = listHead;
- out << "Polynomial in canonical form - " << endl;
- while(spot != NULL)
- {
- if (spot->next != NULL && spot->coefficient == 1 && spot->exponent > 0)
- {
- out << "(" << spot->variable << "^" << spot->exponent << ")" << " + ";
- }
- else if(spot->next != NULL && spot->coefficient != 1 && spot-> exponent > 0)
- {
- out << "(" << spot->coefficient << spot->variable << "^" << spot->exponent << ")" << " + ";
- }
- else if (spot->exponent == 0 && spot->coefficient > -9999)
- {
- out << "(" << spot->coefficient << ")" << endl << endl;
- }
- /*else if (spot->next != NULL && spot->coefficient == 1 && spot->exponent == 0)
- {
- out << "(" << spot->variable << "^" << spot->exponent << ")" << " + ";
- }*/
- /*else if (spot->next != NULL && spot->coefficient == 1 && spot->exponent == 0)
- {
- out << "(" << spot->coefficient << ")";
- }*/
- spot = spot->next;
- }
- }
- //Function to multiply two polynomials.
- linkedList polynomialMultiplication (linkedList& polynomialA, linkedList& polynomialB)
- {
- linkedList newPolynomial;
- //Temporary Nodes point to the first element of each linked list.
- Node* tempNodeA = polynomialA.listHead->next;
- Node* tempNodeB = polynomialB.listHead->next;
- while (tempNodeA != NULL && tempNodeB != NULL)
- {
- int coefficientC;
- int exponentC;
- //Foils out the first element of polynomial A with all the nodes in B.
- coefficientC = tempNodeA->coefficient * tempNodeB->coefficient;
- exponentC = tempNodeA->exponent + tempNodeB->exponent;
- newPolynomial.listInsert (coefficientC, exponentC);
- tempNodeB = tempNodeB->next;
- //When B hits the end of the list
- if (tempNodeB == NULL)
- {
- //While loop for A with nodes remaining.
- while (tempNodeA->next != NULL)
- {
- //Move the pointer to the next node in A, and reset the pointer of B to the beginning of the list.
- tempNodeA = tempNodeA->next;
- tempNodeB = polynomialB.listHead->next;
- //Repeating the foil until it reaches the end of the list.
- while (tempNodeB != NULL)
- {
- coefficientC = tempNodeA->coefficient * tempNodeB->coefficient;
- exponentC = tempNodeA->exponent + tempNodeB->exponent;
- newPolynomial.listInsert (coefficientC, exponentC);
- tempNodeB = tempNodeB->next;
- }
- }
- }
- }
- //Returns the whole new linked list.
- return newPolynomial;
- }
- //Function to add two polynomials.
- linkedList polynomialAddition (linkedList polynomialA, linkedList polynomialB)
- {
- linkedList newPolynomial = polynomialA;
- //Temporary Nodes point to the first element of each linked list.
- Node* tempNodeB = polynomialB.listHead->next;
- while (tempNodeB != NULL)
- {
- newPolynomial.listInsert (tempNodeB->coefficient, tempNodeB->exponent);
- tempNodeB = tempNodeB->next;
- }
- return newPolynomial;
- }
- //Function to subtract the 2nd polynomial from the first polynomial.
- linkedList polynomialSubtraction (linkedList polynomialA, linkedList polynomialB)
- {
- linkedList newPolynomial = polynomialA;
- //Temporary Nodes point to the first element of each linked list.
- Node* tempNodeB = polynomialB.listHead->next;
- while (tempNodeB != NULL)
- {
- newPolynomial.listInsertSubtraction (tempNodeB->coefficient, tempNodeB->exponent);
- tempNodeB = tempNodeB->next;
- }
- return newPolynomial;
- }
- };
- int main(int argc, char* argv[])
- {
- ifstream infile;
- ofstream outfile;
- infile.open(argv[1]);
- outfile.open(argv[2]);
- int exponent;
- int coefficient;
- string line;
- string line2;
- linkedList polynomialA;
- linkedList polynomialB;
- //Populating polynomial 1
- getline (infile, line);
- stringstream ss(line);
- while (ss >> coefficient >> exponent)
- {
- polynomialA.listInsert(coefficient, exponent);
- }
- //Populating polynomial 2
- getline (infile, line2);
- stringstream ss2(line2);
- while (ss2 >> coefficient >> exponent)
- {
- polynomialB.listInsert(coefficient, exponent);
- }
- infile.close();
- polynomialA.printList(outfile);
- polynomialB.printList(outfile);
- linkedList polynomialC;
- polynomialC = polynomialC.polynomialMultiplication(polynomialA, polynomialB);
- outfile << endl;
- outfile << "Printing the multiplication of the first polynomial and the second polynomial" << endl << endl;
- polynomialC.printList(outfile);
- outfile << endl;
- outfile << "Printing addition of the first polynomial and the second polynomial" << endl << endl;
- linkedList polynomialD;
- polynomialD = polynomialD.polynomialAddition(polynomialA, polynomialB);
- polynomialD.printList(outfile);
- outfile << endl;
- outfile << "Printing subtraction of the second polynomial from the first polynomial" << endl << endl;
- linkedList polynomialE;
- polynomialE = polynomialE.polynomialSubtraction(polynomialA, polynomialB);
- polynomialE.printList(outfile);
- return 0;
- }
- //output
- Polynomial in canonical form -
- (6x^6) + (9x^2) + (-5)
- Polynomial in canonical form -
- (7x^7) + (2x^5) + (7x^2) + (12)
- Printing the multiplication of the first polynomial and the second polynomial
- Polynomial in canonical form -
- (42x^13) + (12x^11) + (63x^9) + (42x^8) + (-17x^7) + (72x^6) + (-10x^5) + (63x^4) + (73x^2) + (-60)
- Printing addition of the first polynomial and the second polynomial
- Polynomial in canonical form -
- (7x^7) + (6x^6) + (2x^5) + (16x^2) + (7)
- Printing subtraction of the second polynomial from the first polynomial
- Polynomial in canonical form -
- (0x^7) + (6x^6) + (0x^5) + (9x^2) + (-5)
Advertisement
Add Comment
Please, Sign In to add comment