一元多项式的加法和乘法运算

来源:互联网 发布:火车头采集器数据导出 编辑:程序博客网 时间:2024/04/30 18:27
/***程序功能:一元多项式的加法和乘法操作**存储:用数组存储多项式的所有系数,用一个整型值存储最高指数*/#include<stdio.h>#include<stdlib.h>#define COFFICIENT_MAX 1000/*此结构用来存储一个多项式*/struct polynomial{int cofficient_array[COFFICIENT_MAX];  /*多项式的系数用数组来存储*/int highest_exponent;/*指数最高项的指数*/};void poly_init (struct polynomial *poly){int i;for(i=0; i<COFFICIENT_MAX; i++)poly->cofficient_array[i] = 0;poly->highest_exponent = 0;}void poly_add( struct polynomial *poly1, struct polynomial *poly2, struct polynomial *polysum){int i;polysum->highest_exponent = (poly1->highest_exponent > poly2->highest_exponent) ? poly1->highest_exponent : poly2->highest_exponent;for(i=0; i <= polysum->highest_exponent; i++)polysum->cofficient_array[i] = poly1->cofficient_array[i] + poly2->cofficient_array[i];}void poly_mul( struct polynomial *poly1, struct polynomial *poly2, struct polynomial *polymul){int i,j;polymul->highest_exponent = poly1->highest_exponent + poly2->highest_exponent;for(i=0; i <= poly1->highest_exponent; i++)for(j=0; j<= poly2->highest_exponent; j++)polymul->cofficient_array[i+j] += poly1->cofficient_array[i] * poly2->cofficient_array[j]; }void poly_print(struct polynomial *poly){int i;printf("cofficients: ");for(i=0; i<=poly->highest_exponent; i++)printf("%d ",poly->cofficient_array[i]);printf("\nexponent= %d\n", poly->highest_exponent);}int main(){struct polynomial *poly1, *poly2, *polysum, *polymul;poly1 = malloc( sizeof(struct polynomial));poly2 = malloc( sizeof(struct polynomial));polysum = malloc( sizeof(struct polynomial));polymul = malloc( sizeof(struct polynomial));poly_init(polysum);poly_init(polymul);poly1->cofficient_array[0] = 4;poly1->cofficient_array[1] = 1;poly1->cofficient_array[2] = 0;poly1->cofficient_array[3] = 3;poly1->highest_exponent = 3;poly_print(poly1);poly2->cofficient_array[0] = 1;poly2->cofficient_array[1] = 6;poly2->cofficient_array[2] = 5;poly2->highest_exponent = 2;poly_print(poly2);poly_add(poly1, poly2, polysum);poly_print(polysum);poly_mul(poly1, poly2, polymul);poly_print(polymul);return 0;}


/***程序功能:一元多项式的加法和乘法操作; **存储:采用带表头结点的链表存储一个一元多项式,链表的每个结点存储单项的系数和指数**注意:本程序针对的情况是:结点的顺序按照指数降序排列;**如果结点顺序按指数升序排列,程序稍微修改就可以使用**如果乱序存放,则算法会失败*/#include<stdio.h>#include<stdlib.h>/* 多项式中的每个结点*/struct item_node   {int coefficient;  /*系数*/int exponent;   /*指数*/struct item_node *next;};/*将一个多项式存到链表中,coefs是系数数组,exps是指数数组,n是两个数组的长度*/struct item_node * poly_store(int *coefs, int *exps, int n){struct item_node *head;struct item_node *curr, *new;new = malloc(sizeof(struct item_node));  /*表头结点,不存储有用的信息*/head = curr = new;if(n>0){int i;for(i=0; i<n; i++){new = malloc(sizeof(struct item_node));   /*创建一个新结点*/new->coefficient = coefs[i];new->exponent = exps[i];curr->next = new;     /*存到表尾*/curr = new;}}new->next = NULL;return head;}/*打印多项式*/void poly_print(struct item_node *poly){struct item_node *curr = poly->next;   /*第一个有用结点*/printf("polynomial:");while(curr != NULL){printf("(%d,%d) ",curr->coefficient, curr->exponent);curr = curr->next;}printf("\n");}/*多项式加法操作,返回结果多项式的头指针*/struct item_node * poly_add(struct item_node *poly1, struct item_node *poly2){struct item_node *head, *new, *curr;struct item_node *curr1, *curr2;new = malloc(sizeof(struct item_node));  /*表头结点,不存储有用的信息*/head = curr = new;curr1 = poly1->next, curr2 = poly2->next;while(curr1 != NULL && curr2 != NULL){new = malloc( sizeof(struct item_node) );   /*创建新结点,插入到结果链表中*/curr->next = new;curr = new;/*修改新结点的 系数值 和 指数值*/if(curr1->exponent == curr2->exponent){curr->coefficient = curr1->coefficient + curr2->coefficient;curr->exponent = curr1->exponent;curr1 = curr1->next;curr2 = curr2->next;}else if(curr1->exponent > curr2->exponent){curr->coefficient = curr1->coefficient;curr->exponent = curr1->exponent;curr1 = curr1->next;}else{curr->coefficient = curr2->coefficient;curr->exponent = curr2->exponent;curr2 = curr2->next;}}while(curr1 != NULL)   /*处理链表1中剩余的项*/{new = malloc( sizeof(struct item_node) );   /*创建新结点,插入到结果链表中*/curr->next = new;curr = new;curr->coefficient = curr1->coefficient;curr->exponent = curr1->exponent;curr1 = curr1->next;}while(curr2 != NULL)    /*处理链表2中剩余的项*/{new = malloc( sizeof(struct item_node) );   /*创建新结点,插入到结果链表中*/curr->next = new;curr = new;curr->coefficient = curr2->coefficient;curr->exponent = curr2->exponent;curr2 = curr2->next;}new->next = NULL;return head;}struct item_node * poly_mul(struct item_node *poly1, struct item_node *poly2){struct item_node *head, *new, *curr, *prev;struct item_node *curr1, *curr2;int coef, exp;new = malloc(sizeof(struct item_node));  /*表头结点,不存储有用的信息*/new->next = NULL;head = new;curr1 = poly1->next, curr2 = poly2->next;while(curr1 != NULL){prev = head;curr = head->next;while(curr2 != NULL){coef = curr1->coefficient * curr2->coefficient;exp = curr1->exponent + curr2->exponent;while(curr != NULL && curr->exponent > exp){prev = curr;  curr = curr->next;}if(curr != NULL && curr->exponent == exp)    /*当存储结果的链表中有系数相同的项时,合并同类项*/curr->coefficient += coef;else  /*否则插入创建新结点,并插入当前结点的前面,下面的代码可以处理插入表头,表尾的特殊情况*/{new = malloc(sizeof(struct item_node));new->coefficient = coef;new->exponent = exp;new->next = curr;prev->next = new;/*prev务必始终指向curr的前一个结点,否则插入后会丢失原来处在prev和curr之间的一个或者多个结点*由于这句丢失花了好长时间才找到原因*/prev = new;    }curr2 = curr2->next;}curr1 = curr1->next;curr2 = poly2->next;   /*每进行一次外层循环,curr2就必须指向poly2的第一项*/}return head;}int main(){int coefs1[] = {4, 5};int exps1[] = {3, 2};int coefs2[] = {7, 2, 1, 11};int exps2[] = {4, 3, 1, 0};struct item_node *head1 = poly_store(coefs1, exps1, 2);struct item_node *head2 = poly_store(coefs2, exps2, 4);struct item_node *sum, *mul;poly_print(head1);poly_print(head2);sum = poly_add(head1, head2);poly_print(sum);mul = poly_mul(head1, head2);poly_print(mul);return 0;}


0 0