- #include<iostream>
- #include<math.h>
- using namespace std;
- struct polynomial //定义多项式的结构体。
- {
- float coefficient;
- int power;
- struct polynomial *next;
- };
- int n=0;//n表示节点数。
- struct polynomial *creat()//创建链表函数creat,返回头指针。//
- {
- polynomial *p1,*p2,*head;
- head=NULL;
- p1=new(polynomial);
- cout<<"please input information:";
- p2=p1;
- cin>>p1->coefficient>>p1->power;
- while(p1->coefficient!=0)
- {
- n++;
- if(n==1)//如果是第一个节点。
- {head=p1;}
- else//假如不是第一个节点。
- {p2->next=p1;}
- p2=p1;
- p1=new(polynomial);
- cin>>p1->coefficient>>p1->power;
- }
- delete(p1);
- p2->next=NULL;
- return(head);
- }
- void main()
- {
- struct polynomial *head,*p;
- int i;
- float sum=0,x;
- head=creat();//创建链表,获得head的值,即可进入对数据进行操作。
- cout<<"Please input x:";
- cin>>x;
- p=head;
- if (NULL==p){return;}
- do
- {
- sum=sum+p->coefficient*pow(x,p->power);
- p=p->next;
- }while(p!=NULL);
- cout<<"the answer is:"<<sum;
- }
- //该片段来自于http://www.codesnippet.cn/detail/2608201410319.html
来源: http://www.codesnippet.cn/detail/2608201410319.html