| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2459 人关注过本帖
标题:多项式乘法中结果合并时加减号的问题
只看楼主 加入收藏
稻草人25
Rank: 1
等 级:新手上路
帖 子:15
专家分:0
注 册:2016-4-20
结帖率:40%
收藏
已结贴  问题点数:8 回复次数:2 
多项式乘法中结果合并时加减号的问题
#include<iostream>
using namespace std;
typedef struct node
{
    char sign;  //符号
    float coef; //系数     
    int expn;    //指数  
    node* next;
}
node,*dxscf;  

void sort(dxscf& Head)
{  
    //采用冒泡排序对多项式进行排序
    dxscf Q;
for(dxscf P1 = NULL; P1 != Head->next->next; P1 = Q)   
    for(dxscf P2 = Head->next; P2->next != P1; P2 = P2->next)
    {   
        if(P2->expn > P2->next->expn)    //如果指数前一个大于后一个,则互换指数和系数
        {     
            int temp1;     
            double temp2;     
            char temp3;  //temp1,2,3是三个交换变量     
            temp1 = P2->expn;     
            P2->expn = P2->next->expn;     
            P2->next->expn = temp1;
            temp2 = P2->coef;     
            P2->coef = P2->next->coef;     
            P2->next->coef = temp2;     
            temp3 = P2->sign;     
            P2->sign = P2->next->sign;     
            P2->next->sign = temp3;
        }
         Q = P2->next;    //此处让Q为本次执行后的P2的下一个,以便P1=Q让P1成为P2的下一个,作为下一次循环的结束标志。   
    }
}//冒泡排序(升序)
 
void PrintList(dxscf Head)    //输出函数
{  
    dxscf p = Head ->next;  
    if(p != NULL )
   {   
        if(p->coef != 0)    //系数不为0
        {   
            if(p->expn != 0)    //指数不为0
            {     
                if(p->expn != 1)    //指数是否为1
                {            
                    if(p->coef == 1)    //系数是否为1
                        cout << "x^" << p->expn;      
                    else      
                        cout << p->coef << "x^" << p->expn;
                }     
                else    //指数为1
                    {      
                        if(p->coef == 1)    //指数为1且系数为1      
                            cout << "x";
                        else      
                            cout << p->coef << "x";     
                    }   
            }   
    else     
        cout << p->coef;   //指数为0
        }  //输出第一项
    p = p->next;   //指向第二项
   }

while(p != NULL)
{   
    if(p->sign == '+')    //第一种情况:+
    {   
        if(p->coef != 0)    //系数不为0
        {     
            if(p->expn != 0)    //指数不为0
            {      
                if(p->expn != 1)    //指数不为1
                {      
                    if(p->coef == 1)    // 系数为1      
                        cout << '+' << "x^" << p->expn;      
                    else                    
                        cout << '+' << p->coef << "x^" << p->expn;      
                }      
                else
                {      
                    if(p->coef == 1)    //指数为1     
                        cout << '+' << "x";      
                    else        
                        cout << '+' << p->coef << "x";      
                }     
            }     
            else      
                cout << '+' << p->coef;
            
        }   
        else
            {     
                p = p->next;     
                continue;        
            }   
    }   
   
    if(p->sign == '-')        //第二种情况:-
    {   
        if(p->coef != 0)
        {   
            if(p->expn != 0)
            {      
                if(p->expn != 1)
                {        
                    if(p->coef == -1)         
                        cout << '-' << "x^" << p->expn;        
                    else         
                        cout << p->coef << "x^" << p->expn;      
        }      
                else
                {        
                    if(p->coef == -1)         
                        cout << '-' << "x";        
                    else         
                        cout << p->coef << "x";      
        }     
        }     
            else      
                cout << p->coef;   
        }
}  
    p = p->next;  
}  
}//输出其余的项

void Creat_List(dxscf& Head)       //创建多项式
{  
    cout << "请输入你的因式的项数:" << endl;  
    int n;  
    cin >> n;  
    dxscf p = Head;  
    cout << "开始输入因式的系数和指数:" << endl;  
    for(int i = 0; i < n; i++)
    {   
        dxscf Node = new node;   
        cout<<"请输入第"<<i+1<<"系数:";   
        cin >> Node->coef;
        if(Node->coef > 0)                     //判断符号
            Node->sign = '+';   
        if(Node->coef < 0)   
            Node->sign = '-';   
        cout<<"请输入第"<<i+1<<"指数:";   
        cin >> Node ->expn;   
        Node->next = p->next;   //在head后插入第一个节点
        p->next = Node;   
        p = p->next;  
    }
    p->next = NULL;  

    cout << "按升序排列的多项式是:";  
    sort(Head);  
    PrintList(Head);  
    cout << endl;
}//创建多项式   

void DelList(dxscf& Head, int n)    //删除多项式的第n个因式     
{  
    dxscf p = Head;  
    for(int i = 1; i < n; i++)    //p节点所值位置即为需要删除的前一位(有头节点)
        p = p->next;  
    dxscf Q;   
        Q = p->next;
    p->next = Q ->next;   
        delete Q;  
}  

void multip(dxscf Head1,dxscf Head2)      //用P2的元素去乘P1的每个元素   
{  
    dxscf Head3 = new node;  
    Head3->next = NULL;  
    dxscf P1, P2, P3;  
    P3 = Head3;  //
    for(P1 = Head1->next;  P1!=NULL; P1 = P1->next)   
        for(P2 = Head2->next;  P2!=NULL;P2 = P2->next)
        {    dxscf Node = new node;   
        Node->expn = P1->expn + P2->expn;   
        Node->coef = P1->coef * P2->coef;   
        if(Node->coef >= 0)     
            Node->sign = '+';   
        else     
            Node->sign = '-';   
           
        Node->next = P3->next;
        P3->next = Node;   
        P3 = P3->next;   }
   
        sort(Head3);//head3排序,为合并同类项做基础  
        
        int i = 1;  
        for(dxscf P = Head3->next; P->next != NULL;)
        {      
            if(P->expn == P->next->expn)
            {   
                P->coef += P->next->coef;   
                DelList(Head3,i+1);//把后面指数项加到与前面相同的指数项上面,然后删除该项   
        }   
            else
            {   
                P = P->next;   
                i++;   
        }  
        }//end for 为了合并同类项 排完序后若有同类项,则一定是位置上相邻的  
        cout << "两多项式相乘的结果是: ";
        PrintList(Head3);  cout << endl; }   

int main()
{  
    dxscf Head1 = new node;  
    dxscf Head2 = new node;  
    Head1->next = NULL;  
    Head2->next = NULL;  
    Creat_List(Head1);  
    Creat_List(Head2);         
    multip(Head1,Head2);
    return 0;
}
为什么合并时加减号都存在
[local]1[/local]


Q8_9_AVQ}]JUST%)49$UB}I.png (24.97 KB)
图片附件: 游客没有浏览图片的权限,请 登录注册
搜索更多相关主题的帖子: include 多项式 
2016-12-26 15:15
xzlxzlxzl
Rank: 15Rank: 15Rank: 15Rank: 15Rank: 15
来 自:湖北
等 级:贵宾
威 望:125
帖 子:1091
专家分:5825
注 册:2014-5-3
收藏
得分:8 
主要是void multip(dxscf Head1,dxscf Head2)函数合并同类项时的问题:合并后系数小于0,单符仍为‘+’,应修改为‘-’,应增加一句判断系数正负性的判断,如下红色部分代码:

        for(dxscf P = Head3->next; P->next != NULL;)
        {      
            if(P->expn == P->next->expn)
            {   
                P->coef += P->next->coef;
                if (P->coef<0)P->sign ='-';    //增加该行代码即可
                DelList(Head3,i+1);//把后面指数项加到与前面相同的指数项上面,然后删除该项   
            }   
            else
            {   
                P = P->next;   
                i++;   
            }  
        }//end for 为了合并同类项 排完序后若有同类项,则一定是位置上相邻的  
2016-12-27 10:37
稻草人25
Rank: 1
等 级:新手上路
帖 子:15
专家分:0
注 册:2016-4-20
收藏
得分:0 
回复 2楼 xzlxzlxzl
谢谢
2017-01-06 15:46
快速回复:多项式乘法中结果合并时加减号的问题
数据加载中...
 
   



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

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