| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1029 人关注过本帖
标题:多项式求导
只看楼主 加入收藏
hs2009
Rank: 2
等 级:论坛游民
帖 子:10
专家分:19
注 册:2010-4-18
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:2 
多项式求导
编译没问题,但运行不了。。。。
#include<stdio.h>
typedef struct node
{
    float coef;
    int exp;
    struct node*next;}Pnode;
Pnode*createht(int tag)
{
    float x;int y;
    Pnode*p,*r,*h=(Pnode*)malloc(sizeof(Pnode));
    r=h;
    printf("input x,y:");
    scanf("%f%d",&x,&y);
    while(y!=tag)
    {
        p=(Pnode*)malloc(sizeof(Pnode));
        p->coef=x;
        p->exp=y;
        r->next=p;
        r=p;
        scanf("%f%d",&x,&y);
    }
    r->next =NULL;
    return h;
}
void poly(Pnode*h)
{
    Pnode*pre,*p,*q;
    pre=h;
    p=h->next;
    while(p)
    {
        if(p->exp==0)
        {
            pre->next =NULL;
            free(p);
            break;
        }
        else
        {
            p->coef =p->coef*p->exp ;
            p->exp --;
            pre=p->next;
        }
    }
}
main()
{
    int tag=-1;Pnode*h,*p;
    h=createht(tag);
    poly(h);
    p=h->next;
    while(p)
    {
        printf("%f%d\n",p->coef,p->exp);
        p=p->next ;
    }
}


搜索更多相关主题的帖子: 多项式 
2010-04-18 23:34
ldg628
Rank: 12Rank: 12Rank: 12
等 级:火箭侠
威 望:3
帖 子:526
专家分:3036
注 册:2009-6-23
收藏
得分:20 
void poly(Pnode*h)
{
    Pnode*pre,*p,*q;
    pre=h;
    p=h->next;
    while(p)
    {
        if(p->exp==0)
        {
            pre->next = p->next;
            free(p);
            p = pre->next;  //不知道你的exp是不是降序排列的,如果不是,要改成这样,或者如果要保持项对应关系的话,就不free,使coef也为0,相应代码要改成p->coef = 0; pre = p; p = p->next;
        }
        else
        {                                                                                                         
            p->coef =p->coef*p->exp ;                                                                             
            p->exp --;                                                                                            
            pre=p;                                                                                                
            p = p->next;                                                                                         
        }                                                                                                         
    }                                                                                                            
}   
主要问题是指针没有处理好。。。
2010-04-19 09:48
hs2009
Rank: 2
等 级:论坛游民
帖 子:10
专家分:19
注 册:2010-4-18
收藏
得分:0 
谢了,if语句不用改,只改else语句就行啦,降序和升序是一样的 ,都能实现....
2010-04-19 12:35
快速回复:多项式求导
数据加载中...
 
   



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

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