Note that the expression =(x+1)(x+2)(x+3) has three polynomial operands and is shorthand for the expression (x + 1) * (x + 2) * (x + 3). There may be a standard polynomial class for you to use to evaluate this, though I'm not sure about that. If not, my first crack at defining a polynomial class would be something like this:
(NOTE: I have not compiled or tested the code appearing below. It may, and probably does, have numerous bugs in it. It is intended as a presentation of a concept, not as a working example):
Define a polynomial as a container of terms and a term as a aggregate type of coefficient and base with base as an aggregate type of variable and exponent. Then multiplication of polynomials can be accomplished by taking each term in one polynomial times each term in the other polynomial. Multiplication of terms involves multiplication of coefficients and addition of exponents of bases of the same type.
Code:
class Term(
{
int coeff;
char var;
int exp;
friend Term & operator*(Term &, Term &);
friend ostream & operator<<(ostream &, Term &);
};
Term & operator *(Term & lhs, Term & rhs)
{
Term result;
result.coeff = lhs.coeff * rhs.coeff;
result.var = lhs.var;
result.exp = lhs.exp + rhs.exp;
return result;
}
ostream & operator<<(ostream & os, Term & t)
{
os << t.coeff << t.var << '^' << t.exp;
return os;
}
class Polynomial
{
vector<Term> terms;
friend Polynomial & operator*(Polynomial &, Polynomial &);
friend ostream & operator<<<<(ostream &, Polynomial &);
};
Polynomial & operator*(Polynomial & lhs, Polynomial & lhs)
{
int i, j, k = 0;
Polynomial result
for(i = 0; i < lhs.terms.size(); ++i)
{
for(j = 0; j < rhs.terms.size(); ++j)
{
result[k++] = lhs[i] * rhs[j];
}
}
//collect like terms here;
return result;
}
ostream & operator<<(ostream & os, Polynomial & p)
{
int i;
for(i = 0; i < p.terms.size() - 1; ++i)
{
os << p[i] << " + ";
os << p.[i];
}
return os;
}
This is similar to the approach I've been using to evaluate first order polynomial equations for a single unknown with nested parenthesis. The trick has been to be able to tokenize the original string input such that individual terms/polynomials/operands can be identified with more complex expressions than the type described above.