Jamesyoung36 2017-03-31 09:17 采纳率: 21.1%
浏览 755

用链表写多项式,已经找出了错误位置,但是不知道错误原因

 #include<iostream>
using namespace std;
struct Node
{
    int mi, xishu;
    Node* next;
};


//初始化
Node* initll()
{
    Node* temp = new Node;
    temp->next = NULL;
    temp->mi = 0;//约定,头结点的mi数字代表该多项式的系数
    temp->xishu = 0;
    return 0;
}

void output(Node* tou)//输出多项式的样式
{
    cout << "该多项式的项数为:" << tou->mi << endl;
    cout << "多项式为:" << endl;
    cout << "            ";
    for (int i = 0; i < tou->mi; i++)
    {
        Node* temp=tou->next;
        cout << temp->xishu << "x^" << temp->mi << "+";
        temp = temp->next;
    }
}

//输入函数
void input(Node* tou)
{
    cout << "请输入多项式的位数: ";
    cin >> tou->mi;
    for (int i = 0; i < tou->mi; i++)
    {
        Node* temp = new Node;
        cout << "请输入第" << i + 1 << "位的数据" << endl;
        cout << "          ";
        cout << "请输入幂的值:";
        cin >> temp->mi;
        cout << "请输入系数的值:";
        cin >> temp->xishu;
        temp->next = tou->next;
        tou->next = temp;
    }
}

void calculate(Node* tou)//计算函数
{
    int k,x,num=0;
    cout << "请输入x的值:";
    cin >> x;
    Node* temp = tou->next;
    for (int i = 0; i < tou->mi; i++)
    {
        k = x;
        for (int i = 0; i < temp->mi; i++)
        {
            k = k*k;
        }
        num += (k*temp->xishu);
    }
    cout << "结果为:" << num << endl;
}
int main()
{
    cout << endl;
    Node* head;
    head = initll();
    input(head);
    output(head);
    calculate(head);
    return 0;
}

在input(Node* tou)函数中cin>>tou->mi;错了,是不是因为没有分配空间?初始化不是已经分配了吗?应该怎么解决呢?

  • 写回答

1条回答 默认 最新

  • 仅仅学会简单 2017-03-31 09:33
    关注

    //初始化
    Node* initll()
    {
    Node* temp = new Node;
    temp->next = NULL;
    temp->mi = 0;//约定,头结点的mi数字代表该多项式的系数
    temp->xishu = 0;
    return 0;
    }
    return 0??? return temp吧

    评论

报告相同问题?

悬赏问题

  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题