多项式加减乘除法算法实现

#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>

typedef struct _POLYNODE{
 float coef;//系数
 int exp;//指数
 struct _POLYNODE *next;
}polynode,*polyptr;

void createPoly(polynode **P, char ch[]);//建立多项式链表
polyptr polyAdd(polynode *A,polynode *B);//多项式加
polyptr polyMinus(polynode *A,polynode *B);//减
polyptr polyMulti(polynode *A,polynode *B);//乘
polyptr polyDiv(polynode *A,polynode *B);//除
void order(polynode **P);//排序
void display(polynode *P);//展示多项式
void destroy(polynode **P);//销毁多项式
void menu();//命令菜单
int isPut(char ch[]);
//菜单
void menu(){
 printf("1.输入多项式./n"
  "2.多项式相加./n"
  "3.多项式相减./n"
  "4.多项式相乘./n"
  "5.多项式相除./n"
  "6.显示多项式./n"
  "7.销毁多项式./n"
  "8.退出./n");
}
//判断菜单选择
int IsChoice(int choice){
 if(0 < choice && 9 > choice)
  return 1;
 else
  return 0;
}

int isPut(char ch[])
{
 int i,j = 1;
 for(i = 0; ch[i] != '/0'; i++)
 {
  if(0 == j && '^' == ch[i])
   return 0;
  if('^' == ch[i] && 1 == j)
   j = 0;
  if(('+' ==ch[i] || '-' == ch[i] || '*' == ch[i] || '/' == 
   ch[i]) && 0 == j)
   j = 1;
  
  if('x' != ch[i] && 'X' != ch[i] && '^' != ch[i] && '+' != 
   ch[i] && '-' != ch[i] && '*' != ch[i] && '/' != ch[i] && !
   isdigit(ch[i]))
   return 0;
  else
  {
   if('+' ==ch[0] || '*' == ch[0] || '/' == ch[0] || '^' == 
    ch[0])
    return 0;
   if('/0' == ch[i+1] && ('+' ==ch[0] || '*' == ch[0] || '/' 
    == ch[0] || '^' == ch[0]))
    return 0;
   // 上面是判断字符串首尾是否合格  下面是中间部分
   
   if(0 != i && ch[i+1] != '/0' )
   {
    if(('X' == ch[i] || 'x' == ch[i]) && !isdigit(ch[i-1]) 
     && '+' != ch[i-1] && '-' != ch[i-1] && '*' != ch[i-1] && '/' 
     != ch[i-1])
     return 0;
    if(('X' == ch[i] || 'x' == ch[i]) && '^' != ch[i+1] && 
     '+' != ch[i+1] && '-' != ch[i+1] && '*' != ch[i+1] && '/' != 
     ch[i+1])
     return 0;
    if(('+' == ch[i] || '-' == ch[i] || '*' == ch[i] || '/' 
     == ch[i]) && !isdigit(ch[i-1]) && 'X' != ch[i-1] && 'x' != 
     ch[i-1] && !isdigit(ch[i+1]) && 'X' != ch[i+1] && 'x' != ch
     [i+1])
     return 0;
    if('^' == ch[i] && 'X' != ch[i-1] && 'x' != ch[i-1])
     return 0;
    if('^' == ch[i] && !isdigit(ch[i+1]))
     return 0;
   }
  }
 }
 return 1;
}


void createPoly(polynode **P, char ch[])
{//写到这里
 char *t = ch;
 int i = 0, j = 1;
 int iscoef = 1,isminus = 1;
 polyptr Q,L;
 
 if('-' == ch[0])
 {
  isminus = -1;
  t++;
 }
 while('/0' != *t)
 {
  Q = (polyptr)malloc(sizeof(polynode));
  Q->coef = 1;
  Q->exp = 0;
  Q->next = NULL;//申请节点,初始化参数为1.
  
  if(-1 == isminus)
  {
   Q->coef *= isminus;
   isminus = 1;
  }
  
  while('+' != *t && '-' != *t && '*' != *t && '/' != *t && 
   '/0' != *t)
  {
   if('x' != *t && 'X' != *t)
   {
    while(isdigit(*t))
    {
     i =((int)*t - 48) + i*10;
     t++;
     j *= i;
    }//抽取数字

    if(1 == iscoef && 0 != i)
    {
     Q->coef *= i;
    }
    if(0 == iscoef)
    {
     Q->exp += i;
     iscoef = 1;
    }
    *t;
   }
   else
   {
    iscoef = 0;
    t++;
    if('^' == *t)
     t++;
    else
     Q->exp = 1;
   }
   i = 0;
  }//while 遍历到加减乘除,则退出循环,到下一新的节点
  printf("系数:%f,指数:%d/n",Q->coef,Q->exp);
  iscoef = 1;
  if('/0' != *t)
  {
   if('-' == *t)
    isminus = -1;
   t++;
  }
  if(0 == j)
  {
   Q->coef = 0;
   j = 1;
  }
  
 

  if(NULL == *P)
  {
   *P = Q;
  }
  else
  {
   L->next = Q;
  }
  L = Q;
  
 }//while遍历整个字符串

 
}
polyptr polyAdd(polynode *A,polynode *B)
{
 polyptr P = A, Q,L;
 polyptr COPYA = NULL,COPYB = NULL;
 if(NULL == A || NULL == B)
 {
  return NULL;
  printf("多项式未被建立./n");
 }
 
 while(NULL != P)
 {//复制A
  Q = (polyptr)malloc(sizeof(polynode));
  Q->coef = P->coef;
  Q->exp = P->exp;
  Q->next = NULL;
  if(NULL == COPYA)
   COPYA = Q;
  else
   L->next = Q;
  L = Q;
  P = P->next;
 }
 P = B;
 while(NULL != P)
 {//复制B
  Q = (polyptr)malloc(sizeof(polynode));
  Q->coef = P->coef;
  Q->exp = P->exp;
  Q->next = NULL;
  if(NULL == COPYB)
   COPYB = Q;
  else
   L->next = Q;
  L = Q;
  P = P->next;
 }
   
 L->next = COPYA;//把COPYA,COPYB两个多项式连接起来,整理一下 就OK了.
 order(&COPYB);
// order(&COPYB);
// printf("相加结果为:");
// display(COPYB);
 return COPYB;
// destroy(&COPYB);
}
polyptr polyMinus(polynode *A,polynode *B)
{//相减和相加差不多
 polyptr P = A, Q,L;
 polyptr COPYA = NULL,COPYB = NULL;
 if(NULL == A || NULL == B){
  return NULL;
  printf("多项式未被建立./n");
 }
 
 while(NULL != P)
 {//复制A
  Q = (polyptr)malloc(sizeof(polynode));
  Q->coef = P->coef;
  Q->exp = P->exp;
  Q->next = NULL;
  if(NULL == COPYA)
   COPYA = Q;
  else
   L->next = Q;
  L = Q;
  P = P->next;
 }
 P = B;
 while(NULL != P)
 {//复制B
  Q = (polyptr)malloc(sizeof(polynode));
  Q->coef = -(P->coef);
  Q->exp = P->exp;
  Q->next = NULL;
  if(NULL == COPYB)
   COPYB = Q;
  else
   L->next = Q;
  L = Q;
  P = P->next;
 }
   
 L->next = COPYA;//把COPYA,COPYB两个多项式连接起来,整理一下 就OK了.
 
 order(&COPYB);
// order(&COPYB);
// printf("相减结果为:");
// display(COPYB);
 return COPYB;
// destroy(&COPYB);
 
}
polyptr polyMulti(polynode *A,polynode *B)
{
 polyptr Pa = A,Pb = B, Q,L;
 polyptr COPYAB = NULL;
 if(NULL == A || NULL == B)
 {
  return NULL;
  printf("多项式未被建立./n");
 }
 
 while(NULL != Pa)
 {//复制A
  while(NULL != Pb)
  {
   Q = (polyptr)malloc(sizeof(polynode));
   Q->coef = Pa->coef * Pb->coef;
   Q->exp = Pa->exp + Pb->exp;//
   Q->next = NULL;
   if(NULL == COPYAB)
    COPYAB = Q;
   else
    L->next = Q;
   L = Q;
   Pb = Pb->next;
  }
  Pa=Pa->next;
 }
 order(&COPYAB);
// printf("相乘结果为:");
// display(COPYAB);
 return COPYAB;
// destroy(&COPYAB);
 
}
polyptr polyDiv(polynode *A,polynode *B)
{
 polyptr P = A,result=NULL,p_r=NULL, QivNum,Q,L;
 polyptr COPYA = NULL,COPYB = NULL;
 if(NULL == A || NULL == B)
 {
  return NULL;
  printf("多项式未被建立./n");
 }
 
 while(NULL != P)
 {//复制A
  Q = (polyptr)malloc(sizeof(polynode));
  Q->coef = P->coef;
  Q->exp = P->exp;
  Q->next = NULL;
  if(NULL == COPYA)
   COPYA = Q;
  else
   L->next = Q;
  L = Q;
  P = P->next;
 }
 P = B;
 while(NULL != P)
 {//复制B
  Q = (polyptr)malloc(sizeof(polynode));
  Q->coef = (P->coef);
  Q->exp = P->exp;
  Q->next = NULL;
  if(NULL == COPYB)
   COPYB = Q;
  else
   L->next = Q;
  L = Q;
  P = P->next;
 }
 order(&COPYA);
 order(&COPYB);

 while(COPYA->coef !=0 && COPYA->exp > -5)
 {
  QivNum = (polyptr)malloc(sizeof(polynode));
  QivNum->next=NULL;
  QivNum->coef = COPYA->coef/COPYB->coef;
  QivNum->exp = COPYA->exp-COPYB->exp;
  if(result ==NULL)
   result=QivNum;
  else
   p_r->next=QivNum;
  p_r=QivNum;
  //result = polyMulti(QivNum,COPYB);
  COPYA = polyMinus(COPYA,polyMulti(QivNum,COPYB));
 }
// printf("相除结果为:");
// display(result);
 return result;
// destroy(&COPYAB);

}


void display(polynode *P)
{
 //考虑情况有系数为1,指数为1,0,一般数;系数为系数不为1,指数为  1,0,一般数;
 //系数为负数,指数为1,0,一般数,主要考虑中间符号问题.
 polynode *p=P;
 if(NULL == P){
  printf("多项式为空./n");
  return ;
 }
 if(1 == P->coef){
  if(0 == P->exp)
   printf("1");
  else if(1 == P->exp) printf("x");
  else printf("x^%d",P->exp);
 }
 else{
  if(0 == P->exp)
   printf("%.1f",P->coef);
  else if(1 == P->exp) printf("%.1fx",P->coef);
  else
   printf("%.1fx^%d",P->coef,P->exp);
 }
 P = P->next;
 while(NULL != P){
  if(0 < P->coef){
   if(1 == P->coef){
    if(0 == P->exp)
     printf("+1");
    else if(1 == P->exp) printf("+x");
    else printf("+x^%d",P->exp);
   }
   else{
    if(0 == P->exp)
     printf("+%.1f",P->coef);
    else if(1 == P->exp) printf("+%.1fx",P->coef);
    else
     printf("+%.1fx^%d",P->coef,P->exp);
   }
  }
  else{
   if(-1 == P->coef){
    if(0 == P->exp)
     printf("-1");
    else if(1 == P->exp) printf("-x");
    else printf("-x^%d",P->exp);
   }
   else{
    if(0 == P->exp)
     printf("%.1f",P->coef);
    else if(1 == P->exp) printf("%.1fx",P->coef);
    else
     printf("%.1fx^%d",P->coef,P->exp);
   }
  }
  P = P->next;
 }
 printf("/n");
}

void destroy(polynode **P)
{
 polyptr Q = *P;
 if(NULL == *P)
  return ;
 while(*P != NULL)
 {
  Q = *P;
  *P = (*P)->next;
  delete Q;
 }
 
}

void order(polynode **P){
 //首先 系数为零的要清掉,其次指数从高到低排序,再者系数相同的 要合并.
 polyptr prev,curr,OUT,INcurr;//前一节点和当前节点
 float temp;
 
 //出去第一节点系数为0的项
 while(NULL != *P){
  if(0 != (*P)->coef)
   break;
  else
  {
   if(NULL == (*P)->next)
    return;
   curr = *P;
   (*P) = (*P)->next;
   delete curr;
  }
 }
 if(NULL == *P || NULL == (*P)->next)//如果只剩1项或空,则不 需要整理,退出函数
  return;
 //冒泡排序
 OUT = INcurr = *P;
 while(NULL != OUT->next)
 {//外循环
  while(NULL != INcurr->next)
  {//内循环
   prev = INcurr;
   INcurr = INcurr->next;
   if(prev->exp < INcurr->exp)
   {
    temp = prev->coef;
    prev->coef = INcurr->coef;
    INcurr->coef = temp;//交换系数
    
    temp = prev->exp;
    prev->exp = INcurr->exp;
    INcurr->exp = temp;//交换指数
   }
  }
  OUT = OUT->next;
  INcurr = *P;
 }
 //去除0项
 prev = curr = *P;
 curr = curr->next;
 while(NULL != curr)
 {
  if(0 == curr->coef)
  {
   prev->next = curr->next;
   delete curr;
   curr = prev->next;
  }
  else
  {
   prev = curr;
   curr = curr->next;
  }
 }
 //合并同类项
 OUT = INcurr = *P;
 while(NULL != OUT->next)
 {
  while(NULL != INcurr->next)
  {
   prev = INcurr;
   INcurr = INcurr->next;
   if(INcurr->exp == OUT->exp)
   {
    OUT->coef += INcurr->coef;
    prev->next = INcurr->next;
    delete INcurr;
    INcurr = prev;
   }
  }
  INcurr = OUT = OUT->next;
  if(NULL == OUT)
   return;
 }
 
}

void main()
{
 int choice;
 // int i;
 char ch[100];
 polynode *polyA,*polyB,*result;
 
 polyA = polyB = NULL;
   
 menu();
 scanf("%d",&choice);
 while(!IsChoice(choice))
 {
  menu();
  printf("输入错误,重新输入./n");
  scanf("%d",&choice);
 }
 while(8 != choice)
 {
  switch(choice)
  {
  case 1:
   if(NULL != polyA || NULL != polyB)
   {
    destroy(&polyA);
    destroy(&polyB);
    printf("原多项式被销毁./n");
   }
   printf("输入多项式A:/n");
   scanf("%s",&ch);  
   printf("/n%s/n",&ch); /
   while(!isPut(ch)){
    printf("输入错误!重新输./n");
    scanf("%s",&ch);
   }
   createPoly(&polyA,ch);//建立多项式A链表
   display(polyA);//

 


   printf("输入多项式B:/n");
   scanf("%s",&ch);
   while(!isPut(ch)){
    printf("输入错误!重新输./n");
    scanf("%s",&ch);
   }
   createPoly(&polyB,ch);//建立多项式B链表
   order(&polyB);
   order(&polyA);//整理排序多项式
   
   printf("建立多项式成功!多项式:/nA为:");
   display(polyA);
   printf("B为:");
   display(polyB);
   break;
  case 2:
   result=polyAdd(polyA,polyB);
   printf("相加结果为:");
   display(result);
   //destroy(&result);
   break;
  case 3:
   result=polyMinus(polyA,polyB);
   printf("相减结果为:");
   display(result);
    //destroy(&result);
   break;
  case 4:
   result=polyMulti(polyA,polyB);
   printf("相乘结果为:");
   display(result);
   //destroy(&result);
   break;
  case 5:
   result=polyDiv(polyA,polyB);
   printf("相除结果为:");
   display(result);
   //destroy(&result);
   break;
  case 6:
   printf("------显示多项式------/nA  :");
   display(polyA);
   printf("B  :");
   display(polyB);
   break;
  case 7:
   destroy(&polyA);
   destroy(&polyB);
   printf("此多项式已被清空./n");
   break;
  default:
   return ;
  }
  choice = 0;
  menu();
  scanf("%d",&choice);
  while(!IsChoice(choice) || 0 == choice)
  {
   menu();
   printf("输入错误,重新输入./n");
   scanf("%d",&choice);
  }
 }
}

 

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
#include<stdio.h> #include<stdlib.h> #include<malloc.h> #include<iostream> using namespace std; #define TRUE 1 #define FALSE 0 #define OK 1 #define ERROR 0 #define INFEASIBLE -1 #define OVERFLOW -2 typedef int Status; typedef struct { float coef; int expn; } term,ElemType; typedef struct polynomail { ElemType term; struct polynomail *next; }polynomail, *linklist; Status locatelem(linklist l,ElemType e,linklist &q) { linklist p,m; p=l->next; if(!p) { q=l; return FALSE; } if((p->term).expn>e.expn) {q=l;return FALSE;} else if(p->next) { while(p->next) { m=p->next; if((p->term).expn>e.expn) { q=l; return FALSE; } else if((m->term).expn>e.expn) { q=p;return FALSE;} else p=m; } q=p;return FALSE; } else{ q=p;return FALSE;} } void makenode(linklist &s,ElemType e) { s=(linklist)malloc(sizeof(polynomail)); s->term=e; } void creatpolyn(linklist &p,int m) { linklist h,q,s; ElemType e; p=(linklist)malloc(sizeof(polynomail)); p->next=NULL; for(int i=0;i<m;i++) { printf("输入一元多项式的系数\n"); scanf("%f",&e.coef); printf("输入一元多项式的指数\n"); scanf("%d",&e.expn); if(!locatelem(p,e,q)) { makenode(s,e); s->next=q->next; q->next=s; } } } void destroypolyn(linklist &p) { linklist q; while(p) { q=p->next; free(p); p=q; } } void printpolyn(linklist p) { linklist q; q=p->next; while(q) { printf("+%fX^",((q->term).coef)); printf("%d",((q->term).expn)); q=q->next; } printf("\n"); } int polynlength(linklist p) { linklist q; q=p->next; int i=0; while(q) { i++; q=q->next; } return i; } void add(linklist &pa;,linklist pb) { float sum; linklist qa,qb,qc; qa=pa->next; qb=pb->next; qc=pa; while(qa&&qb;) { if(qa->term.expn>qb->term.expn) { qc->next=qb;qc=qb;qb=qb->next; } else if(qa->term.expn<qb->term.expn) { qc->next=qa;qc=qa;qa=qa->next; } else { sum=qa->term.coef+qb->term.coef; if(sum) { qa->term.coef=sum; qc->next=qa;qc=qa; qa=qa->next; qb=qb->next; } else { qa=qa->next; qb=qb->next; } } } qc->next=qa?qa:qb; free(pb); } void sub(linklist pa,linklist pb) { float sum; linklist qa,qb,qc; qa=pa->next; qb=pb->next; qc=pa; while(qa&&qb;) { if(qa->term.expn>qb->term.expn) { qc->next=qb;qc=qb;qb=qb->next; } else if(qa->term.expn<qb->term.expn) { qc->next=qa;qc=qa;qa=qa->next; } else { sum=qa->term.coef-qb->term.coef; if(sum) { qa->term.coef=sum; qc->next=qa;qc=qa; qa=qa->next; qb=qb->next; } else { qa=qa->next; qb=qb->next; } } } qc->next=qa?qa:qb; free(pb); } void mul(linklist pa,linklist pb,linklist &ps;) { linklist qa,qb,pc,qc,qd,qf; qa=pa->next; qf=qb=pb->next; ps=(linklist)malloc(sizeof(polynomail));ps->next=NULL; pc=(linklist)malloc(sizeof(polynomail));pc->next=NULL;qc=pc; for(qa;qa!=NULL;qa=qa->next) { pc=(linklist)malloc(sizeof(polynomail));pc->next=NULL;qc=pc; qb=qf; for(qb;qb!=NULL;qb=qb->next) { qd=(linklist)malloc(sizeof(polynomail)); qd->next=NULL; qd->term.coef=qb->term.coef*qa->term.coef; qd->term.expn=qb->term.expn+qa->term.expn; qc->next=qd;qc=qc->next; } add(ps,pc); } } void main() { int n; linklist a,b,c,d,f,g,h,i,j,k,o,z; int m,l; printf("输入多项式的个数 "); scanf("%d",&m); creatpolyn( a,m); d=a; printf("得到多项式\n"); printpolyn(d); printf("输入多项式的个数 "); scanf("%d",&l); creatpolyn( b,l); f=b; printf("得到多项式\n"); printpolyn(d); printf("两项式的和\n"); g=a;h=b; add(g,h); printpolyn(g); printf("两项式的差\n"); i=a;j=b; sub(i,j); printpolyn(i); printf("两项式的积\n"); k=a;o=b; mul(k,o,z); printpolyn(z); scanf("%d",&n); }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值