重载流运算符>>对于多项式

时间:2011-04-07 11:13:48

标签: c++

如何为此代码重载输入流运算符:

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;
}

2 个答案:

答案 0 :(得分:1)

小注:你的构造函数中没有参数就有内存泄漏。首先你分配&amp;创建一个新的Node,然后将poly指向此NodeNULL。你刚丢失了指向分配的Node的单个指针。

至于你的问题:假设你得到格式良好的输入,你为什么不在每个+(和可能的 - )分割字符串,然后提取系数和度数,为你提取的每一对创建节点,对它们进行排序按度数并将它们放在多项式中?

顺便说一句,我没有看到任何允许您将节点链接在一起的方法。所以我认为你有更多的方法可以实现。

答案 1 :(得分:0)

您想要做的是“解析”。 Stroustrup在编写计算器来评估像“1 +(5 * 14)”这样的表达式时,在“C ++编程语言”中演示了它。只有x作为变量,你的表达式才有点难度。您可以将他的示例作为解析的基础,但评估对您来说会有点困难(例如“(1 + x)*(1-x)”,当然是1-x 2