"Victor Bazarov" <v.********@com Acast.net> wrote in message
news:je******** ***********@new sread1.mlpsca01 .us.to.verio.ne t...
I think FAQ 5.8 should be of some help.
OK then. Naturally, it is not compilable because of the error I mentioned
before, but this is what I have:
** CODE BEGINS **
#include <iostream>
#include <sstream>
#include <vector>
#include <string>
#include <cmath>
#include <algorithm>
using namespace std;
string &stringToSpaces (string &toSpaces);
string intToString(int from);
class AddingException {};
class Expression
{
public:
Expression();
Expression(int coeff, int xDegree, int yDegree);
Expression(cons t Expression &other);
Expression operator *(const Expression &other) const;
void operator +=(const Expression &other);
bool sameDegrees(con st Expression &other) const;
string topLine() const;
string bottomLine() const;
bool operator <(const Expression &other) const;
friend istream &operator >>(istream &strm, Expression &expr);
private:
int m_coeff, m_xDegree, m_yDegree;
};
Expression::Exp ression()
: m_coeff(0), m_xDegree(0), m_yDegree(0)
{}
Expression::Exp ression(int coeff, int xDegree, int yDegree)
: m_coeff(coeff), m_xDegree(xDegr ee), m_yDegree(yDegr ee)
{}
Expression::Exp ression(const Expression &other)
: m_coeff(other.m _coeff), m_xDegree(other .m_xDegree),
m_yDegree(other .m_yDegree)
{}
Expression Expression::ope rator *(const Expression &other) const
{
return Expression(m_co eff * other.m_coeff,
m_xDegree * other.m_xDegree ,
m_yDegree * other.m_yDegree );
}
void Expression::ope rator +=(const Expression &other)
{
if (sameDegrees(ot her))
m_coeff += other.m_coeff;
else
throw AddingException ();
}
bool Expression::ope rator <(const Expression &other) const
{
if (m_xDegree == other.m_xDegree )
return (m_yDegree < other.m_yDegree );
else return (m_xDegree > other.m_xDegree );
}
bool Expression::sam eDegrees(const Expression &other) const
{
return (m_xDegree == other.m_xDegree ) && (m_yDegree == other.m_yDegree );
}
string Expression::top Line() const
{
//0: Don't print anything if multiplied by zero
if (m_coeff == 0)
return "";
string retVal;
//1: Make a string out of the coefficient
if (m_coeff > 1 || m_coeff < -1 || (m_xDegree == 0 && m_yDegree == 0))
retVal += intToString((in t) abs(m_coeff));
//2: Prepare the coefficient section
//2.1: Change all of those elements into spaces
stringToSpaces( retVal);
//2.2: Add two spaces that correspond to the operator and the space after
it
retVal += " ";
//3: Display the xDegree
//3.1: If the xDegree is greater than zero, add a space for the x
if (m_xDegree > 0)
retVal += ' ';
//3.2: If the xDegree is greater than 1, print it out
if (m_xDegree > 1)
retVal += intToString(m_x Degree);
//4: Display the yDegree
//4.1: If the yDegree is greater than zero, add a space for the y
if (m_yDegree > 0)
retVal += ' ';
//4.2: if the yDegree is greater than 1, print it out
if (m_yDegree > 1)
retVal += intToString(m_y Degree);
//5: Add the trailing space
retVal += ' ';
return retVal;
}
string Expression::bot tomLine() const
{
if (m_coeff == 0)
return "";
string retVal;
//1: add the addition or subtraction sign
retVal += (m_coeff < 0) ? "- " : "+ ";
//2: Prepare the coefficient section
if (m_coeff > 1 || m_coeff < -1 || (m_xDegree == 0 && m_yDegree == 0))
retVal += intToString((in t) abs(m_coeff));
//3: Display the xDegree
//3.1: If the xDegree is greater than zero, add the x
if (m_xDegree > 0)
retVal += 'x';
//3.2: If the xDegree is greater than 1, print out spaces for it
if (m_xDegree > 1) {
string rep(intToString (m_xDegree));
retVal += stringToSpaces( rep);
}
//4: Display the yDegree
//4.1: If the yDegree is greater than zero, add the y
if (m_yDegree > 0)
retVal += 'y';
//4.2: if the yDegree is greater than 1, print out spaces for it
if (m_yDegree > 1) {
string rep(intToString (m_yDegree));
retVal += stringToSpaces( rep);
}
//5: Add the trailing space
retVal += ' ';
return retVal;
}
istream &operator >>(istream &strm, Expression &expr)
{
expr.m_coeff = 1;
if (strm.peek() == '-') {
expr.m_coeff = -1;
strm.get();
} else if (strm.peek() == '+') {
strm.get();
}
if (strm.peek() != 'x' && strm.peek() != 'y') {
int coeff;
strm >> coeff;
expr.m_coeff *= coeff;
}
for (int i = 0; i < 2; ++i) {
if (strm.peek() == 'x') {
strm.get();
if (strm.peek() >= '0' && strm.peek() <= '9')
strm >> expr.m_xDegree;
else expr.m_xDegree = 1;
} else if (strm.peek() == 'y') {
strm.get();
if (strm.peek() >= '0' && strm.peek() <= '9')
strm >> expr.m_yDegree;
else expr.m_yDegree = 1;
}
}
}
class ChainOfExpressi ons
{
ChainOfExpressi ons operator *(const ChainOfExpressi ons &other) const;
void insert(const Expression &expr);
friend istream &operator >>(istream &strm, ChainOfExpressi ons &expr);
friend ostream &operator <<(ostream &strm, const ChainOfExpressi ons
&expr);
private:
vector< Expression > expressions;
};
ChainOfExpressi ons
ChainOfExpressi ons::operator *(const ChainOfExpressi ons &other) const
{
vector< Expression >::const_iterat or thisIt(expressi ons.begin());
ChainOfExpressi ons retChain;
while (thisIt != expressions.end ()) {
vector< Expression >::const_iterat or otherIt(other.e xpressions.begi n());
while (otherIt != other.expressio ns.end()) {
Expression newExpr((*thisI t) * (*otherIt));
bool same = false;
vector< Expression >::iterator it(retChain.exp ressions.begin( ));
while (it != retChain.expres sions.end()) {
if (it->sameDegrees(ne wExpr)) {
(*it) += newExpr;
same = true;
}
}
if (!same)
retChain.expres sions.push_back (newExpr);
++otherIt;
}
++thisIt;
}
sort(retChain.e xpressions.begi n(), retChain.expres sions.end());
}
void ChainOfExpressi ons::insert(con st Expression &expr)
{
vector<Expressi on>::iterator it(expressions. begin());
bool same = false;
while (it != expressions.end ())
if (it->sameDegrees(ex pr)) {
(*it) += expr;
same = true;
}
if (!same)
expressions.pus h_back(expr);
}
ostream &operator <<(ostream &strm, const ChainOfExpressi ons &chain)
{
string topLine;
string bottomLine;
vector<Expressi on>::const_iter ator it(chain.expres sions.begin());
while (it != chain.expressio ns.end()) {
topLine += it->topLine();
bottomLine += it->bottomLine() ;
}
if (*(bottomLine.b egin()) == '+') {
topLine = topLine.substr( 2, 2000);
bottomLine = bottomLine.subs tr(2, 2000);
} else {
topLine = topLine.substr( 1, 2000);
bottomLine = bottomLine.subs tr(1, 2000);
bottomLine[0] = '-';
}
}
istream &operator >>(istream &strm, ChainOfExpressi ons &chain)
{
while (strm) {
Expression expr;
strm >> expr;
chain.insert(ex pr);
}
}
string &stringToSpaces (string &toSpaces)
{
string::iterato r it(toSpaces.beg in());
while (it != toSpaces.end()) {
*it = ' ';
++it;
}
return toSpaces;
}
string intToString(int from)
{
ostringstream strm;
strm << from << flush;
return string(strm.str ());
}
int main()
{
while (cin.peek() != '#') {
char buf[90];
cin.getline(buf , 90);
istringstream line1(string(bu f));
cin.getline(buf , 90);
istringstream line2(string(bu f));
ChainOfExpressi ons line1chn;
ChainOfExpressi ons line2chn;
line1 >> line1chn;
line2 >> line2chn;
ChainOfExpressi ons product(line1ch n * line2chn);
cout << product;
}
}
** CODE ENDS **