| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 5477 人关注过本帖
标题:多项式加法程序:
只看楼主 加入收藏
madzhou
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2007-4-9
收藏
 问题点数:0 回复次数:10 
多项式加法程序:
#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
中学者
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:20
帖 子:3554
专家分:80
注 册:2007-9-14
收藏
得分:0 
初步猜测可能是没初始化next域.....释放时出现问题..

樱花大战,  有爱.
2008-05-31 11:53
Loli
Rank: 1
来 自:飞燕算法群46520219
等 级:新手上路
帖 子:348
专家分:0
注 册:2008-5-27
收藏
得分:0 
搞这么多指针干嘛呢???

" border="0" />[color=white]
2008-05-31 11:58
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
菜鸟选手
Rank: 1
等 级:新手上路
帖 子:132
专家分:0
注 册:2008-5-5
收藏
得分:0 
[code]
/*****************************************************************
** HighlightCodeV3.0 software by yzfy(雨中飞燕) http:// **
*****************************************************************/
/*****************************************************************
** HighlightCodeV3.0 software by yzfy(雨中飞燕) http:// **
*****************************************************************/
#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]]

算法学习群57909089
2008-05-31 18:14
Loli
Rank: 1
来 自:飞燕算法群46520219
等 级:新手上路
帖 子:348
专家分:0
注 册:2008-5-27
收藏
得分:0 
楼上,你还加code标签干嘛?你知道code标签的作用是什么吗??

" border="0" />[color=white]
2008-05-31 18:17
sunkaidong
Rank: 4
来 自:南京师范大学
等 级:贵宾
威 望:12
帖 子:4496
专家分:141
注 册:2006-12-28
收藏
得分:0 
楼上的楼上你可以用
istream_iterator<T> in; 对象的超出末端迭代器做。。这样就不用考虑是多少项的限制了。。。

[[it] 本帖最后由 sunkaidong 于 2008-5-31 18:21 编辑 [/it]]

学习需要安静。。海盗要重新来过。。
2008-05-31 18:20
菜鸟选手
Rank: 1
等 级:新手上路
帖 子:132
专家分:0
注 册:2008-5-5
收藏
得分:0 
-,- 第一次用那个工具贴码
贴了N次 终于..~

算法学习群57909089
2008-05-31 19:28
dongyangming
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2015-7-8
收藏
得分:0 
大神,能把多项式减法,乘法的编程发出来吗
2015-07-08 12:40
快速回复:多项式加法程序:
数据加载中...
 
   



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

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