算法实现不了系数的正负号加减和x^0不显示为0

这段代码实现了一个多项式运算的类,包括多项式的加法和乘法操作,并提供了输出功能。通过迭代器遍历多项式的项,进行相应的数学运算,并将结果存储到新的多项式列表中。加法函数处理了系数和指数的比较与合并,乘法函数则通过两层循环计算每个项的乘积并累加到结果列表。
摘要由CSDN通过智能技术生成

结果问题类似这样,多项式中+-同时存在

 


//输出函数,用以输出多项式
        void Poly_output()
        {
            list<term>::iterator iter = m_poly_list_first.begin();	//输出多项式的每一项
            cout<<"第一个多项式为:";
            for(;iter!=m_poly_list_first.end();)
            { 
			    term t_temp=*iter; 
                cout<<t_temp.coef<<"x^"<<t_temp.exp; 
                if(++iter!=m_poly_list_result.end())
                cout<<"+";
            }
            cout<<endl;
            list<term>::iterator iterl = m_poly_list_second.begin();  //输出第二个多项式的每一项
			cout<<"第二个多项式为:";
			for(;iterl!=m_poly_list_second.end();) 
			{
				term t_temp = *iterl;
				cout<<t_temp.coef<<"x^"<<t_temp.exp;
				if(++iterl!=m_poly_list_result.end())
				cout<<"+";
			}
			cout<<endl;
        }
        void Poly_output_result()
        {
        	list<term>::iterator iter = m_poly_list_result.begin();  //输出多项式结果
			for(;iter!=m_poly_list_result.end();) 
			{
				term t_temp = *iter;
				cout<<t_temp.coef<<"x^"<<t_temp.exp;
				if(++iter!=m_poly_list_result.end())
				cout<<"+";
			}
		}
//加法函数,其基本思想同上边的私有成员函数Poly_add();
//此处不带参数,多项式运算对象为私有数据成员
        void Poly_add()
        {
            list<term>::iterator iter_first = m_poly_list_first.begin();
            list<term>::iterator iter_second = m_poly_list_second.begin();
            while(iter_first != m_poly_list_first.end() && iter_second != m_poly_list_second.end())
            {
                term t_temp;
                term t_first=(term)*iter_first;
                term t_second =(term)*iter_second;
                if(t_first.exp>t_second.exp)
                {
                    m_poly_list_result.push_back(t_first);
                    iter_first++;
                }
                else if(t_second.exp>t_first.exp)
                {
                    m_poly_list_result.push_back(t_second);
                    iter_second++;
                }
                else
                {
                    t_temp.coef=t_first.coef+t_second.coef;
                    t_temp.exp=t_first.exp;
                    m_poly_list_result.push_back(t_temp);
                    iter_first++;
                    iter_second++;
                }
            }
            for(;iter_first != m_poly_list_first.end();iter_first++)
            {
                m_poly_list_result.push_back(*iter_first);
            }
            for(;iter_second !=m_poly_list_second.end();iter_second++)
            {
                m_poly_list_result.push_back(*iter_second);
            }
        }
     
	//乘法函数,用以作多项式乘法
    void Poly_multi()
    {
        list<term> poly_list_result;
        list<term>::iterator iter_first = m_poly_list_first.begin();
        for(;iter_first!=m_poly_list_first.end();iter_first++)
        {
            list<term> poly_list_temp;      //用以存储多项式的中间运算结果
            list<term>::iterator iter_second=m_poly_list_second.begin();
            for(;iter_second!=m_poly_list_second.end();iter_second++)
            {
                term t_temp;    //用以存储项的中间运算结果
                term t_first = (term)*iter_first;
                term t_second= (term)*iter_second;   //此处实现多项式的相乘
                t_temp.coef =t_first.coef*t_second.coef;   //系数相乘
                t_temp.exp = t_first.exp + t_second.exp;    //指数相加
                poly_list_temp.push_back(t_temp);
            }
    //此处调用私有成员函数Poly_add();
                poly_list_result =Poly_add(poly_list_temp,poly_list_result);
        }
        //将运算结果赋值给私有数据成员,用以输出 
        m_poly_list_result = poly_list_result;
    }
     
};

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值