/** *@brief 求两个一元多项式P(X)和Q(X)的乘积 P(x) * Q(X) For two unary polynomial P (X) and Q (X) = P (X) (X) * Q *@author Hxm *@date 2016/3/26 * *@note 要求:1.通过键盘随机输入两多项式P(X) 和Q(X)的内容 2.输出结果要有P(X)和Q(X)的以及它们的乘积P(X)*Q(X) Requirements: 1. The random input through the keyboard two polynomial P (X) and Q (X) 2. The output should have P and Q (X) (X) and their product P * Q (X) (X) */ #include<stdio.h> #include<stdlib.h> #include<windows.h> #include<malloc.h> typedef struct polyNode { float coef; int expon; struct polyNode *next; }pNode; void face() { system("color e4"); printf("-------------------------------------------------------------------\n"); printf("*********two polynomial add, muilt, plus*******\n"); printf("------Please chose one operation to start------\n "); printf("0.exit\n"); printf("1.add two polynomial:\n"); printf("2.sub two polynomial :\n"); printf("3.multiply two polynomial:\n "); } void insertPoly(pNode *head, pNode *input) { pNode *p, *q; int flag=0; p=head; //p is the prior link node if(p->next==NULL){ p->next=input; } else{ q=p->next; while(flag==0){ if(input->expon<q->expon){ if(q->next==NULL){ q->next=input; flag=1; } else{ p=q; q=p->next; } } else{ if(input->expon>q->expon){ input->next=q; p->next=input; flag=1; } else{ q->coef=q->coef+input->coef; flag=1; free(input); if(q->coef==0){ p->next=q->next; free(q); } } } } } } pNode *creatPoly(char pch) { pNode *head, *input; float x; int y; head=(pNode *)malloc(sizeof(pNode)); head->next=NULL; printf("Please enter polynomial %c:(style is :coef expon; input 0 0 to end ) \n",pch); scanf("%f %d", &x, &y); while(x!=0){ input=(pNode *)malloc(sizeof(pNode)); input->coef=x; input->expon=y; input->next=NULL; insertPoly(head, input); printf("please enter the next term of polynomial %c: (input 0 0 to end) \n", pch); scanf("%f %d", &x, &y); } return head; } pNode *addPoly(pNode *head, pNode *prior) { pNode *input; int flag=0; while(flag==0){ if(prior->next==NULL) flag=1; else{ prior=prior->next; input=(pNode *)malloc(sizeof(pNode)); input->coef=prior->coef; input->expon=prior->expon; input->next=NULL; insertPoly(head, input); } } return head; } pNode *subPoly(pNode *head, pNode *prior) { pNode *input; int flag=0; while(flag==0){ if(prior->next==NULL){ flag=1; } else{ prior=prior->next; input=(pNode *)malloc(sizeof(pNode)); input->coef=0-prior->coef; input->expon=prior->expon; input->next=NULL; insertPoly(head, input); } } return head; } pNode *multPoly(pNode *head1, pNode *head2) { pNode *input, *r, *p; int flag=0; r=(pNode *)malloc(sizeof(pNode)); r->next=NULL; head1=head1->next; p=head2; while(flag==0){ if(p->next==NULL){ p=head2; head1=head1->next; continue; } if(head1==NULL){ flag=1; continue; } p=p->next; input=(pNode *)malloc(sizeof(pNode)); input->coef=p->coef * head1->coef; input->expon=p->expon + head1->expon; input->next=NULL; insertPoly(r,input); } return r; } void displayPoly(pNode *head) { pNode *p; int flag=0; p=head->next; if(head->next==NULL){ printf("0\n"); return ; } while(flag==0){ if(p->coef>0 && head->next!=p) printf("+"); if(p->coef==1) ; else if(p->coef==-1) printf("-"); else printf("%f",p->coef); if(p->expon!=0) printf("x^%d", p->expon); else if((p->coef==1) || (p->coef==-1)) printf("1"); if(p->next==NULL) flag=1; else p=p->next; } printf("\n"); } int main() { pNode *p , *q; int choice=-1; face(); while(choice!=0){ scanf("%d", &choice); switch(choice) { case 0: break; case 1: { printf("Your choice is: add two polynomial:\n"); p=creatPoly(‘p‘); //input polynomial printf("P(x)="); displayPoly(p); q=creatPoly(‘q‘); printf("Q(X)="); displayPoly(q); printf("R(x)=P(x)+Q(x)="); p=addPoly(p, q); displayPoly(p); choice=-1; face(); break; } case 2: { printf("your choice is: sub two polynomial:\n"); p=creatPoly(‘p‘); //input polynomial printf("P(x)="); displayPoly(p); q=creatPoly(‘q‘); printf("Q(X)="); displayPoly(q); printf("R(x)=P(x)-Q(x)="); p=subPoly(p, q); displayPoly(p); choice=-1; face(); break; } case 3: { printf("your choice is: multiply two polynomial:\n"); p=creatPoly(‘p‘); //input polynomial printf("P(x)="); displayPoly(p); q=creatPoly(‘q‘); printf("Q(X)="); displayPoly(q); printf("R(x)=P(x)*Q(x)="); p=multPoly(p, q); displayPoly(p); choice=-1; face(); break; } default: { printf("Error input!! please choose again~~~\n"); face(); break; } } } }
运行效果如下:
时间: 2024-10-27 13:29:15