#include <iostream> #include <cstdlib> using namespace std; struct PolyNode { double coef; int exp; }; struct LNode { PolyNode data; LNode *next; }; typedef LNode *LinkList; void createPolyList(LinkList &L,int n) { L=new LNode; L->next=NULL; LNode *r,*p; r=L; for(int i=1; i<=n; i++) { p=new LNode; cin>>p->data.coef>>p->data.exp; r->next=p; r=p; } r->next=NULL; } int main() { LinkList L; int n; double x; cin>>n; createPolyList(L,n); cin>>x; cout<<getValue(L,x); return 0; }
时间: 2024-01-27 08:05:23 浏览: 24
这段代码中缺少了 `getValue` 函数的实现,所以需要将其补充完整。代码如下:
```cpp
#include <iostream>
#include <cstdlib>
#include <cmath>
using namespace std;
struct PolyNode {
double coef;
int exp;
};
struct LNode {
PolyNode data;
LNode *next;
};
typedef LNode *LinkList;
void createPolyList(LinkList &L, int n) {
L = new LNode;
L->next = NULL;
LNode *r, *p;
r = L;
for (int i = 1; i <= n; i++) {
p = new LNode;
cin >> p->data.coef >> p->data.exp;
r->next = p;
r = p;
}
r->next = NULL;
}
double getValue(LinkList L, double x) {
double res = 0;
LNode *p = L->next;
while (p) {
res += p->data.coef * pow(x, p->data.exp);
p = p->next;
}
return res;
}
int main() {
LinkList L;
int n;
double x;
cin >> n;
createPolyList(L, n);
cin >> x;
cout << getValue(L, x);
return 0;
}
```
需要注意,在实现 `getValue` 函数时,需要将 `pow` 函数包含进来,所以需要在代码文件头部添加包含头文件 `#include <cmath>`。