#include<iostream>
//#include<string>
using namespace std;
class polynomial
{
public:
float coef;
int exp;
polynomial *next;
//void count(polynomial *p,int x);
};
int main()
{
void count(polynomial *p,int x);
// string str;
polynomial *p;
p=new polynomial;
int e;
float c;
polynomial *q,*r;
q=p;
r=p;
// cout<<p;
// cout<<q;
// cout<<r;
while(1)
{
cin>>c;
if(c=='\n')
break;
// if(c==0)
// continue;
cin>>e;
if(e=='\n')
break;
q->coef=c;
q->exp=e;
r->next=q;
r=q;
}
// cout<<q;
r->next=p;
int x;
cin>>x;
count(p,x);
return 0;
}
void count(polynomial *p,int x)
{
float c;
int e;
int f=1;
float num=0;
polynomial *k,*m;
k=p;
m=p;
while(k!=m)
{
c=k->coef;
e=k->exp;
p=k->next;
k=p;
if(e>=1)
{
for(e;e>0;e--)
{
f=x*f;
num+=c*f;
}
f=1;
}
if(e==0)
num+=c;
if(e<0)
{
for(e;e<0;e++)
{
f=f/x;
num+=c*f;
}
f=1;
}
}
cout<<num;
}
这是我的代码有一些语句是调试的时候注释掉了,无视就好。
问题可能有不少吧~菜鸟一只,目前发现的主要问题就是main函数中构建链表的循环不跳出,我的想法是一旦输入回车就跳出多项式的输入。不过不能实现。
也许还有一些其他的问题我还没发觉,想了好几个小时了,还是不能解决,求大神帮助下。谢谢
慕妹3146593