| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2130 人关注过本帖
标题:多项式加法怎么就错了
只看楼主 加入收藏
令狐少侠56
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:320
专家分:175
注 册:2014-4-10
结帖率:58.18%
收藏
 问题点数:0 回复次数:2 
多项式加法怎么就错了
原题,加法就出错了乘法还没写。因为加法函数注释掉才能像题目一样输入数据,否则输入N后得按enter,所以我就先将它注释掉了,
逻辑错了么?
我的方法:Poly1中的第k项与Poly2中的项依次扫描,匹配到求和,没匹配到直接将poly1的k项保存。

图片附件: 游客没有浏览图片的权限,请 登录注册

程序代码:
#include <stdio.h>
#include <stdlib.h>

typedef struct PolyNode *PtrPolyNode;
struct PolyNode //结构体类型声明忘记写分号
{
    int coef;
    int expon;
    struct PolyNode *next;
};


PtrPolyNode  CreatePolyList(int N)
{
    PtrPolyNode head;
    PtrPolyNode previous,current;

    int i;

    head=NULL;
    
    for( i=0 ; i<N ; i++)
    {
        current = ( PtrPolyNode )malloc( sizeof( struct PolyNode) ) ;
        current->next = NULL ;

        scanf("%d" , &(current->coef) ) ;   //忘记给系数和指数赋值
        scanf("%d" , &(current->expon) ) ;

        if(head==NULL)
            head = current ;
        else
            previous->next = current ;

            previous = current ;
    }
        return head;
}


/*PtrPolyNode Addition( PtrPolyNode Poly1 , PtrPolyNode Poly2 )
{
    PtrPolyNode head;
    PtrPolyNode current,previous;
    

    head=NULL;

    while( Poly1!=NULL )        
    {
        if( Poly1->expon==Poly2->expon && Poly2!=NULL ) //指数相同时系数相加,保存
        {
            current = ( PtrPolyNode ) malloc( sizeof(struct PolyNode) );

            current->next = NULL ;
            current->coef = Poly1->coef + Poly2->coef;
            current->expon = Poly1->expon;

            if(head=NULL)
                head=current;
            else
                previous->next=current;

            previous=current;

            Poly1=Poly1->next;
            Poly2=Poly2->next;/*由于指数按递减排序,故不需要将Poly1第k项与Poly2所有项比较,
                              Poly2高阶项匹配到以后直接从它的下一项开始扫描*/
/*        }

        if( Poly1->expon != Poly2->expon && Poly2!=NULL )
        {
            Poly2=Poly2->next;    //与Poly2下一项比较
        }

        if( Poly2==NULL )        //Poly2中没有项能与Poly1第K项匹配,将其保存后进入k+1项的匹配
        {
            current = ( PtrPolyNode ) malloc( sizeof(struct PolyNode) );

            current->next = NULL ;
            current->coef = Poly1->coef;
            current->expon = Poly1->expon;

            if(head=NULL)
                head=current;
            else
                previous->next=current;

            previous=current;

            Poly1=Poly1->next;
        }
    }
    
    return head;
}
*/

void OutPut(PtrPolyNode head)
{

    while(head!=NULL)
    {
        printf("%d ",head->coef);
        printf("%d ",head->expon);
    
        head=head->next;
    }
    printf("\n");
}


int main()
{
    int N1,N2;        //非0项个数
    PtrPolyNode Poly1,Poly2,Poly3;

    scanf("%d",&N1);
    Poly1 = CreatePolyList( N1 ) ;

    scanf("%d",&N2);
    Poly2 = CreatePolyList( N2 ) ;

    OutPut( Poly1 );
    OutPut( Poly2 );

    //Poly3 = Addition( Poly1 , Poly2 );
    //    OutPut( Poly3 );
}
搜索更多相关主题的帖子: 多项式 
2015-10-26 10:27
令狐少侠56
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:320
专家分:175
注 册:2014-4-10
收藏
得分:0 
没人么

[此贴子已经被作者于2015-10-26 12:15编辑过]

2015-10-26 10:34
令狐少侠56
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:320
专家分:175
注 册:2014-4-10
收藏
得分:0 
回复 2楼 令狐少侠56
来人看看呗

[此贴子已经被作者于2015-10-26 12:15编辑过]

2015-10-26 11:25
快速回复:多项式加法怎么就错了
数据加载中...
 
   



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

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