7-22 一元多项式的乘法与加法运算 (20 分)
设计函数分别求两个一元多项式的乘积与和。
输入格式:
输入分2行,每行分别先给出多项式非零项的个数,再以指数递降方式输入一个多项式非零项系数和指数(绝对值均为不超过1000的整数)。数字间以空格分隔。
输出格式:
输出分2行,分别以指数递降方式输出乘积多项式以及和多项式非零项的系数和指数。数字间以空格分隔,但结尾不能有多余空格。零多项式应输出0 0
。
输入样例:
4 3 4 -5 2 6 1 -2 0 3 5 20 -7 4 3 1
输出样例:
15 24 -25 22 30 21 -10 20 -21 8 35 6 -33 5 14 4 -15 3 18 2 -6 1 5 20 -4 4 -5 2 9 1 -2 0
第一次做:
#include <stdio.h> #include <malloc.h> typedef int ElemType; typedef struct LNode { ElemType real; ElemType index; struct LNode *next; }LNode, *LinkList; void Input(LinkList &l) { int n; LNode *temp, *p; scanf("%d", &n); p = l; while(n--){ temp = (LNode*)malloc(sizeof(LNode)); temp->next = NULL; scanf("%d %d", &temp->real, &temp->index); p->next = temp; p = temp; } } void Output(LinkList &l) { LNode *p = l->next; if(!p) printf("0 0 "); else while(p){ if(p->next == NULL){ printf("%d %d ", p->real, p->index); } else printf("%d %d ", p->real, p->index); p = p->next; } } void Insert(LinkList &l, int real, int index) { LNode *p = l->next; LNode *temp, *pre = l; temp = (LNode *)malloc(sizeof(LNode)); temp->real = real; temp->index = index; temp->next = NULL; if(!p){ pre->next = temp; temp->next = p; } else{ while(p){ if((p->index)<index){//在第一个小于此数位置插入数据 pre->next = temp; temp->next = p; break; } else if((p->index) == index){ if(p->real + real != 0) (p->real) += real; else//如果系数为零则删除此节点 pre->next = p->next; break; } else{ p = p->next; pre = pre->next; } } if(!p){ pre->next = temp; temp->next = p; } } } LinkList MP(LinkList &l1, LinkList &l2) { int real[1010], index[1010], len=0, i ; LNode *p1, *p2; LinkList List; List = (LNode *)malloc(sizeof(LNode)); List->next = NULL; p1 = l1->next; p2 = l2->next; while(p1){ while(p2){ real[len] = (p1->real) * (p2->real); index[len] = (p1->index) + (p2->index); ++len; p2 = p2->next; } p2 = l2->next; p1 = p1->next; } for(i=0; i<len; ++i){ Insert(List,real[i],index[i]); } return List; } LinkList Plus(LinkList &l1, LinkList &l2) { LNode *p1, *p2; p1 = l1->next; p2 = l2->next; LinkList List; List = (LNode *)malloc(sizeof(LNode)); List->next = NULL; while(p1){ Insert(List, p1->real, p1->index); p1= p1->next; } while(p2){ Insert(List, p2->real, p2->index); p2= p2->next; } return List; } int main() { int real, index; int n1, n2; LinkList L1, L2, L3, L4; L1 = (LNode *)malloc(sizeof(LNode)); L2 = (LNode *)malloc(sizeof(LNode)); L3 = (LNode *)malloc(sizeof(LNode)); L4 = (LNode *)malloc(sizeof(LNode)); L1->next = NULL; L2->next = NULL; L3->next = NULL; L4->next = NULL; Input(L1); Input(L2); L3 = MP(L1,L2); L4 = Plus(L1,L2); Output(L3); Output(L4); }
第二次做:
#include<stdio.h> #include<malloc.h> typedef struct LNode { int Coef; int Index; struct LNode * next; }LNode, *List; void InitList(List &L) { L = (LNode*)malloc(sizeof(LNode)); L->next = NULL; } void InsertLNode(List &L, int coef, int index)//插入,利用该函数将运算出的每一项插入得出的总结果 { LNode *temp, *p = L->next, *pre = L; temp = (LNode*)malloc(sizeof(LNode)); temp->Coef = coef; temp->Index = index; temp->next = NULL; if(L->next == NULL){ L->next = temp; return ; } while(p != NULL){ if(p->Index == index){//指数相等,判断是叠加还是相消 if(p->Coef + coef == 0) pre->next = p->next;//消除 else p->Coef += coef;//叠加 return ; } else if(p->Index > index){//后移 p = p->next; pre = pre->next; } else if(p->Index < index){//找到插入位置,插入 pre->next = temp; temp->next = p; return ; } } if(p == NULL){//插入位置在最后,在最后面插入 pre->next = temp; } } List Plus(List L1, List L2)//相加运算 { List L; InitList(L); LNode *p1 = L1->next; LNode *p2 = L2->next; while(p1){ InsertLNode(L,p1->Coef, p1->Index); p1 = p1->next; } while(p2){ InsertLNode(L,p2->Coef, p2->Index); p2 = p2->next; } return L; } List MuTi(List L1, List L2)//相乘运算 { List L; InitList(L); LNode *p1 = L1->next; LNode *p2 = L2->next; int coef, index; while(p1){ while(p2){ coef = (p1->Coef) * (p2->Coef); index = (p1->Index) + (p2->Index); InsertLNode(L, coef, index); p2 = p2->next; } p2 = L2->next; p1 = p1->next; } return L; } void CoutList(List L)//输出多项式 { LNode *p = L->next; if(p == NULL){ printf("0 0 "); return ; } while(p != NULL){ if(p->next != NULL) printf("%d %d ", p->Coef, p->Index); else printf("%d %d ", p->Coef, p->Index); p = p->next; } } void CreatList(List &L)//构造多项式 { InitList(L); LNode *temp, *p = L; int N; scanf("%d", &N); for(int i=0; i<N; i++){ temp = (LNode*)malloc(sizeof(LNode)); scanf("%d %d", &(temp->Coef), &(temp->Index)); temp->next = NULL; p->next = temp; p = temp; } } int main() { List L1, L2, L3, L4; CreatList(L1); CreatList(L2); L3 = MuTi(L1, L2); L4 = Plus(L1, L2); CoutList(L3); CoutList(L4); }