I am looking to allow the user to submit a polynomial such as +3x^6 +2x^2 +9x^0 into a set of variables which would be sent to a linked list.

What is the best way to separate the variables to allow them to be looked at individually. So that:

C E
3 6
2 2
9 0

The user is also prompted to enter the number of coefficients they are using.

3
Contributors
6
Replies
7
Views
8 Years
Discussion Span
Last Post by timb89

Here is something I threw together really quick using a vector and a structure. Shouldn't be that hard to transfer it over to using a linked list.

``````#include <iostream>
#include <vector>

using namespace std;

struct monomial
{
int coeff;
int exp;
};

int main()
{
int numCoeff = 0;

cout << "How many coefficients will the polynomial have?" << endl;
cin >> numCoeff;
vector<monomial> polynomial(numCoeff);

for( unsigned int i = 0; i < polynomial.size(); i++ )
{
cout << "Enter the coefficient of term " << i+1 << ": ";
cin >> polynomial[i].coeff;
cout << "Enter the exponent of term " << i+1 << ": ";
cin >> polynomial[i].exp;
cout << endl;
}

for( unsigned int i = 0; i < polynomial.size(); i++ )
{
cout << polynomial[i].coeff << "x^" << polynomial[i].exp;
if( i != polynomial.size()-1 )
cout << " + ";
else
cout << endl;
}
cout << endl;

return 0;
}``````

thanks but the format needs to use linked list, and the polynomial is entered all at once. so i need to figure out how to separate the each term. im trying to use cin.ignore but am confused.

thanks! i see what you're saying but i pretty much just want for this example polynomial +3x^6 +2x^2 +9x^0:

store c = +3... ignore x and ^... store 2... store c and e in linked list
ignore space
store c = +2... ignore x and ^... store 2... store c and e in linked list
ignore space
store c = +9... ignore x and ^... store 0... store c and e in linked list

the ignore function is confusing me at the moment.

You don't need to use the ignore function, you can just do something like this :

``````const int SPACE = ' ';
//peek the next character to see if its a space, if so then discard it
if(cin.peek() == SPACE) cin.get(); //opt #1

char ch = 0;
cin.get(ch); //Opt # 2
if(ch != SPACE){ /*do something */ }

//opt #3
string term;
cin >> term; //read in a term and skip white spaces
prase(term);

//Hint on opt # 4
stringstream ss("3x^6 + 2x^2 + 9");
string str;
while(ss >> str){
cout << str << endl; //prints each term
}``````

This prompts you to type in the polynomial then it finds and replaces all "x^" with a " ". Then it loads the modified string into a stringstream and then populates the vector of monomials.

``````#include <iostream>
#include <sstream>
#include <vector>

using namespace std;

struct monomial
{
int coeff;
int exp;
};

int main()
{
stringstream ss;
int numTerms = 0;
string input = "";

cout << "How many terms will the polynomial have?" << endl;
cin >> numTerms;
cin.ignore();

vector<monomial> polynomial(numTerms);
cout << "Enter the polynomial: ";
getline(cin, input);

for( unsigned int i = 0; i < input.size(); i++ )
if( input.substr(i, 2) == "x^" )
input.replace(i, 2, " "); //replace all x^ with a space

ss << input; //add input to the stream

for( unsigned int i = 0; ss >> polynomial[i].coeff; i++ ) //while there is still information to be read from the stream
ss >> polynomial[i].exp;

for( unsigned int i = 0; i < polynomial.size(); i++ ) //output
{
cout << polynomial[i].coeff << "x^" << polynomial[i].exp;
if( i != polynomial.size()-1 )
cout << " + ";
else
cout << endl;
}
cout << endl;

return 0;
}``````