//不考虑存在指数为负数的情况 输入的时候指数是从低到高操作
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define LEN sizeof(struct Polynomial)
typedef struct LNode
{
float coef;
int expn;
}LNode;
typedef struct Polynomial
{
LNode data;
struct Polynomial *next;
}*Poly;
//1建立多项式
void CreatePoly( Poly &P )
{
Poly temp, rear;
P = (Poly) malloc (LEN);
if( !P )
exit(0);
P->next = NULL;
int i=1, n;
rear = P;
printf("输入多项式的项数:");
scanf("%d", &n);
while( i<=n )
{
temp = (Poly) malloc (LEN);
if( !temp )
exit(0);
loop:
printf("输入第%d项的系数:", i);
scanf("%f", &temp->data.coef);
if( !temp->data.coef )
goto loop;
printf("输入指数:");
scanf("%d", &temp->data.expn);
temp->next = rear->next;
rear->next = temp;
rear = temp;
++i;
}
}
//输出多项式
void PrintPoly( Poly P )
{
Poly temp = P->next;
if( !temp )
exit(0);
printf(" %.2f", temp->data.coef);
printf("X^");
printf("%d", temp->data.expn );
temp = temp->next;
while( temp )
{
if( temp->data.coef>0 )
printf(" +");
printf(" %.2f", temp->data.coef );
printf("X^");
printf("%d", temp->data.expn );
temp = temp->next;
}
printf("\n");
}
//求导函数
void DerivationPoly( Poly P )
{
Poly rear, temp, q;
temp = (Poly) malloc (LEN);
if( !temp )
exit(0);
temp->next = NULL;
rear = temp;
if( !P->next )
return;
P = P->next;
while( P )
{
if( P->data.expn )
{
q = (Poly) malloc (LEN);
q->data.coef = P->data.coef * P->data.expn;
q->data.expn = P->data.expn - 1;
q->next = rear->next;
rear->next = q;
rear = q;
}
P = P->next;
}
if( !temp->next )
printf("0\n");
else
PrintPoly( temp );
}
//求多项式的积分
void Inteqral( Poly P )
{
Poly rear, temp, q;
if( !P->next )
return;
P = P->next;
//不考虑存在指数为负数的情况
temp = (Poly) malloc (LEN);
if( !temp )
exit(0);
temp->next = NULL;
rear = temp;
while( P )
{
q = (Poly) malloc (LEN);
q->data.coef = P->data.coef/(P->data.expn+1);
q->data.expn = P->data.expn+1;
q->next = rear->next;
rear->next = q;
rear = q;
P = P->next;
}
PrintPoly( temp );
}
//求给定X的初值求多项式的值
void Evalution( Poly P )
{
if( !P->next )
exit(0);
double x, sum=0.0;
printf("输入x的值:");
scanf("%f", &x);
while( P )
{
sum += P->data.coef*pow(x,P->data.expn);
P = P->next;
}
printf("多项式的值为:%.2f\n", sum);
}
//两多项式相加
void AddPoly( Poly &temp, Poly P1, Poly P2 )
{
Poly p, rear;
temp = (Poly) malloc (LEN);
if( !temp )
exit(0);
temp->next = NULL;
rear = temp;
P1 = P1->next;
P2 = P2->next;
while( P1 && P2 )
{
if( P1->data.expn > P2->data.expn )
{
p = (Poly) malloc (LEN);
p->data.coef = P2->data.coef;
p->data.expn = P2->data.expn;
p->next = rear->next;
rear->next = p;
rear = p;
P2 = P2->next;
}
else if( P1->data.expn < P2->data.expn )
{
p = (Poly) malloc (LEN);
p->data.coef = P1->data.coef;
p->data.expn = P1->data.expn;
P1 = P1->next;
p->next = rear->next;
rear->next = p;
rear = p;
}
else if( P1->data.expn == P2->data.expn )
{
P1->data.coef += P2->data.coef;
if( P1->data.coef )//为零就删除该结点
{
p = (Poly) malloc (LEN);
p = (Poly) malloc (LEN);
p->data.coef = P1->data.coef;
p->data.expn = P1->data.expn;
P1 = P1->next;
P2 = P2->next;
p->next = rear->next;
rear->next = p;
rear = p;
}
}
}
if( P2 )
rear->next = P2;
if( P1 )
rear->next = P1;
if( !temp->next )
printf("0\n");//相加的结果刚好为0
}
//两多项式相乘
void Mult(Poly &temp, Poly P1, LNode P2 )
{
temp = (Poly) malloc (LEN);
if( !temp )
exit(0);
temp->next = NULL;
Poly rear = temp, p;
P1 = P1->next;
while( P1 )
{
P1->data.coef *= P2.coef;
if( P1->data.coef )
{
p = (Poly) malloc (LEN);
if( !p )
exit(0);
p->data.coef = P1->data.coef;
p->data.expn = P1->data.expn+P2.expn;
p->next = rear->next;
rear->next = p;
rear = p;
}
P1 = P1->next;
}
//
PrintPoly(temp);
}
void MultPoly( Poly P1, Poly P2 )
{
if( !P2 && !P1 )
return;
Poly sum1, sum2, temp;
P2 = P2->next;
Mult( temp, P1, P2->data );
sum2 = sum1 = temp;
P2 = P2->next;
while( P2 )
{
sum1 = sum2;
Mult( temp, P1, P2->data );
AddPoly( sum2, sum1, temp );
//AddPoly( sum, temp );
P2 = P2->next;
}
printf("输出相乘后的多项式:");
PrintPoly(sum2);
}
int main()
{
Poly p1, p2, temp;
CreatePoly( p1 );
printf("输出多项式:\n");
PrintPoly( p1 );
CreatePoly( p2 );
printf("输出多项式:\n");
PrintPoly( p2 );
MultPoly( p1, p2 );
//
AddPoly(temp, p1, p2 );
//
printf("输出相加后的多项式:\n");
//
PrintPoly( temp );
/*
printf("输出对多项式求导的结果:\n");
DerivationPoly( p1 );
printf("输出对多项式积分的结果:\n");
Inteqral( p1 );
*/
//
Evalution( p1 );
return 0;
}