| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 595 人关注过本帖, 1 人收藏
标题:多项式相加的问题
只看楼主 加入收藏
MCDULL
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2010-4-3
结帖率:0
收藏(1)
已结贴  问题点数:20 回复次数:3 
多项式相加的问题
程序运行之后显示内存不能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
hahayezhe
Rank: 15Rank: 15Rank: 15Rank: 15Rank: 15
来 自:湖南张家界
等 级:贵宾
威 望:24
帖 子:1386
专家分:6999
注 册:2010-3-8
收藏
得分:10 
肯定是指针运用出问题了 细细的找 哦
2010-04-04 09:35
MCDULL
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2010-4-3
收藏
得分:0 
回复 2楼 hahayezhe
我对指针引用就是不熟悉。能不能帮我看下
2010-04-04 23:20
寒风中的细雨
Rank: 17Rank: 17Rank: 17Rank: 17Rank: 17
等 级:贵宾
威 望:66
帖 子:1710
专家分:8645
注 册:2009-9-15
收藏
得分:10 
#include<stdlib.h>
#include<stdio.h>
#include<ctype.h>
#include<malloc.h>
#include<iostream.h>

#define LEN   sizeof(struct Pnode)
//定义的是双向链表 但是并没有用到
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;
    int i = 1;
    float x;
    p = pa->next;
    r = p->next;
    q = pb->next;
    s = q->next;
    t = pa;
    //比较存在三种情况  相等 就在原来的基础上加
    //大于前 小于后 就插入中间
    //两头端点情况插入
    //把pb插入到pa当中

    while(q)
    {
        i = 1;
        while( p )
        {
            if( (p->exp>q->exp)&& (i==1) )//前端点插入
            {
                q->next = t->next;
                t->next = q;
                t = q;
                q = s;
                if(!s)//判断插入的结点是否已完
                    return pa;
                s = s->next;
            }
            ++i;
            if( (p->next==NULL) && (p->exp<q->ext) )//插入到最后
            {
                q->next = p->next;
                p->next = q;
                q = s;
                if(!s)//判断插入的结点是否已完
                    return pa;
                s = s->next;
            }
            if( i!=1 )
                if( (p->exp<q->exp) && (r->exp>q->exp) )//插入中间
                {
                    q->next = p->next;
                    p->next = q;
                    q = s;
                    if(!s)//判断插入的结点是否已完
                        return pa;
                    s = s->next;
                }
            if(p->exp == q->exp)
            {
                p->coef += q->coef;
                q = s;
                if(!s)//判断插入的结点是否已完
                    return pa;
                s = s->next;
            }
            p = p->next;
            r = p->next;
        }
        p = pa->next;
        r = p->next;
    }
}

    /*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 = (polynode) malloc (LEN), q;
  //P->coef = 0.0;
  //p->next = NULL;
  p->next = NULL;
  int i;
  printf("输入%d个非零项\n",m);
  for (i = 1; i <= m; ++i)
  {
      q = (polynode) malloc (LEN);
      scanf("%f%d",&P->coef,&P->exp);
      q->next = p->next;
      p->next = q;
      p = q;
  }
  free(P);
  return h;
} // CreatPolyn

//
void PrintfPoly( polynode P )
{
    polynode q = P;
    if(!q)//表示没有分配到空间 而不是0
        eixt(0);
    /*{
        putchar('0');
        return;
    }*/
    /*if(q->coef!=1)
    {
        printf("%f",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;
    }*/
    while( q )//幂的取值是从0开始后递增
    {
        if( p->coef != 1 )
            printf("%f", p->coef);//输出系数
        if( !q->exp )
            putchar('1');
        else if( q->exp==1 )
            putchar('X');
        else
            printf("X^%d",q->exp);
        if( q->next )//后面还有元素就要输出+ 没有就不需要
            printf(" + ");
        q = q->next;
    }
}

int main()
{
     polynode P1; //= (polynode) malloc (LEN);
     polynode P2; //= (polynode) malloc (LEN);
     //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);
     return 0;
}
只是写出了基本思路 编译 链接 就自己动手
如果输入的多项式 是乱序 自己就加个排序函数就行了
2010-04-05 11:46
快速回复:多项式相加的问题
数据加载中...
 
   



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

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