注册 登录
编程论坛 C++教室

不想功亏一篑啊,但目前想不出好的解决方法,望提点~~~

lianjiecuowu 发布于 2011-09-08 23:27, 419 次点击
#include<iostream>
#include<cstdlib>
#include<cmath>

using namespace std;

const int data=5;
class linklist;
class snode
{
public:
    friend class linklist;
private:
    int coef;
    int expn;
    int data;
    snode *next;
};
class linklist
{
private:
    snode *head;
public:
    linklist();
    ~linklist();
    bool isempty();
    void insertlist();
    void treavlist1();
    //void answer();
};
linklist::linklist()
{
    head=new snode;
    if(head->next==NULL)
    {
        cerr<<"memory allocation failare!"<<endl;
        return ;
    }
    head->next=NULL;
}

linklist::~linklist()
{delete head;}
bool linklist::isempty()
{
    return head->next==NULL;
}
void linklist::insertlist()
{

    int expn=0,m,n;
    int coef;
    snode *p=head;
    snode *q=NULL;
    snode *newp;
    cout<<"请输入计算的项数:     ";
    cin>>n;
    cout<<"请分别输入项的未知量x的系数:    ";
    for(m=0;m!=n;m++)
    {
        newp=new snode;
        if(newp==NULL)
        {
            cerr<<"memory allocation failare!"<<endl;
            exit(1);
        }
        cin>>coef;
        newp->data=data;
        newp->coef=coef;
        newp->expn=expn++;
        if(!p)
            p->next=newp;
        else
        {
            p->next=newp;
            p=newp;
        }
        
    }
    newp->next=NULL;
   
}
void linklist::treavlist1()
{
    snode *p=head->next;
    if(!p)
    {
        cout<<"多项式为空!"<<endl;
        return ;
    }
    while(p)
    {
        if(p!=head->next&&p->coef>0)
            cout<<"+";
        if(p->expn!=0)
        cout<<p->coef<<"x"<<p->expn;
        else
            cout<<p->coef;
        p=p->next;
    }
}
/*void linklist::answer()
{
    snode *p=head->next;
    if(!p)
    {
        cout<<"无法计算多项式的值!"<<endl;
        return ;
    }
    int s=0;
    while(p)
    {
        s+=(p->coef)*pow(p->data,p->expn);     //“pow”重载函数的调用不明确,该如何解决啊
        p=p->next;

    }

}*/
int main()
{
    linklist L;
    if(L.isempty())
        L.insertlist();
    cout<<"输入的多项式为:     ";
        L.treavlist1();
        cout<<endl;
        cout<<"代入未知数变量x的值后,多项式的值为:    ";
        //L.answer();
        system("pause");
        return 0;
        
}
3 回复
#2
rjsp2011-09-09 08:01
对于代码 pow(int,int),编译器不知道你想调用的是 pow(float,float) 还是 pow(double,double)
#3
烟雾中的迷茫2011-09-09 14:26
哪有问题?
#4
lianjiecuowu2011-09-09 16:49
已经解决了我,谢谢啊
1