/*==========================================
A: 1*(x^2) + 2*(x^3) +....+ 4*(x^15) + ...
B: 5*(x^2) + 4*(x^4) +....+ 8*(x^15) + ...
计算 A + B ,A - B ,A * B = ?
测试数据:
5
-2 1
4 -5
-5 1
6 8
9 8
3
3 0
2 -2
-1 -2
==========================================*/
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
int list_coe;
int list_index;
struct node *next;
}*linklist,listnode;
void swap(int *a,int *b){ int temp = *a; *a = *b; *b = temp; }//交换两个数
void lsort(int *index ,int *coe,int len) //降序排列
{
int i,j;
for( i = 0 ; i < len-1 ; i ++)
for( j = i+1;j < len;j ++ ){
if( *( index + i ) < *( index + j) ){
swap( (index + i) , (index + j) );
swap( (coe + i) , (coe + j) );
}
}
}
int cmp(int a,int b) { return ((a > b)?1:(a < b?-1:0)); } //比较大小
linklist create_list(int *coe,int *index,int length) // 建立链表
{
linklist head = NULL,p;
int i;
for(i = 0;i < length;i ++) //头插法,倒序
{
p = (listnode*)malloc(sizeof(listnode));
p->list_coe = *(coe + i);
p->list_index = *(index + i);
p->next = head;
head = p;
}
return (head);
}
linklist add_list(linklist head0,linklist head1) //相加函数
{
linklist sum,p,q,r,head;
p = head0;
q = head1;
sum = NULL;
while( p && q )
{
r = (linklist)malloc(sizeof(listnode));
switch( cmp( p->list_index , q->list_index ) )//方法与减法函数类似
{
case 1 :
r->list_coe = q->list_coe;
r->list_index = q->list_index;
q = q->next;
break;
case 0 :
r->list_coe = p->list_coe + q->list_coe;
r->list_index = p->list_index;
p = p->next;
q = q->next;
break;
case -1:
r->list_coe = p->list_coe;
r->list_index = p->list_index;
p = p->next;
break;
}
if(sum == NULL)
head = r;
else
sum->next = r;
sum = r;
}
if(p == NULL)
sum->next = q;
else
sum->next = p;
return (head);
}
linklist min_list(linklist head0,linklist head1) //相减函数
{
linklist head,p,q,r,min;
min = head = NULL; //新表的头尾指针 ,尾插法建立新表 (差)
p = head0; q = head1;
while( p && q )
{
r = (linklist)malloc(sizeof(listnode));
switch( cmp( p->list_index , q->list_index ) ) //根据两链表的节点值大小选择将插入到新表中的结点
{
case 1 : //将指数小的先插入
r->list_coe = q->list_coe * (-1);
r->list_index = q->list_index;
q = q->next;
break;
case 0 : //指数相等时将两结点的系数相减放入新表
r->list_coe = p->list_coe - q->list_coe;
r->list_index = p->list_index;
p = p->next;
q = q->next;
break;
case -1:
r->list_coe = p->list_coe;
r->list_index = p->list_index;
p = p->next;
break;
}
if(min == NULL)
head = r;
else
min->next = r;
min = r;
}
if(p == NULL)
min->next = q;
else
min->next = p;
return (head);
}
linklist chen_list(linklist head0,linklist head1) //相乘函数
{
linklist p,q,r;
linklist head[100],maf[100]; //用指针数组,每个指针指向的链都是
int i = 0,j; //一个多项式中的一项跟另一个多项式中全部项的乘积
p = head0; //然后将这多条链表相加即可
while(p)
{
head[i] = NULL;
maf[i] = head[i];
q = head1;
while(q) //相乘时,指数相加,系数相乘,赋给新表的结点中的值
{
r = (linklist)malloc(sizeof(listnode));
r->list_coe = p->list_coe * q->list_coe;
r->list_index = p->list_index + q->list_index;
if(maf[i] == NULL)
head[i] = r;
else
maf[i]->next = r;
maf[i] = r;
q = q->next;
}
maf[i]->next = NULL;
i ++; //一共有 i 条链
p = p->next;
}
linklist sum; //sum 为多条链相加后的新链的头指针
sum = add_list(head[0],head[1]);
for(j = 2;j < i;j ++)
sum = add_list(sum,head[j]);
return (sum);
}
void print_list(linklist head)
{
linklist p = head;
while(p)
{
printf("%d*",p->list_coe);
printf(p->list_index >= 0 ? "(x^%d)" : "(x^(%d))" , p->list_index);
p = p->next;
if(p) { if(p->list_coe >= 0) printf("+"); }
else printf("\n\n");
}
}
void bing_list(int *index , int *coe , int *len) //合并同类项
{
int f = 1,h,u,i;
for(i = 1;i < *len ;)
{
for(h = 0;h < f;h ++)
{
if( *(index + i) == *(index + h) )//后面的指数有跟前面的相等,则将前项将后项系数加上
{
*(coe + h) += *(coe + i);
break;
}
}
if(h >= f) { f ++; i ++ ; }
else
{
for(u = i+1;u < *len;u ++) //有相同项则将后面的逐项前移
{
*(coe + u-1) = *(coe + u);
*(index + u-1) = *(index + u);
}
(*len) --;
}
}
}
int main(int argc, char *argv[])
{
linklist head[2]; //指向两个多项式的头指针
linklist sum,lit,pro;//和,差,积
int coefficient[20],index[20],length[2];//系数,指数,项数
int i,j,h,f,u;
for(j = 0;j < 2 ;j ++)
{
printf("请输入第%d个多项式的项数:",j+1);
scanf("%d",&length[j]);
printf("请输入%d组数据,每组包括系数和指数!\n",length[j]);
for(i = 0;i < length[j];i ++)
scanf("%d%d",&coefficient[i],&index[i]);
bing_list( index , coefficient , &length[j] );//将输入数据合并同类项
lsort( index , coefficient , length[j]);
head[j] = create_list( coefficient , index , length[j] );
printf("构造的多项式为:\t");
print_list( head[j] );
}
printf("两多项式进行运算\n\n");
sum = add_list( head[0] , head[1] );
printf("相加得: ");
print_list(sum);
lit = min_list(head[0],head[1]);
printf("相减得: ");
print_list(lit);
pro = chen_list(head[0],head[1]);
printf("相乘得: ");
print_list(pro);
system("PAUSE");
return 0;
}
一元多项式的运算(加、减、乘)C 实现
最新推荐文章于 2022-11-01 22:00:56 发布