标题:多项式加法程序:
取消只看楼主
madzhou
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2007-4-9
 问题点数:0 回复次数:2 
多项式加法程序:
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<math.h>

typedef struct polynode
{
    float coef;
    int exp;
    struct polynode *next;
}NODE;

void printpoly(NODE*);
NODE *createpoly(int);
NODE *polyadd(NODE*,NODE*);

int main(void)
{
    NODE *poly1,*poly2,*poly3;
    int keyin=0;
    printf("\n ===多项式相加===\n");
    printf("\n 1. 第一个多项式poly1(x)共有多少项:");
    scanf("%d",&keyin);
    printf("\n");
    poly1=createpoly(keyin);
    printf("\n");
    printf("\n 2. 第二个多项式poly2(x)共有多少项: ");
        scanf("%d",&keyin);
    poly2=createpoly(keyin);
    poly3=polyadd(poly1,poly2);
    printf("\n 1. poly1(x)=");
    printpoly(poly1);
    printf("\n 2. poly2(x)=");
    printpoly(poly2);
    printf("\n 3. poly3(x)=poly1(x)+poly2(x)");
    printf("\n poly3(x)=");
    printpoly(poly3);
    printf("\n");
    return 0;

}


void printpoly(NODE *ptr)
{
    char opr;
    while (ptr!=NULL)
    {
       if(ptr->coef>0)
      {
         opr='+';
      }

    
    else
    {
        opr='-';
    }
    printf("%c%4.1fx^%d",opr,fabs(ptr->coef),ptr->exp);
    ptr=ptr->next;
    
    }
}

NODE *createpoly(int n)
{
    float coef=0;
    int exp=0;
    int i=0;
    NODE *head,*tail,*ptr;
    
    head=tail=NULL;
    do{
        i++;
        ptr=(NODE*)malloc(sizeof(NODE));
        printf("  输入第(%d)项的系数与指数(以空白隔开):",i);
        scanf("%f %d",&coef,&exp);
        ptr->coef =coef;
        ptr->exp=exp;
        ptr->next=NULL;
        if(head==NULL)
            head=ptr;
        else
            tail->next=ptr;
        tail=ptr;

    }while(n>i);
    return head;
}

NODE* polyadd(NODE *poly1,NODE *poly2)
{
    NODE * tail,*head,*ptr1,*ptr2,*ptr3;
    ptr1=poly1;
    ptr2=poly2;
    head=tail=NULL;
    while(ptr1!=NULL && ptr2!=NULL)
    {
      ptr3=(NODE*)malloc(sizeof(NODE));
      if(ptr1->exp>ptr2->exp)
      {
          ptr3->coef=ptr1->coef;
          ptr3->exp=ptr1->exp;
          ptr1=ptr1->next;
      }
      else if(ptr1->exp<ptr2->exp)
      {
          ptr3->coef=ptr2->coef;
          ptr3->exp=ptr2->exp;
          ptr2=ptr2->next;
      }
      else
      {
           ptr3->coef=ptr1->coef+ptr2->coef;
          ptr3->exp=ptr1->exp;
          ptr1=ptr1->next;
          ptr2=ptr2->next;
      }
      ptr3->next=NULL;
      if(head==NULL)
          head=ptr3;
      else
          tail->next=ptr3;

    }
    if(ptr1==NULL)
    {
        while(ptr2!=NULL)
        {
            ptr3=(NODE*)malloc(sizeof(NODE));
            ptr3->coef=ptr2->coef;
          ptr3->exp=ptr2->exp;
          ptr2=ptr2->next;
          tail->next=ptr3;
          tail=ptr3;
        }
    }
    else
    {
        while(ptr1!=NULL)
        {
            ptr3=(NODE*)malloc(sizeof(NODE));
            ptr3->coef=ptr1->coef;
          ptr3->exp=ptr1->exp;
          ptr1=ptr1->next;
          tail->next=ptr3;
          tail=ptr3;
        }

    }
    return head;
}

编译链接都没有问题,运行时出现内存错误!
搜索更多相关主题的帖子: 多项式 加法 
2008-05-31 11:49
madzhou
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2007-4-9
得分:0 
感觉是少了free函数,不过加在什么地方比较复杂
多项式用链表结构表示是比较合适的!我有一点感觉是用了Malloc函数,肯定要用free函数.另外ptr3=(NODE*)malloc(sizeof(NODE))这句中是后来加上强制类型转换编译才能通过.
2008-05-31 17:27
madzhou
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2007-4-9
得分:0 
这次编译通过了,不过free的问题还请大侠指教!
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<math.h>

typedef struct polynode
{
    float coef;
    int exp;
    struct polynode *next;
}NODE;

void printpoly(NODE*);
NODE *createpoly(int);
NODE *polyadd(NODE*,NODE*);

int main(void)
{
    NODE *poly1,*poly2,*poly3;
    int keyin=0;
    printf("\n ===多项式相加===\n");
    printf("\n 1. 第一个多项式poly1(x)共有多少项:");
    scanf("%d",&keyin);
    printf("\n");
    poly1=createpoly(keyin);
    printf("\n");
    printf("\n 2. 第二个多项式poly2(x)共有多少项: ");
        scanf("%d",&keyin);
    poly2=createpoly(keyin);
    poly3=polyadd(poly1,poly2);
    printf("\n 1. poly1(x)=");
    printpoly(poly1);
    printf("\n 2. poly2(x)=");
    printpoly(poly2);
    printf("\n 3. poly3(x)=poly1(x)+poly2(x)");
    printf("\n poly3(x)=");
    printpoly(poly3);
    printf("\n");
    return 0;

}


void printpoly(NODE *ptr)
{
    char opr;
    while (ptr!=NULL)
    {
       if(ptr->coef>0)
      {
         opr='+';
      }

    
    else
    {
        opr='-';
    }
    printf("%c%4.1fx^%d",opr,fabs(ptr->coef),ptr->exp);
    ptr=ptr->next;
    
    }
}

NODE *createpoly(int n)
{
    float coef=0;
    int exp=0;
    int i=0;
    NODE *head,*tail,*ptr;
    
    head=tail=NULL;
    do{
        i++;
        ptr=(NODE*)malloc(sizeof(NODE));
        printf("  输入第(%d)项的系数与指数(以空白隔开):",i);
        scanf("%f %d",&coef,&exp);
        ptr->coef =coef;
        ptr->exp=exp;
        ptr->next=NULL;
        if(head==NULL)
            head=ptr;
        else
            tail->next=ptr;
        tail=ptr;

    }while(n>i);
    return head;
}

NODE* polyadd(NODE *poly1,NODE *poly2)
{
    NODE * tail,*head,*ptr1,*ptr2,*ptr3;
    ptr1=poly1;
    ptr2=poly2;
    head=tail=NULL;
    while(ptr1!=NULL && ptr2!=NULL)
    {
      ptr3=(NODE*)malloc(sizeof(NODE));
      if(ptr1->exp>ptr2->exp)
      {
          ptr3->coef=ptr1->coef;
          ptr3->exp=ptr1->exp;
          ptr1=ptr1->next;
      }
      else if(ptr1->exp<ptr2->exp)
      {
          ptr3->coef=ptr2->coef;
          ptr3->exp=ptr2->exp;
          ptr2=ptr2->next;
      }
      else
      {
           ptr3->coef=ptr1->coef+ptr2->coef;
          ptr3->exp=ptr1->exp;//ptr3->exp=ptr2->exp;
          ptr1=ptr1->next;
          ptr2=ptr2->next;
      }
      ptr3->next=NULL;
      if(head==NULL)
          head=ptr3;
      else
          tail->next=ptr3;
      tail=ptr3;
      

    }
    if(ptr1==NULL)
    {
        while(ptr2!=NULL)
        {
            ptr3=(NODE*)malloc(sizeof(NODE));
            ptr3->coef=ptr2->coef;
          ptr3->exp=ptr2->exp;
          ptr2=ptr2->next;
          tail->next=ptr3;
          tail=ptr3;
        }
    }
    else
    {
        while(ptr1!=NULL)
        {
            ptr3=(NODE*)malloc(sizeof(NODE));
            ptr3->coef=ptr1->coef;
          ptr3->exp=ptr1->exp;
          ptr1=ptr1->next;
          tail->next=ptr3;
          tail=ptr3;
        }

    }
    return head;
}

前面的兄弟可以试着用数组实现多项式加,不过数组大小不能动态变化!
2008-05-31 18:08



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-216836-1-1.html




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

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