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...

If the polynomial is restricted to a single variable polynomial then each node needs to have a coefficient and an exponent. You can recieve user input for each, individually or you can accept the full polynomial from the user as a string and break it down term by term until you reach that point. Once you have the coefficient and exponent for a term you can create a new node and add it to the list of nodes. You may or may not want a function to simply the polynomial into standard form and you may or may not want to include nodes that have zero for the coefficient.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.