/*****************************************************************************************
*求多项式的和,积(改进版)
*核心思想:输入的多项式先排序,再运算
*多项式(p1,p2)求和算法: 1.多项式排序O(logMN);
* 2.求和过程,简化成了计算p1∪p2 : O(max(M,N));
*
*
*多项式(p1,p2)求积算法: 1.新建一个M*N项的单链表p3保存积: O(M*N);
* 2.排序p3:O(logMN);
* 3.合并p3中的同类项
*author;fangchang
*time: 2016/04/03 22:13
******************************************************************************************/
#include<stdio.h>
#include<stdlib.h>
typedef struct polyNode { //多项式的结构体
int coef; //系数
int exp; //指数
struct polyNode * next;
}*ppolyNode;
typedef ppolyNode poly;
poly createPoly(); //新建一个多项式
void sortPoly(poly head,poly tail); //排序多项式链表
void uniquePoly(poly head); //有序多项式合并同类项
void printPoly(poly head); //打印一个多项式
void addPoly(); //计算2个多项式相加
void multPoly(); //计算2个多项相乘
int main () {
printf("M+N:\n");
addPoly();
printf("M*N:\n");
multPoly();
fflush(stdin);
getchar();
return 1;
}
poly createPoly() { //新建一个多项式 (单链表)
int i,j;
poly head = (poly)malloc(sizeof(struct polyNode));
poly pre,cur;
if(! head) {
printf("createPoly malloc failed.\n");
return NULL;
}
head->coef=0;
head->exp=0;
head->next=NULL;
pre=head;
printf("please input coef and exp ,input 0 end input:\n");
while(scanf("%d%d",&i,&j) && i!=0) { //输入0 0 结束
cur = (poly)malloc(sizeof(struct polyNode));
if(! cur) {
printf("createPoly malloc failed.\n");
return NULL;
}
cur->coef = i;
cur->exp = j;
cur->next =NULL;
pre->next = cur;
pre = cur;
}
return head;
}
void printPoly(poly head) { //打印多项式(单链表)
if(!head || ! head->next) {
return ;
}
poly p = head->next;
while(p) {
printf("%d %d\n",p->coef,p->exp);
p=p->next;
}
}
void sortPoly(poly head,poly tail) { //单链表排序(快排: log(MN))
if ( head->next == tail || head->next->next == tail )
return;
poly mid = head->next;
poly p = head;
poly q = mid;
int pivot = mid->exp;
poly t = mid->next;
while ( t != tail )
{
if ( t->exp < pivot )
p = p->next = t;
else
q = q->next = t;
t = t->next;
}
p->next = mid;
q->next = tail;
sortPoly( head, mid );
sortPoly( mid, tail );
}
void uniquePoly(poly head) { //有序多项式合并同类项
if(!head || !head->next || !head->next->next) {
return ;
}
poly p0,p1,p2,p3;
p0=head;
p1=p0->next;
p2=p1->next;
while(p1 && p2) {
if(p2->exp == p1->exp) {
p1->coef+=p2->coef;
p3=p2->next;
p1->next=p3;
free(p2);
p2=p3;
}
p1=p2;
p2=p2->next;
}
p0=head;
p1=p0->next;
while(p1) {
if(0==p1->coef) { //删除系数为0的项
p2=p1->next;
p0->next=p2;
free(p1);
p1=p2;
}
else {
p0=p1;
p1=p1->next;
}
}
}
void addPoly() {
printf("please input poly1:\n");
poly poly1= createPoly();
printf("please input poly2:\n");
poly poly2= createPoly();
poly p1,p2;
int tmp;
if(!poly1->next && poly2->next) {
printPoly(poly2);
return;
}
if(!poly2->next && poly1->next) {
printPoly(poly1);
return;
}
sortPoly(poly1,NULL); //p1,p2的指数(exp)均有序,计算p1+p2变成了计算p1∪p2
sortPoly(poly2,NULL);
p1=poly1->next;
p2=poly2->next;
while(p1 && p2) { //计算p1∪p2
if(p1->exp==p2->exp) {
if(0!=p1->coef + p2->coef) { //合并同类型
printf("%d %d\n",(p1->coef + p2->coef),p1->exp);
}
p1=p1->next;
p2=p2->next;
}
else if(p1->exp>p2->exp) {
printf("%d %d\n",p2->coef,p2->exp);
p2=p2->next;
}
else {
printf("%d %d\n",p1->coef,p1->exp);
p1=p1->next;
}
}
while(p1) {
printf("%d %d\n",p1->coef,p1->exp);
p1=p1->next;
}
while(p2) {
printf("%d %d\n",p2->coef,p2->exp);
p2=p2->next;
}
}
void multPoly() {
printf("please input poly1:\n");
poly poly1= createPoly();
printf("please input poly2:\n");
poly poly2= createPoly();
poly p1,p2;
poly poly3,pre,cur;
int coef,exp;
if(!poly1->next || !poly2->next) {
printf("the result is 0.\n");
return ;
}
poly3=(poly)malloc(sizeof(struct polyNode));
if(!poly3) {
return ;
}
poly3->coef=0;
poly3->exp=0;
poly3->next=NULL;
pre= poly3;
for(p1=poly1->next;p1;p1=p1->next) { //新建单链表保存M*N
for(p2=poly2->next;p2;p2=p2->next) {
coef=p1->coef * p2->coef ;
exp = p1->exp + p2->exp ;
cur = (poly)malloc(sizeof(struct polyNode));
cur->coef =coef;
cur ->exp = exp;
cur->next = NULL;
pre->next = cur;
pre= cur;
}
}
sortPoly(poly3,NULL); //排序
uniquePoly(poly3); //去重,合并同类项
printPoly(poly3); //打印
}
end.
多项式的和,积(优化版)
最新推荐文章于 2022-01-23 19:16:38 发布