
1 #include<stdio.h> 2 #include<math.h> 3 #include<stdlib.h> 4 typedef struct{ 5 int ceof; 6 int exp; 7 }PolyTerm; 8 typedef struct{ 9 PolyTerm *element; 10 int length; 11 }SeqPoly; 12 Create(SeqPoly*P) 13 { 14 scanf("%d",&P->length); 15 P->element=(PolyTerm*)malloc((P->length+1)*sizeof(PolyTerm)); 16 for(int i=1;i<=P->length;i++) 17 { 18 scanf("%d",&P->element[i].ceof); 19 scanf("%d",&P->element[i].exp); 20 } 21 } 22 Sub(SeqPoly*A,SeqPoly*B,SeqPoly*P) 23 { 24 int a=1,b=1,p=1; 25 P->element=(PolyTerm*)malloc((A->length+B->length)*sizeof(PolyTerm)); 26 while(a<=A->length&&b<B->length) 27 { 28 while(a<=A->length&&A->element[a].exp<B->element[b].exp) 29 { 30 P->element[p].ceof=A->element[a].ceof; 31 P->element[p].exp=A->element[a].exp; 32 p++,a++; 33 } 34 while(b<B->length&&A->element[a].exp>B->element[b].exp) 35 { 36 P->element[p].ceof=0-B->element[b].ceof; 37 P->element[p].exp=B->element[b].ceof; 38 p++,b++; 39 } 40 if(A->element[a].exp==B->element[b].exp) 41 { 42 if(A->element[a].ceof==B->element[b].ceof); 43 else { 44 P->element[p].ceof=A->element[a].ceof-B->element[b].ceof; 45 P->element[p].exp=A->element[a].exp; 46 p++; 47 } 48 a++,b++; 49 } 50 } 51 while(a<=A->length) 52 { 53 P->element[p].ceof=A->element[a].ceof; 54 P->element[p].exp=A->element[a].exp; 55 a++,p++; 56 } 57 while(b<=B->length) 58 { 59 P->element[p].ceof=0-B->element[b].ceof; 60 P->element[p].exp=B->element[b].exp; 61 b++,p++; 62 } 63 P->length=p-1; 64 65 } 66 Order(SeqPoly*P) 67 { 68 for(int i=1;i<=P->length;i++) 69 { 70 printf("第%d项%dx^%d\n",i,P->element[i].ceof,P->element[i].exp); 71 } 72 } 73 int main() 74 { 75 SeqPoly P1,P2,P; 76 Create(&P1),Create(&P2); 77 Sub(&P1,&P2,&P); 78 Order(&P); 79 return 0; 80 81 }