#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;错了,是不是因为没有分配空间?初始化不是已经分配了吗?应该怎么解决呢?