注册 登录
编程论坛 数据结构与算法

帮我看哪错了?多项式问题

jfckpep 发布于 2010-04-29 23:53, 1170 次点击
#include <stdio.h>
#include <stdlib.h>

struct Node
{
    float coef;
    int expn;
    struct Node *next;
}Link;

struct Node *Makenode(struct Node *head)
{
    float coef;
    int expn;
    struct Node *p=NULL;
    struct Node *L=head;
    p=(struct Node*)malloc(sizeof(Link));
    if(!p)
    {
        printf("分配内存失败!\n");
        exit(0);
    }
    if(head = NULL)
        head=p;
    else
    {
        while(L)
        {
            L=L->next;
        }
        p->next=L;
        L=p;
    }
    printf("请输入系数:");
    scanf("%f",&coef);
    //printf("\n");
    printf("请输入指数:");
    scanf("%d",&expn);
    printf("\n");
    p->coef=coef;
    p->expn=expn;
    return head;
}
void printpoly(struct Node *head)
{
    struct Node *p=head;
    p=p->next;
    while(p)
    {
//        p=p->next;
        printf("%fX^%d",p->coef,p->expn);
        p=p->next;
    }
}


main()
{
    int n,i;
    struct Node *head=NULL;
    printf("请输入项数:");
    scanf("%d",&n);
    printf("\n");
    for(i=1;i<=n;i++)
    {
        printf("输入第%d项的数据\n",i);
        head=Makenode(head);
    }
    printpoly(head);
}
13 回复
#2
寒风中的细雨2010-04-30 07:32
if(head == NULL)
        head=p;

void printpoly(struct Node *head)
{
    struct Node *p=head;
    p=p->next;  这句注释掉
    while(p)
#3
jfckpep2010-04-30 09:16
为什么这样改?,“=”不是表示空么,p=p->next 如果不去掉会有什么影响呢?
#4
xichong2010-04-30 09:26
if语句中采用的是逻辑表达式,==才是逻辑表达式,=是赋值表达式,两者是不一样的
#5
xichong2010-04-30 09:31
p=p->next 如果不去掉会有什么影响呢? 那就要看你创建的链表中头结点中是否有值了,如果有,则要去掉,循环第一次就要输出头结点中数据域中的值!
#6
jfckpep2010-04-30 10:46
#include <stdio.h>
#include <stdlib.h>

struct Node
{
    float coef;
    int expn;
    struct Node *next;
}Link;

struct Node *Makenode(struct Node *head)
{
    float coef;
    int expn;
    struct Node *p;
    struct Node *L=head;
    p=(struct Node*)malloc(sizeof(Link));
    if(!p)
    {
        printf("分配内存失败!\n");
        exit(0);
    }
    if(head == NULL)
        head=p;
    else
    {
        while(L->next!=NULL)
        {
            L=L->next;
        }
        L->next=p;
//        L=p;
    }
    L=p;
    printf("请输入系数:");
    scanf("%f",&coef);
    //printf("\n");
    printf("请输入指数:");
    scanf("%d",&expn);
    printf("\n");
    p->coef=coef;
    p->expn=expn;
    L->next = NULL;
    return head;
}
void printpoly(struct Node *head)
{
    float coef;
    int expn;
    struct Node *p=head;
//    p=p->next;
    printf("f(x)=");
    while(p)
    {
        printf("%.3fX^%d",p->coef,p->expn);
        if(p->next!=NULL)
        {
            if(p->next->coef>0)
                printf("+");
        }
        p=p->next;
    }
}
struct Node *fluxion(struct Node *headp,struct Node *headf)
{
    struct Node *p=headp;
    while(p->next!=NULL)
    {
        struct Node *f=headf;
        struct Node *temp;
        temp=(struct Node *)malloc(sizeof(Link));
        if(!temp)
        {
            printf("分配内存失败!\n");
            exit(0);
        }
        if(headf==NULL)
            headf=temp;
        else
        {
            while(f->next!=NULL)
            {
                f=f->next;
            }
            f->next=temp;
        }
        f=temp;
        temp->coef=(p->coef)*(p->expn);
        temp->expn=p->expn-1;
        f->next=NULL;
        p=p->next;
    }
    return headf;
}
main()
{
    int n,i;
    struct Node *head=NULL;
    struct Node *headf=NULL;
    printf("请输入项数:");
    scanf("%d",&n);
    printf("\n");
    for(i=1;i<=n;i++)
    {
        printf("输入第%d项的数据\n",i);
        head=Makenode(head);
    }
    printpoly(head);
    printf("\n");

    fluxion(headl);
    printf("\n");
   
}
为什么调用了fluxion函数,却没结果输出?
#7
寒风中的细雨2010-04-30 16:48
struct Node *fluxion(struct Node *headp,struct Node *headf)

fluxion(headl);

难道编译器没有报错?
#8
寒风中的细雨2010-04-30 16:50
struct Node *head=NULL;
struct Node *headf=NULL;
headl 都没有定义
#9
jfckpep2010-04-30 18:49
啊,不好意思,发贴时弄错了,原来是这样的:
fluxion(head,headf);
printpoly(headf);
#10
寒风中的细雨2010-04-30 19:11
不明白的地方自己一定要去好好地思考一番
 实在搞不懂 在去请教  这样自己才能更加有体会 明白的更透彻

不然 自己只会惯性提问 碰到不清楚的时候
#11
寒风中的细雨2010-04-30 19:12
个人一点意见
#12
寒风中的细雨2010-04-30 19:12
建议
#13
jfckpep2010-04-30 20:37
无论是意见还是建议,我都接受,对学习不感兴趣,让自己都没了去思考的动力,所以学习也不好,真的谢谢了,我会记得你的意见和建议的。
#14
2010-05-03 12:54
#include <stdio.h>
#include <stdlib.h>

struct Node
{
    float coef;
    int expn;
    struct Node *next;
}Link;

struct Node *Makenode(struct Node *head)
{
    float coef;
    int expn;
    struct Node *p=NULL;
    struct Node *L=head;
    struct Node *q=head;
    p=(struct Node*)malloc(sizeof(Link));
    if(!p)
    {
        printf("分配内存失败!\n");
        exit(0);
    }
    p->next=NULL;
    if(head == NULL)
    head=p;
    else
    {
        while(L)
        {
            q=L;
            L=L->next;
        }
        p->next=L;
        q->next=p;
    }
    printf("请输入系数:");
    scanf("%f",&coef);
    //printf("\n");
    printf("请输入指数:");
    scanf("%d",&expn);
    printf("\n");
    p->coef=coef;
    p->expn=expn;
    return head;
}
void printpoly(struct Node *head)
{
    struct Node *p=head;
    p=p->next;
    while(p)
    {
//        p=p->next;
        printf("%fX^%d",p->coef,p->expn);
        p=p->next;
    }
}


int main()
{
    int n,i;
    struct Node *head=NULL;
    printf("请输入项数:");
    scanf("%d",&n);
    printf("\n");
    for(i=1;i<=n;i++)
    {
        printf("输入第%d项的数据\n",i);
        head=Makenode(head);
    }
    printpoly(head);
    return 0;
}
1