开发者

overloading stream operator >> for polynomials

how could I overload input stream operator for this code:

struct Node
{
    int degree;
    int coeff;
    Node* link;
};

Node* cons (int c,int d,Node* p);
Node* doCancelation(int d,Node* p);

class polynomial
{
private:
    Node* poly;
    static const int CHAR='X';
    char character;
public:
    polynomial();
    polynomial(int c,int d);
    //void printPoly()const;
    void insert (int c,int d);
    int degree() const;
    int coeff(int d) const;
    void setPrintVariable(char x);
    // changes the variable used when printing the polynomial
    char getPrintVariable() const;
    // returns the variable used when printing the polynomial
    friend std::ostream& operator << (std::ostream &output, const polynomial &a);
    friend std::istream& operator >> (std::istream &input, polynomial&a);
};

polynomial::polynomial()
{
    poly= new Node;
    poly=NULL;
}
polynomial::polynomial(int c,int d)
{
    character = CHAR;
    poly= new Node;
    poly->coeff=c;
    poly->degree=d;
    poly->link=NULL;

}

 void polynomial::setPrintVariable(char x)
{
    character=x;
}

char polynomial::getPrintVariable() const
{
    return character;
}

void polynomial::insert (int c,int d)
{
    if(poly==NULL)
    {
        poly=cons(c,d,poly);
        return;
    }
    if(d<poly->degree)
    {
        poly=cons(c,d,poly);
        return;
    }
    if(d==poly->degree)
    {
        if(c==-(poly->coeff))
       开发者_运维知识库     poly=doCancelation(d,poly);
        else
            poly->coeff += c;
        return;
    }
    Node* q=poly;
    Node* r=q->link;
    while(r!=NULL && d>=r->degree)
    {
        if(d==r->degree)
        {
            if(c==-(r->coeff))
                poly=doCancelation(d,poly);
            else
                r->coeff += c;
            return;
        }
        q=r;
        r=r->link;
    }
    q->link=cons(c,d,r);
}

Node* doCancelation(int d,Node* p)
{
    if(p==NULL)return p;
    if(p->degree==d)
    {
        Node* q=p->link;
        delete p;
        return q;
    }
    else
    {
        p->link = doCancelation (d,p->link);
        return p;
    }
}

std::ostream& operator << (std::ostream &output, const polynomial &a)
{
    Node* q=a.poly;
    if(a.poly==NULL)
        output<<"( )";
    else
        while(q != NULL)
        {
            output<<std::showpos<<q->coeff<<std::noshowpos<<'x'<<"^"<<q->degree<<" ";
            q=q->link;
        }
        return output;
}


const polynomial operator +(const polynomial &a,const polynomial &b )
{

}

int polynomial::degree() const
{
    Node* q=poly;
    if(poly==NULL)
        return 0;
    while(q->link !=NULL)
        q=q->link;
    return q->degree;
}

int polynomial::coeff(int d) const
{
    Node* q=poly;
    if(poly==NULL)
        return 0;
    while(q !=NULL && d <= q->degree)
    {
        if(d==q->degree)
            return q->coeff;
        q=q->link;
    }
    return 0;
}

Node* cons (int c,int d,Node* p)
{
    Node* q= new Node;
    q->coeff=c;
    q->degree=d;
    q->link=p;
    return q;
}


Small remark: you have a memory leak in your constructor without arguments. First you allocate & create a new Node, then you set poly pointing to this Node to NULL. You just lost your single pointer to the allocated Node.

As for you question: assuming you get well-formed input, why don't you split the string at every + (and possibly -), then extract the coefficient and degree, create nodes for each pair you extracted, sort them by degree and put them in a polynomial?

Btw, I don't see any method that allows you to link nodes together. So I think you have some more methods to implement.


What you want to do is called "parsing". Stroustrup demonstrates it in "The C++ Programming Language" when he writes a calculator to evaluate expressions like "1 + (5 * 14)". Your expressions are only a little bit harder, with x as a variable. You could take his example as a base for parsing, but the evaluation will be a bit harder for you (e.g. "(1+x)*(1-x)" which is of course 1-x2)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜