Hi there. I am trying to code a program to add or subtract two polynomials using linked lists. For ex. (+4x^2-2x^1) + (+2x^12-5x^5). So far I have a read function which takes in a string and uses substrings to take the coefficient and exponents and insert it to a node. For example, it reads the string "+4x^3+3x^1", takes the coefficient of 4, turns it into an int, and saves it in the coefficient of struct Node, takes the exponent of 3, and saves it in the exponent variable of that same node, and then points to a new node, which is then filled with coefficient of 3 and exponent of 1. I have tested my code and it works. But now I want to be able to write a function in which I can look at the first node of the Original Polynomial, and a Secondary Polynomial, and add or subtract them and put it into a node of a Result Polynomial, and then keep on moving through the nodes. I was thinking of writind a function void Polynomial::Result(Polynomial Original, Polynomial Second){ but I do not know how to do that. Here is my code:

``````#include <iostream>
#include <string>
#include <sstream>
using namespace std;

struct Node
{
int coefficient;    // the coefficient of the term
int exponent;       // the exponent of the term
Node* next;         // NULL if there is no next node

Node(){                     //initialize the Node
coefficient = 0;
exponent = 0;
next = NULL;
}
Node(int c, int e){         //Takes two integer and stores them in
coefficient = c;        //the coefficients and exponents
exponent = e;
next = NULL;
}
};

class Polynomial{
private:
string list;

public:
Polynomial();           //Constructor
~Polynomial();          //Destructor
void Print();           //outputs the current nodes in polynomial form
int strtoint(string);
};
cout << "---------------" << endl;
cout << "1: Read" << endl;
cout << "2: Print" << endl;
cout << "3. Add" << endl;
cout << "4. Subtract" << endl;
cout << "0: exit" << endl;
cout << "---------------" << endl;
}
int main(){
Polynomial Original;                    //create the first linked list named Original
Polynomial Second;                      //create the second linked list named second,
//used to do operations on the first linked list
Polynomial Result;                      //Final linked list that has the result of the operations

int MenuAction;                         //Stores the action the user wants the program to perform

case 1:
break;
case 2:
Original.Print();
break;
case 3:
break;
case 4:
break;
}
break;

//would like to do next

}
return 0;
}

Polynomial::Polynomial(){                   //Constructor method
list = "";
}

Polynomial::~Polynomial(){                  //Destructor Method
Node* temp;

delete temp;
}
}

int coeficient;                                                     //Initialize coefficient to be used in the Node
int exponent;                                                       //Initialize exponent to be used in the Node
cout << "Please enter a polynomial (ex. +4x^3-2x^0)" << endl;
cin >> list;

string tempString = list.substr(0);                                 //Make a substring named tempString
//that is an exact copy of string list
//loop starts here
while(tempString != ""){
bool negative = tempString.at(0) == '-';                            //if the sign is -, the bool negative == true
coeficient = strtoint(tempString.substr(1,tempString.find('x')));   //next element in string will be the coefficient
tempString = tempString.substr(tempString.find('^') +1);
if (tempString.find('-') < tempString.find('x')){                    //if the index of '-' is left of 'x'
exponent = strtoint(tempString.substr(0,tempString.find('-'))); //the exponent = the numbers between '^' and '-'
tempString = tempString.substr(tempString.find('-'));           //set tempString = the remainder of the string
}else if (tempString.find('+') < tempString.find('x')){              //if the index of '+' is left of 'x'
exponent = strtoint(tempString.substr(0,tempString.find('+'))); //the exponent = the numbers between '^' and '+'
tempString = tempString.substr(tempString.find('+'));           //set tempString = the remainder of the string
}else{
exponent = strtoint(tempString);                                //case to exit the loop when we reach the end of the string
tempString = "";
}
if (negative)                                                       //if bool coefficient == true, make it negative
coeficient *= -1;
head = new Node(coeficient,exponent);                           //send coefficient and exponent to the first node
else
for (Node *temp = head; temp->next != NULL; temp = temp->next)
temp->next = new Node(coeficient, exponent);
//loop ends here
}
}

int Polynomial::strtoint(string str){                               //Function that takes in a string with numbers and converts
int integer;                                                    //it to an integer
std::istringstream(str) >> integer;
return integer;
}

void Polynomial::Print(){                                           //Prints the LinkedList to the screen
for (Node* temp = head; temp != NULL; temp = temp->next){
cout << temp->coefficient << "x^" << temp->exponent << endl;
}
}

int holdcoefficient;
int holdexponent;
for (Node* temp = head; temp != NULL; temp = temp->next){
}
}
*/
``````

Any ideas are appreciated :)