| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 591 人关注过本帖
标题:一元多项式的加法。。给点意见哈。
只看楼主 加入收藏
shenlanlan
Rank: 1
等 级:新手上路
帖 子:14
专家分:0
注 册:2011-8-25
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:3 
一元多项式的加法。。给点意见哈。
#include<stdio.h>
#include<stdlib.h>
typedef struct pnode{
    float coef;//系数
    int exp;//指数
    struct pnode *next;
}pnode,*ploype;


void creatplo(ploype h,int n){
    ploype p,q;
    int i;
    h=(ploype)malloc(sizeof(pnode));
    h->next=NULL;
    p=h;

    printf("input the element's coef and exp:");
    for(i=1;i<=n;i++){
        q=(ploype)malloc(sizeof(pnode));
        scanf("%f  %d",&q->coef,&q->exp);
        q->next=NULL;
        p->next=q;
        p=q;
    }
}

int comp(int a,int b){
    if(a<b)  return -1;
    if(a=b)  return 0;
    if(a>b)  return 1;
}

void add(ploype pa,ploype pb,ploype pc){//?????
    ploype p,q,pre,s;
    int w;
    float sum;
    p=pa;
    q=pb;
    pre=pa;  pc=pa;
    while(p&&q){
         w=comp(p->exp,q->exp);
        switch(w){
        case -1:p=p->next;   break;
        case 0: sum=p->coef+q->coef;
                if(sum!=0){
                    p->coef=sum;
                    pre=p;
                }
                else{
                    pre->next=p->next;
                    free(q);
                }
                p=pre->next;
                s=q;q=q->next;
                free(s);
                break;
        case 1:s=q->next;q->next=p;
              pre->next=q;
              pre=q;
              q=s;   break;
        }
    }
    if(pb) pre->next=q;
    free(pb);
}

int main(){
    ploype pa,pb,pc,p;
    int n1,n2;
    printf("input the lenght of pa,n1=");
    scanf("%d",&n1);
    printf("input the lenght of pb,n2=");
    scanf("%d",&n2);
    creatplo(pa,n1);
    creatplo(pb,n2);
    printf("caulate the result:");
    add(pa,pb,pc);
    p=pc->next;
    printf("pc=pa+pb");
    while(p!=NULL){
        printf("%f,%d",p->coef,p->exp);
        p=p->next;
    }
    return 0;
}
//最核心的add算法里,就是无法运算,对照了一下其他的代码,就是找不出错误,希望大家帮忙看看。。。谢谢了。
搜索更多相关主题的帖子: 系数 指数 element include 多项式 
2011-12-19 20:47
xie2010
Rank: 2
来 自:广东
等 级:论坛游民
帖 子:5
专家分:10
注 册:2010-10-27
收藏
得分:10 
数据结构能看懂就好了~
2011-12-19 22:03
silent_world
Rank: 9Rank: 9Rank: 9
等 级:蜘蛛侠
威 望:1
帖 子:258
专家分:1138
注 册:2011-9-24
收藏
得分:10 
算法的思维方式完全正确;
有些小细节已经改正,如下:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

typedef struct pnode{
     float coef;//系数
     int exp;//指数
     struct pnode *next;
}pnode,*ploype;


int creatplo(pnode **h,int n)
{
     ploype p = 0, q = 0, tmp = 0;
     int i;

     tmp=(ploype)malloc(sizeof(pnode));
     if(0 == tmp)
     {
          *h = 0;
          return -1;
     }
     memset(tmp, 0, sizeof(pnode));
     tmp->next=NULL;
     p=tmp;
     *h = tmp;
     
     printf("input the element's coef and exp:");
     for(i=1;i<=n;i++)
     {
          q = (ploype)malloc(sizeof(pnode));
          scanf("%f %d",&q->coef,&q->exp);
          q->next=NULL;
          p->next=q;
          p=q;
     }

     return 0;
}

int comp(int a,int b){
     if(a<b)  return -1;
     if(a=b)  return 0;
     if(a>b)  return 1;
}

void add(ploype pa,ploype pb,ploype *pc){//?????
     ploype p,q,pre,s;
     int w;
     float sum;
     
     pre=pa;  *pc=pa;
     p=pa->next;
     q=pb->next;
     while(p&&q){
          w=comp(p->exp,q->exp);
          switch(w){
          case -1:p=p->next;   break;
          case 0: sum=p->coef+q->coef;
               if(sum!=0){
                    p->coef=sum;
                    pre=p;
               }
               else{
                    pre->next=p->next;
                    free(p);
               }
               p=pre->next;
               s=q;
               q=q->next;
               free(s);
               break;
          case 1:
               s=q->next;
               q->next=p;
               pre->next=q;
               pre=q;
               q=s;   
               break;
          }
     }
     if(pb) pre->next=q;
     free(pb);
}

int main(){
     ploype pa,pb,pc,p;
     int n1,n2;
     printf("input the lenght of pa,n1=");
     scanf("%d",&n1);
     printf("input the lenght of pb,n2=");
     scanf("%d",&n2);

     creatplo(&pa,n1);
     creatplo(&pb,n2);
     printf("caulate the result:");
     add(pa,pb,&pc);
     p=pc->next;
     printf("pc=pa+pb");
     while(p!=NULL){
          printf("%f,%d",p->coef,p->exp);
          p=p->next;
     }
     return 0;
}
2011-12-20 17:40
shenlanlan
Rank: 1
等 级:新手上路
帖 子:14
专家分:0
注 册:2011-8-25
收藏
得分:0 
回复 3楼 silent_world
恩,看懂了,谢谢你啦。。。。。。
2011-12-22 13:58
快速回复:一元多项式的加法。。给点意见哈。
数据加载中...
 
   



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

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