当前位置: 代码迷 >> C语言 >> 多项式加法程序:
  详细解决方案

多项式加法程序:

热度:578   发布时间:2008-05-31 11:49:17.0
多项式加法程序:
#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;
}

编译链接都没有问题,运行时出现内存错误!
搜索更多相关的解决方案: 多项式  加法  

----------------解决方案--------------------------------------------------------
初步猜测可能是没初始化next域.....释放时出现问题..
----------------解决方案--------------------------------------------------------
搞这么多指针干嘛呢???

[color=white]
----------------解决方案--------------------------------------------------------
感觉是少了free函数,不过加在什么地方比较复杂
多项式用链表结构表示是比较合适的!我有一点感觉是用了Malloc函数,肯定要用free函数.另外ptr3=(NODE*)malloc(sizeof(NODE))这句中是后来加上强制类型转换编译才能通过.
----------------解决方案--------------------------------------------------------
这次编译通过了,不过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;
}

前面的兄弟可以试着用数组实现多项式加,不过数组大小不能动态变化!
----------------解决方案--------------------------------------------------------
[code]
/*****************************************************************
** HighlightCodeV3.0 software by yzfy(雨中飞燕) http://yzfy.org **
*****************************************************************/
/*****************************************************************
** HighlightCodeV3.0 software by yzfy(雨中飞燕) http://yzfy.org **
*****************************************************************/
#include <iostream>
#include <list>
#include <cstdlib>
using namespace std;
class Node
{
   
public:
        int exp;       //指数
        
int coef;      //系数
   
Node():exp(0),coef(0){}
}
;

class Polynomial
{
   
   
private:
        list<Node>first_poly;        //第一个多项式
        
list<Node>second_poly;       //第二个多项式
        
list<Node>result_poly;       //储存结果
        
list<Node> MpAdd(list<Node>& t_first_poly,\
                                    
list<Node>& t_second_poly);
    public:
        void MultipPoly();           //多项式相乘
        
void AddPoly();              //多项式相加
        
void InPut();                //输入多项式
        
void OutPut();               //输出多项式
};

void Polynomial::InPut()
{
   
Node temp;
    int n;
    cout<<"\t***********多项式的加法和乘法***********"<<endl;
    cout<<"请输入第一个多项式的项数"<<endl;
    cin>>n;
    cout<<"请按照降幂的顺序输入指数"<<endl;
    for(int i=1;i<=n;i++)
    {
        
cout<<"输入第"<<i<<"项的系数和指数"<<endl;
        cin>>temp.coef>>temp.exp;
        first_poly.push_back(temp);
    }
   
n=0;
    cout<<"请输入第二个多项式的项数"<<endl;
    cin>>n;
    cout<<"请按照降幂的顺序输入指数"<<endl;
    for(int i=1;i<=n;i++)
    {
        
cout<<"输入第"<<i<<"项的系数和指数"<<endl;
        cin>>temp.coef>>temp.exp;
        second_poly.push_back(temp);
    }
}  

void Polynomial::OutPut()
{
   
list<Node>::iterator iter=result_poly.begin();
    for(;iter!=result_poly.end();)
    {
        
Node temp=*iter;
        cout<<temp.coef<<"x^"<<temp.exp;
        if(++iter!=result_poly.end())
        cout<<"+";
    }
}

void Polynomial::AddPoly()
{
   
list<Node>::iterator fiter=first_poly.begin();
    list<Node>::iterator siter=second_poly.begin();
    while(fiter!=first_poly.end()&&siter!=second_poly.end())
    {
            
Node temp;
            Node ftemp=(Node)*fiter;
            Node stemp=(Node)*siter;
            if(ftemp.exp>stemp.exp)
            {
               
result_poly.push_back(ftemp);
                fiter++;      
            }
                    
else if(ftemp.exp<stemp.exp)
                    {
                        
result_poly.push_back(stemp);
                        siter++;
                    }
                    
else {    temp.coef=ftemp.coef+stemp.coef;
                                temp.exp=ftemp.exp+stemp.exp;
                                result_poly.push_back(temp);
                                fiter++;
                                siter++;
                            }
        }
   
for(;fiter!=first_poly.end();fiter++)
    {
            
result_poly.push_back(*fiter);
        }
   
for(;siter!=second_poly.end();siter++)
    {
            
result_poly.push_back(*siter);
        }
}
   
void Polynomial::MultipPoly()
{
   
list<Node>::iterator fiter=first_poly.begin();
    list<Node>temp_result_poly;
    for(;fiter!=first_poly.end();fiter++)
    {
            
list<Node>stemp_result_poly;
            list<Node>::iterator siter=second_poly.begin();
            for(;siter!=second_poly.end();siter++)
            {
               
Node temp;
                Node ftemp=(Node)*fiter;
                Node stemp=(Node)*siter;
                temp.coef=ftemp.coef*stemp.coef;
                temp.exp=ftemp.exp+stemp.exp;
                stemp_result_poly.push_back(temp);
            }
            
temp_result_poly=MpAdd(stemp_result_poly,temp_result_poly);
            
    }
   
result_poly=temp_result_poly;                           
}   

list<Node> Polynomial::MpAdd(list<Node>& t_first_poly,\
                           
list<Node>& t_second_poly)
{
   
list<Node>::iterator fiter=t_first_poly.begin();
    list<Node>::iterator siter=t_second_poly.begin();
    list<Node>temp_result_poly;
    while(fiter!=t_first_poly.end()&&siter!=t_second_poly.end())
    {
            
Node temp;
            Node ftemp=(Node)*fiter;
            Node stemp=(Node)*siter;
            if(ftemp.exp>stemp.exp)
            {
               
temp_result_poly.push_back(ftemp);
                fiter++;      
            }
                    
else if(ftemp.exp<stemp.exp)
                    {
                        
temp_result_poly.push_back(stemp);
                        siter++;
                    }
                    
else {    temp.coef=ftemp.coef+stemp.coef;
                                temp.exp=ftemp.exp+stemp.exp;
                                temp_result_poly.push_back(temp);
                                fiter++;
                                siter++;
                            }
        }
   
for(;fiter!=t_first_poly.end();fiter++)
    {
            
temp_result_poly.push_back(*fiter);
        }
   
for(;siter!=t_second_poly.end();siter++)
    {
            
temp_result_poly.push_back(*fiter);
        }
        
return temp_result_poly;
}
   
                    
int main()
{   
   
Polynomial poly_a;
    poly_a.InPut();
    poly_a.AddPoly();
    cout<<"多项式加法的运算结果:"<<endl;
    poly_a.OutPut();
    cout<<endl;
    poly_a.MultipPoly();
    cout<<"多项式乘法的运算结果:"<<endl;
    poly_a.OutPut();
    system("pause");
    return 0;
}

[code]

[[it] 本帖最后由 菜鸟选手 于 2008-5-31 18:16 编辑 [/it]]
----------------解决方案--------------------------------------------------------
楼上,你还加code标签干嘛?你知道code标签的作用是什么吗??

[color=white]
----------------解决方案--------------------------------------------------------
楼上的楼上你可以用
istream_iterator<T> in; 对象的超出末端迭代器做。。这样就不用考虑是多少项的限制了。。。

[[it] 本帖最后由 sunkaidong 于 2008-5-31 18:21 编辑 [/it]]
----------------解决方案--------------------------------------------------------
-,- 第一次用那个工具贴码
贴了N次 终于..~
----------------解决方案--------------------------------------------------------
  相关解决方案