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 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了