程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C >> C語言問答 >> 一個c語言問題

一個c語言問題

編輯:C語言問答

一個c語言問題

100

將以下多項式運算的程序進行改進,原程序中用戶輸入的多項式必須是按照降冪排列才能得到正確的結果。現在請將程序修改後運行用戶按照隨意的順序進行輸入,而程序可以實現按降冪排列後再進行運算。

#define Null  0
#define True  1
#define False 0
typedef struct polyterm
    {
     int coef;
     int exp;
     struct polyterm *next;
    } TERM;
TERM *reverse(TERM *q);
void polyout(TERM *head);

TERM *creatpoly() /**/
{
 TERM *head,*r,*s;
 int m,n;
 head=(TERM *)malloc(sizeof(TERM));
 printf("input coef and exp(1,2<CR>):\n");
 scanf("%d,%d",&n,&m);
 r=head;
 while(n)
 {
  s=(TERM *)malloc(sizeof(TERM));
  s->coef=n;
  s->exp=m;
  r->next=s;
  r=s;
  printf("\nInput coef and exp:\n");
  scanf("%d,%d",&n,&m);
 }
 r->next=Null;
 head=head->next;
 return (head);
} /* creatpoly */

TERM *polyadd(TERM *ha,TERM *hb)
{
  TERM *hc,*p,*q,*s,*r;
  int x;
  p=ha;
  q=hb;
  hc=(TERM *)malloc(sizeof(TERM));
  s=hc;
  while((p!=Null)&&(q!=Null))
  {
   if(p->exp==q->exp) /*  coeficients */
    {
     x=p->coef+q->coef;
     if(x!=0)
     {
      r=(TERM *)malloc(sizeof(TERM));
      r->exp=p->exp;
      r->coef=x;
      s->next=r;
      s=r;
     }
     p=p->next;
     q=q->next;
   }
  else if(p->exp<q->exp)
       {
    r=(TERM *)malloc(sizeof(TERM));
    r->coef=q->coef;
    r->exp=q->exp;
    s->next=r;
    s=r;
    q=q->next;
       }
       else /* p->exp>q->exp */
       {
    r=(TERM *)malloc(sizeof(TERM));
    r->exp=p->exp;
    r->coef=p->coef;
    s->next=r;
    s=r;
    p=p->next;
       }
   }
  while(p!=Null)
  {
   r=(TERM *)malloc(sizeof(TERM));
   r->exp=p->exp;
   r->coef=p->coef;
   s->next=r;
   s=r;
   p=p->next;
  }
  while(q!=Null)
  {
   r=(TERM *)malloc(sizeof(TERM));
   r->exp=q->exp;
   r->coef=q->coef;
   s->next=r;
   s=r;
   q=q->next;
  }
 s->next=Null;
 r=hc;
 hc=hc->next;
 free(r);
 return (hc);
}  /*polyadd */

TERM *polymulti(TERM *f,TERM *g)  /* */
{
 TERM *fp,*gp,*hp,*q,*h;
 int maxp,p,r,x;

 maxp=f->exp+g->exp;
 h=(TERM *)malloc(sizeof(TERM));
 hp=h;
 g=reverse(g);
 for(r=maxp;r>=0;r--)
 {
  x=0;
  fp=f;
  gp=g;
  while((fp!=Null)&&(gp!=Null))
  {
   p=fp->exp+gp->exp;
   if(p>r) fp=fp->next;
   else if(p<r) gp=gp->next;
    else
    {
     x+=fp->coef*gp->coef;
     fp=fp->next;
     gp=gp->next;
    }
  } /*end of while */
  if(x!=0)
  {
   q=(TERM *)malloc(sizeof(TERM));
   q->exp=r;
   q->coef=x;
   q->next=Null;
   hp->next=q;
   hp=q;
  }
 }/* end of for */
 hp=h;
 h=h->next;
 free(hp);
 return (h);
}

TERM* reverse(TERM *q)
{
 TERM *p1,*p2;
 if(q!=Null)
  {
   p1=q->next;
   q->next=Null;
   while(p1!=Null)
   {
    p2=p1->next;
    p1->next=q;
    q=p1;
    p1=p2;
   }  /* end of while */
 }
 return (q);
}

void polyout(TERM *head)
{
 TERM *p,*q;
 p=head;
/* p=head->next; */
 while(p!=Null)
 {
  printf("%d,%d ",p->coef,p->exp);
  p=p->next;
 }
 printf("\n");
}

 void main()
 {
  TERM *ha,*hb,*hc,*p,*q,*h;
  printf("\nInput the 1st polynomial");
  ha=creatpoly();
  printf("\nInput the 2nd polynomial");
  hb=creatpoly();
  printf("\nthe 1st polynomial is:");
  polyout(ha);
  printf("\nthe 2nd polynomial is:");
  polyout(hb);
  hc=polyadd(ha,hb);
  printf("\nthe addition of the two polynomial is:");
  polyout(hc);
  h=polymulti(ha,hb);
  printf("\nthe multiplication of the two polynomial is:");
  polyout(h);
  return ;
 }

最佳回答:

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

#define Null  0
#define True  1
#define False 0


typedef struct polyterm
    {
     int coef;
     int exp;
     struct polyterm *next;
    } TERM;
TERM *reverse(TERM *q);
void polyout(TERM *head);

TERM *creatpoly() /**/
{
 TERM *head,*r,*s;
 int m,n;
 head=(TERM *)malloc(sizeof(TERM));
 printf("input coef and exp(1,2<CR>):\n");
 scanf("%d,%d",&n,&m);
 r=head;
 while(n)
 {
  s=(TERM *)malloc(sizeof(TERM));
  s->coef=n;
  s->exp=m;
  r->next=s;
  r=s;
  printf("\nInput coef and exp:\n");
  scanf("%d,%d",&n,&m);
 }
 r->next=Null;
 head=head->next;
 return (head);
} /* creatpoly */

TERM *polyadd(TERM *ha,TERM *hb)
{
  TERM *hc,*p,*q,*s,*r;
  int x;
  p=ha;
  q=hb;
  hc=(TERM *)malloc(sizeof(TERM));
  s=hc;
  while((p!=Null)&&(q!=Null))
  {
   if(p->exp==q->exp) /*  coeficients */
    {
     x=p->coef+q->coef;
     if(x!=0)
     {
      r=(TERM *)malloc(sizeof(TERM));
      r->exp=p->exp;
      r->coef=x;
      s->next=r;
      s=r;
     }
     p=p->next;
     q=q->next;
   }
  else if(p->exp<q->exp)
       {
    r=(TERM *)malloc(sizeof(TERM));
    r->coef=q->coef;
    r->exp=q->exp;
    s->next=r;
    s=r;
    q=q->next;
       }
       else /* p->exp>q->exp */
       {
    r=(TERM *)malloc(sizeof(TERM));
    r->exp=p->exp;
    r->coef=p->coef;
    s->next=r;
    s=r;
    p=p->next;
       }
   }
  while(p!=Null)
  {
   r=(TERM *)malloc(sizeof(TERM));
   r->exp=p->exp;
   r->coef=p->coef;
   s->next=r;
   s=r;
   p=p->next;
  }
  while(q!=Null)
  {
   r=(TERM *)malloc(sizeof(TERM));
   r->exp=q->exp;
   r->coef=q->coef;
   s->next=r;
   s=r;
   q=q->next;
  }
 s->next=Null;
 r=hc;
 hc=hc->next;
 free(r);
 return (hc);
}  /*polyadd */

TERM *polymulti(TERM *f,TERM *g)  /* */
{
 TERM *fp,*gp,*hp,*q,*h;
 int maxp,p,r,x;

 maxp=f->exp+g->exp;
 h=(TERM *)malloc(sizeof(TERM));
 hp=h;
 g=reverse(g);
 for(r=maxp;r>=0;r--)
 {
  x=0;
  fp=f;
  gp=g;
  while((fp!=Null)&&(gp!=Null))
  {
   p=fp->exp+gp->exp;
   if(p>r) fp=fp->next;
   else if(p<r) gp=gp->next;
    else
    {
     x+=fp->coef*gp->coef;
     fp=fp->next;
     gp=gp->next;
    }
  } /*end of while */
  if(x!=0)
  {
   q=(TERM *)malloc(sizeof(TERM));
   q->exp=r;
   q->coef=x;
   q->next=Null;
   hp->next=q;
   hp=q;
  }
 }/* end of for */
 hp=h;
 h=h->next;
 free(hp);
 return (h);
}

TERM* reverse(TERM *q)
{
 TERM *p1,*p2;
 if(q!=Null)
  {
   p1=q->next;
   q->next=Null;
   while(p1!=Null)
   {
    p2=p1->next;
    p1->next=q;
    q=p1;
    p1=p2;
   }  /* end of while */
 }
 return (q);
}

void polyout(TERM *head)
{
 TERM *p,*q;
 p=head;
/* p=head->next; */
 while(p!=Null)
 {
  printf("%d,%d ",p->coef,p->exp);
  p=p->next;
 }
 printf("\n");
}

 void main()
 {
  TERM *ha,*hb,*hc,*p,*q,*h;
  printf("\nInput the 1st polynomial");
  ha=creatpoly();
  printf("\nInput the 2nd polynomial");
  hb=creatpoly();
  printf("\nthe 1st polynomial is:");
  polyout(ha);
  printf("\nthe 2nd polynomial is:");
  polyout(hb);
  hc=polyadd(ha,hb);
  printf("\nthe addition of the two polynomial is:");
  polyout(hc);
  h=polymulti(ha,hb);
  printf("\nthe multiplication of the two polynomial is:");
  polyout(h);
  return ;
 }

 

 

追問:

   謝謝

可是這個結果還是不太對

回答:

你的加法和乘法的算法我沒有管,你那個算法對不對我就不知道了!

我只是添加了一個InsertTerm函數,和修改了你的createploy函數,方法是在創建多項式鏈表的時候就插入到正確的位置,讓它按降序排列,我調試過了,這部分沒有錯誤!

你的加法和乘法我沒有看,不知道對不對!

 

 

 

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

#define Null  0
#define True  1
#define False 0


typedef struct polyterm
{
 int coef;
 int exp;
 struct polyterm *next;
} TERM;
TERM *reverse(TERM *q);
void polyout(TERM *head);


TERM* InsertTerm(TERM *head,TERM *s)
{
 TERM *p=s;
 TERM *ph=head;
 if(p->exp>ph->exp)
 {
  p->next=ph;
  head=p;
  return head;
 }
 while(ph)
 {
  if(p->exp==ph->exp)
  {
   printf("cannot input same exp!");
   return Null;
  }
  if(p->exp<ph->exp && ph->next==Null)
  {
   ph->next=p;
   p->next=Null;
   return head;
  }
  else if(p->exp<ph->exp)
  {
   ph=ph->next;
  }
  else
  {
   p->next=ph->next;
   ph->next=p;
   return head;
  }
 }
 return head;
}


TERM *creatpoly() /**/
{
 TERM *head,*r,*s;
 int m,n;
 char ch;
 head=(TERM *)malloc(sizeof(TERM));
 printf("input coef and exp(1,2<CR>):\n");
 scanf("%d,%d",&n,&m);
 r=head;
 r->coef=n;
 r->exp=m;
 r->next=Null;
 printf("continue?(Enter \"Y\" continue else exit!)");
 getchar();
 scanf("%c",&ch);
 while(ch=='y' || ch=='Y')
 {
  s=(TERM *)malloc(sizeof(TERM));
  printf("\nInput coef and exp:\n");
  scanf("%d,%d",&n,&m);
  s->coef=n;
  s->exp=m;
  if((r=InsertTerm(head,s))==Null)
  {
   free(s);
   continue;
  }
  else
  {
   head=r;
  }
  printf("continue?(Enter \"Y\" continue else exit!)");
  getchar();
  scanf("%c",&ch);
 }

 return (head);
} /* creatpoly */


TERM *polyadd(TERM *ha,TERM *hb)
{
 TERM *hc,*p,*q,*s,*r;
 int x;
 p=ha;
 q=hb;
 hc=(TERM *)malloc(sizeof(TERM));
 s=hc;
 while((p!=Null)&&(q!=Null))
 {
  if(p->exp==q->exp) /*  coeficients */
  {
   x=p->coef+q->coef;
   if(x!=0)
   {
    r=(TERM *)malloc(sizeof(TERM));
    r->exp=p->exp;
    r->coef=x;
    s->next=r;
    s=r;
   }
   p=p->next;
   q=q->next;
  }
  else if(p->exp<q->exp)
  {
   r=(TERM *)malloc(sizeof(TERM));
   r->coef=q->coef;
   r->exp=q->exp;
   s->next=r;
   s=r;
   q=q->next;
  }
  else /* p->exp>q->exp */
  {
   r=(TERM *)malloc(sizeof(TERM));
   r->exp=p->exp;
   r->coef=p->coef;
   s->next=r;
   s=r;
   p=p->next;
  }
 }
 while(p!=Null)
 {
  r=(TERM *)malloc(sizeof(TERM));
  r->exp=p->exp;
  r->coef=p->coef;
  s->next=r;
  s=r;
  p=p->next;
 }
 while(q!=Null)
 {
  r=(TERM *)malloc(sizeof(TERM));
  r->exp=q->exp;
  r->coef=q->coef;
  s->next=r;
  s=r;
  q=q->next;
 }
 s->next=Null;
 r=hc;
 hc=hc->next;
 free(r);
 return (hc);
}  /*polyadd */

TERM *polymulti(TERM *f,TERM *g)  /* */
{
 TERM *fp,*gp,*hp,*q,*h;
 int maxp,p,r,x;
 
 maxp=f->exp+g->exp;
 h=(TERM *)malloc(sizeof(TERM));
 hp=h;
 g=reverse(g);
 for(r=maxp;r>=0;r--)
 {
  x=0;
  fp=f;
  gp=g;
  while((fp!=Null)&&(gp!=Null))
  {
   p=fp->exp+gp->exp;
   if(p>r) fp=fp->next;
   else if(p<r) gp=gp->next;
   else
   {
    x+=fp->coef*gp->coef;
    fp=fp->next;
    gp=gp->next;
   }
  } /*end of while */
  if(x!=0)
  {
   q=(TERM *)malloc(sizeof(TERM));
   q->exp=r;
   q->coef=x;
   q->next=Null;
   hp->next=q;
   hp=q;
  }
 }/* end of for */
 hp=h;
 h=h->next;
 free(hp);
 return (h);
}

TERM* reverse(TERM *q)
{
 TERM *p1,*p2;
 if(q!=Null)
 {
  p1=q->next;
  q->next=Null;
  while(p1!=Null)
  {
   p2=p1->next;
   p1->next=q;
   q=p1;
   p1=p2;
  }  /* end of while */
 }
 return (q);
}

void polyout(TERM *head)
{
 TERM *p,*q;
 p=head;
 /* p=head->next; */
 while(p!=Null)
 {
  printf("%d,%d ",p->coef,p->exp);
  p=p->next;
 }
 printf("\n");
}

void main()
{
 TERM *ha,*hb,*hc,*p,*q,*h;
 printf("\nInput the 1st polynomial\n");
 ha=creatpoly();
 printf("\nInput the 2nd polynomial\n");
 hb=creatpoly();
 printf("\nthe 1st polynomial is:");
 polyout(ha);
 printf("\nthe 2nd polynomial is:");
 polyout(hb);
 hc=polyadd(ha,hb);
 printf("\nthe addition of the two polynomial is:");
 polyout(hc);
 h=polymulti(ha,hb);
 printf("\nthe multiplication of the two polynomial is:");
 polyout(h);
 return ;
}

 

 

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved