题目一:一元多项式
利用线性表实现一个一元多项式Polynomial
f(x) = a0 + a1x+ a2x2 + a3x3+ … + anxn
提示:
Polynomial的结点结构如下:
struct term
{
float coef; //系数
int expn; //指数
};
可以使用链表实现,也可以使用顺序表实现。
要求:
Ø 能够实现一元多项式的输入和输出
Ø 能够进行一元多项式相加
Ø 能够进行一元多项式相减
Ø 能够计算一元多项式在x处的值
Ø 能够计算一元多项式的导数(选作)
Ø 能够进行一元多项式相乘(选作)
Ø 编写测试main()函数测试线性表的正确性
参考代码:
#include<iostream>
using namespace std;
struct node
{
int coef;
int exp;
struct node *next;
};
void attach(int c, int e, node **rear)
{
node *p = new node;
p->coef = c;
p->exp = e;
p->next = NULL;
(*rear)->next = p; //新节点和尾节点节点连接
*rear = p; //新节点成为新的尾节点
}
void attach2(int c, int e, node **rear)
{
node *p = new node;
p->coef = -c;
p->exp = e;
p->next = NULL;
(*rear)->next = p;
*rear = p;
}
node *read()
{
int a, c, e;
node *rear;
node *t;
cout << "请输入多项式的非零项个数:" << endl;
cin >> a;
node *blank = new node;
blank->next = NULL;
rear = blank;
cout << "请按指数递降方式输入多项式非零项系数和指数:" << endl;
while (a--)
{
cin >> c >> e;
attach(c, e, &rear);
}
t = blank;
blank = blank->next;
delete t;
return blank;
}
int compare(int a, int b)
{
if (a > b)
return 1;
if (a < b)
return 0;
if (a == b)
return -1;
}
node *add(node* p1, node *p2)
{
node *front, *rear, *tt;
front = new node;
front->next = NULL;
rear = front; //该空节点为新多项式的头结点
int sum = 0;
while (p1&&p2)
{
switch (compare(p1->exp, p2->exp))
{
case 1:
attach(p1->coef, p1->exp, &rear);
p1 = p1->next;
break;
case 0:
attach(p2->coef, p2->exp, &rear);
p2 = p2->next;
break;
case -1:
sum = p1->coef + p2->coef;
if (sum)
attach(sum, p1->exp, &rear);
p1 = p1->next;
p2 = p2->next;
break;
}
}
for (; p1 != 0; p1 = p1->next)
attach(p1->coef, p1->exp, &rear);
for (; p2 != 0; p2 = p2->next)
attach(p2->coef, p2->exp, &rear);
tt = front;
front = front->next; //front指针指向了第一个存储有数据的节点
delete tt;
return front;
}
node *Minus(node* p1, node *p2)
{
node *front, *rear, *tt;
front = new node;
front->next = NULL;
rear = front;
int minus = 0;
while (p1&&p2)
{
switch (compare(p1->exp, p2->exp))
{
case 1:
attach(p1->coef, p1->exp, &rear);
p1 = p1->next;
break;
case 0:
attach2(p2->coef, p2->exp, &rear);
p2 = p2->next;
break;
case -1:
minus= p1->coef - p2->coef;
if (minus)
attach(minus, p1->exp, &rear);
p1 = p1->next;
p2 = p2->next;
break;
}
}
for (; p1 != 0; p1 = p1->next)
attach(p1->coef, p1->exp, &rear);
for (; p2 != 0; p2 = p2->next)
attach2(p2->coef, p2->exp, &rear);
tt = front;
front = front->next;
delete tt;
return front;
}
void print(node *p)
{
if (!p)
{
cout << "0 0";
cout << endl;
return;
}
int flag = 0;
while (p)
{
if (!flag)
flag = 1;
else
cout << " ";
cout << p->coef << " " << p->exp;
p = p->next;
}
cout << endl;
}
node *mult(node* p1, node *p2)
{
node *front, *rear, *tt, *t1, *t2; //用p1的每一项与p2的每一项相乘
front = new node;
front->next = NULL;
rear = front;
int c, e;
if (!p1 || !p2)
return NULL;
t1 = p1; t2 = p2;
while (t2)
{
c = t1->coef*t2->coef;
e = t1->exp + t2->exp;
attach(c, e, &rear);
t2 = t2->next;
}
t1 = t1->next;
while (t1)
{
t2 = p2;
rear = front;
while (t2)
{
c = t1->coef*t2->coef;
e = t1->exp + t2->exp;
while ((rear->next) && (rear->next->exp > e))
rear = rear->next;
if ((rear->next) && (rear->next->exp == e))
{
if ((rear->next->coef + c) != 0)
rear->next->coef += c;
else
{
tt = rear->next;
rear->next = tt->next;
delete tt;
}
}
else
{
tt = new node;
tt->coef = c;
tt->exp = e;
tt->next = rear->next;
rear->next = tt;
rear = rear->next;
}
t2 = t2->next;
}
t1 = t1->next;
}
tt = front;
front = front->next;
delete tt;
return front;
}
int calculate(node *p,int x)
{
if (!p)
{
cout << "无多项式可计算";
cout << endl;
return 0;
}
int result,result0=0,sum=0;
while (p)
{
int exp0 = 1;
for (int i = 0; i < p->exp; i++)
{
exp0 = x*exp0;
}
result0 = p->coef*exp0;
sum += result0;
p = p->next;
}
return sum;
}
int main()
{
node *p1, *p2, *pp, *ps,*pq;
p1 = read();
p2 = read();
cout << "两多项式的和为:" << endl;
pp = add(p1, p2);
print(pp);
cout << "两多项式的差为:" << endl;
pq = Minus(p1, p2);
print(pq);
cout << "两多项式的乘积为:" << endl;
ps = mult(p2, p1);
print(ps);
int x;
cout << "请输入x的值:" << endl;
cin >> x;
cout << "将x代入第一个多项式,结果为:" << endl;
cout << calculate(p1, x) << endl;
system("pause");
return 0;
}