Hello, I am currently working on a project that requires me to write write a program that adds, subtracts, multiplies and evaluates polynomials. I am required to use linked lists of nodes. I am stuck on writing the code to input the polynomial. I understand how I will input each term of the polynomial into nodes, but I don't understand how to retain the entire polynomial from the user input. The user input should be as follows: (EX: 2x^2 + 2x^1 - 5). Please understand that I am not asking any of you to write this program for me, I am simply stuck on how to handle the input from the user. I have thought of using an array, but I'm pretty sure I'm supposed to input the polynomial directly into a linked list of terms(nodes). Here is the code I have so far:
#include<iostream>
using namespace std;
// STRUCTURE:
// Node structure (each node represents a term in a polynomial)
struct Node
{
int coeff, exp;
Node* link;
};
typedef Node* NodePtr;
// Defines NodePtr to point to structure.
// CLASS:
// Poly Class (represents an entire polynomial consisting of terms)
class Poly
{
public:
friend istream& operator >>(istream& ins, Poly& f);
// Handles input
friend ostream& operator >>(ostream& outs, Poly& f);
// Handles output
Poly();
// Default Constructor
void create_node();
// Creates nodes based on how many men are input in line.
void create_add_node();
// Creates additional nodes.
//void choose_winner();
// Cycles through the linked list (men in line) and eliminates every
// third man until node = node->link (one man is left).
private:
Node* link;
NodePtr front, current;
int c, e;
char ch, ch2;
string input;
};
int main()
{
Poly test;
cin >> test;
return(0);
}
Poly::Poly() : link(NULL), current(NULL), front(NULL), c(0), e(0)
{
}
istream& operator >>(istream& ins, Poly& f)
{
ins >> f.c >> f.ch >> f.ch2 >> f.e;
// Inputs first term.
f.create_node();
while(ins != '\0')
{
ins >> f.c >> f.ch >> f.ch2 >> f.e;
f.create_add_node();
}
return ins;
}
ostream& operator >>(ostream& outs, Poly& f)
{
return outs;
}
void Poly::create_node()
{
front = new Node;
front->coeff = c; // Creates first node.
front->exp = e;
front->link = NULL;
current = front; // Sets current pointer to the first node.
cout << front->coeff;
}
void Poly::create_add_node()
{
NodePtr tmp = new Node;
tmp->coeff = c;
tmp->exp = e;
tmp->link = NULL;
current->link = tmp;
current = tmp;
}
I am assuming that I am over thinking the process. I appreciate your time and appreciate any input you may have. Kind Regards...