| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 595 人关注过本帖, 1 人收藏
标题:多项式相加的问题
取消只看楼主 加入收藏
MCDULL
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2010-4-3
结帖率:0
收藏(1)
已结贴  问题点数:20 回复次数:1 
多项式相加的问题
程序运行之后显示内存不能read。麻烦看下哪里出错了。
#include<stdlib.h>
#include<stdio.h>
#include<ctype.h>
#include<malloc.h>
#include<iostream.h>

typedef struct pnode{  
    float coef;      
    int   exp;         
    struct pnode *next;
    struct pnode *prior;
}polynode;
    polynode *add(polynode *pa,polynode *pb)
    {
        polynode *p,*q,*r,*s,*t;
        float x;
        p=pa->next;q=pb->next;
        s=pa;
        while((p!=pa)&&(q!=pb))
        {
        if(p->exp<q->exp){s=p;p=p->next;}
        if(p->exp>q->exp){r=q->next;q->next=p;s->next=q;s=q;q=r;}
        else{
            x=p->coef+q->coef;
            if(x!=0){p->coef=x;s=p;}
            else{s->next=p->next;free(p);}
            p=s->next;r=q;q=q->next;free(r);
            }
        }
        if(q!=pb)
        {t=q;
        while(t->next!=pb)t=t->next;
            s->next=q;
            t->next=pa;
        }
        return(pa);
    }

polynode * CreatPolyn(polynode *P,int m)
{  
  if(m <= 0) return NULL;
  polynode *h = P = (pnode*)malloc(sizeof(pnode)), *Q;
  P->coef = 0.0;
  int i;
  printf("输入%d个非零项\n",m);
  for (i = 1; i <= m; ++i)
    {  
        scanf("%f%d",&P->coef,&P->exp);
        if(P->coef)
        Q = P;
        P = P->next = (polynode*)malloc(sizeof(polynode));
    }
  Q->next = NULL;
  free(P);
  return h;
} // CreatPolyn


   
   



void PrintfPoly(polynode *P)
{
  polynode *q = P;
  if(!q) {
   putchar('0');
      return;
  }
  if(q->coef!=1)
    {
     printf("%g",q->coef);
     if(q->exp==1) putchar('X');
     else if(q->exp) printf("X^%d",q->exp);
     }
  else if(!q->exp) putchar('1');
      else if(q->exp==1) putchar('X');
            else printf("X^%d",q->exp);
  q = q->next;
  while (q)
  {
     if(q->coef > 0) putchar('+');
     if(q->coef!=1)
     {
        printf("%g",q->coef);
        if(q->exp==1) putchar('X');
        else if(q->exp) printf("X^%d",q->exp);
        }
  

     else if(!q->exp) putchar('1');
          else if(q->exp==1) putchar('X');
                else printf("X^%d",q->exp);
     q = q->next;
  }
}




void main(){
     polynode *P1 = (polynode*)malloc(sizeof(polynode));
     polynode *P2 = (polynode*)malloc(sizeof(polynode));
     polynode * CreatPolyn(polynode *P,int m);
     polynode *polyadd(polynode *pa,polynode *pb);
     void PrintfPoly(polynode *P);
     P1=CreatPolyn(P1,5);
     P2=CreatPolyn(P2,5);
     P1=add(P1,P2);
     PrintfPoly(P1);


}
搜索更多相关主题的帖子: 相加 多项式 
2010-04-03 23:43
MCDULL
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2010-4-3
收藏
得分:0 
回复 2楼 hahayezhe
我对指针引用就是不熟悉。能不能帮我看下
2010-04-04 23:20
快速回复:多项式相加的问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.031049 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved