多项式运算主要是系数和指数的处理,本文用一个简单的例子实现该功能。
首先,新建控制台应用程序
然后,新建类
头文件
// Polynomial.h: interface for the CPolynomial class. // ////////////////////////////////////////////////////////////////////// #if !defined(AFX_POLYNOMIAL_H__06B9BCCD_1789_4834_9FE7_BFC8101D00CF__INCLUDED_) #define AFX_POLYNOMIAL_H__06B9BCCD_1789_4834_9FE7_BFC8101D00CF__INCLUDED_ #include<IOS.h> #if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000 struct tPOLYNOMIAL_ITEM { int nCoef;//系数 int nExp;//指数 tPOLYNOMIAL_ITEM* pNext;//指向下一项的指针 tPOLYNOMIAL_ITEM() { nCoef = 0; nExp = 0; pNext = NULL; } }; class CPolynomial { public: CPolynomial(); virtual ~CPolynomial(); private: tPOLYNOMIAL_ITEM* m_pPolynomial; public: double Calculate(); void Simplified();//化简多项式——合并同类项,移除系数为0的项 void Output(); //多项式输出 void Input(); //输入多项式 int GetCount(); //获取多项式项的个数 void Sort(bool bReduce = true); //降次幂排序 void AddItem(int nCoef,int nExp);//多项式增加一项 void Clear(); //多项式置空 //等号重载 CPolynomial& operator = (CPolynomial & polynomial) { Clear(); m_pPolynomial = NULL; tPOLYNOMIAL_ITEM* pCurr = polynomial.m_pPolynomial; while(pCurr) { AddItem(pCurr->nCoef,pCurr->nExp); pCurr = pCurr->pNext; } return *this; } //两个多项式相加 static void Polynomial_SUM(CPolynomial& ItemRes,CPolynomial & ItemA,CPolynomial & ItemB) { ItemRes.Clear(); tPOLYNOMIAL_ITEM* pCurr = ItemA.m_pPolynomial; while(pCurr) { ItemRes.AddItem(pCurr->nCoef,pCurr->nExp); pCurr = pCurr->pNext; } pCurr = ItemB.m_pPolynomial; while(pCurr) { ItemRes.AddItem(pCurr->nCoef,pCurr->nExp); pCurr = pCurr->pNext; } ItemRes.Simplified(); } //一个多项式与一个系数相乘 static void Polynomial_QUA(CPolynomial& ItemRes,CPolynomial & ItemA,int nFactor) { ItemRes.Clear(); tPOLYNOMIAL_ITEM* pCurr = ItemA.m_pPolynomial; while(pCurr) { int nCoef = nFactor * pCurr->nCoef; ItemRes.AddItem(nCoef,pCurr->nExp); pCurr = pCurr->pNext; } ItemRes.Simplified(); } //多项式A 减去 多项式B static void Polynomial_DIFF(CPolynomial& ItemRes,CPolynomial & ItemA,CPolynomial & ItemB) { ItemRes.Clear(); tPOLYNOMIAL_ITEM* pCurr = ItemA.m_pPolynomial; while(pCurr) { ItemRes.AddItem(pCurr->nCoef,pCurr->nExp); pCurr = pCurr->pNext; } pCurr = ItemB.m_pPolynomial; while(pCurr) { int nCoef = -1 * pCurr->nCoef; ItemRes.AddItem(nCoef,pCurr->nExp); pCurr = pCurr->pNext; } ItemRes.Simplified(); } //两个多项式相乘 static void Polynomial_QUA(CPolynomial& ItemRes,CPolynomial & ItemA,CPolynomial & ItemB) { ItemRes.Clear(); int nCoef = 0,nExp=0; tPOLYNOMIAL_ITEM* pCurrA = ItemA.m_pPolynomial; while(pCurrA) { tPOLYNOMIAL_ITEM* pCurrB = ItemB.m_pPolynomial; while(pCurrB) { nCoef = pCurrB->nCoef * pCurrA->nCoef; //系数相乘 nExp = pCurrB->nExp + pCurrA->nExp; //指数相加 ItemRes.AddItem(nCoef,nExp); pCurrB = pCurrB->pNext; } pCurrA = pCurrA->pNext; } ItemRes.Simplified(); } }; #endif // !defined(AFX_POLYNOMIAL_H__06B9BCCD_1789_4834_9FE7_BFC8101D00CF__INCLUDED_)
// Polynomial.cpp: implementation of the CPolynomial class. // ////////////////////////////////////////////////////////////////////// #include<iostream.h> #include "Polynomial.h" #include<math.h> ////////////////////////////////////////////////////////////////////// // Construction/Destruction ////////////////////////////////////////////////////////////////////// CPolynomial::CPolynomial() { m_pPolynomial = NULL; } CPolynomial::~CPolynomial() { Clear(); } void CPolynomial::Clear() { tPOLYNOMIAL_ITEM* pCurr = m_pPolynomial; tPOLYNOMIAL_ITEM* pNext = NULL; while(pCurr) { pNext = pCurr->pNext; delete pCurr; pCurr = pNext; } m_pPolynomial = NULL; } void CPolynomial::AddItem(int nCoef,int nExp) { if (m_pPolynomial)//如果多项式不为空 { tPOLYNOMIAL_ITEM* pCurr = m_pPolynomial; if (pCurr->nExp == nExp)//如果与第一个指数相同,则合并同类项,返回 { pCurr->nCoef = pCurr->nCoef + nCoef; return; } while(pCurr->pNext) { pCurr = pCurr->pNext; if(pCurr->nExp == nExp)//在指针向后移动的过程中,如果与某一项指数相同,则合并同类项,返回 { pCurr->nCoef = pCurr->nCoef + nCoef; return; } } //没有找到同类项,添加到最后一个 pCurr->pNext = new tPOLYNOMIAL_ITEM; pCurr->pNext->nCoef = nCoef; pCurr->pNext->nExp = nExp; pCurr->pNext->pNext = NULL; } else//如果多项式为空,则添加到首指针 { m_pPolynomial = new tPOLYNOMIAL_ITEM; m_pPolynomial->nCoef = nCoef; m_pPolynomial->nExp = nExp; m_pPolynomial->pNext = NULL; } } void CPolynomial::Sort(bool bReduce) { if (!m_pPolynomial)//如果为空,直接返回 { return; } if (!m_pPolynomial->pNext)//如果只有一个,直接返回 { return; } int nCount = GetCount(); int index = 0; //链表临时存储在线性数组中 tPOLYNOMIAL_ITEM* pTemp = new tPOLYNOMIAL_ITEM[nCount]; tPOLYNOMIAL_ITEM* pCurr = m_pPolynomial; while(pCurr) { pTemp[index].nCoef = pCurr->nCoef; pTemp[index].nExp = pCurr->nExp; pTemp[index].pNext = pCurr; index++; pCurr = pCurr->pNext; } //冒泡法排序 int i=0; tPOLYNOMIAL_ITEM item; for (i=1; i<nCount; i++) { for (int j=0; j<nCount-i; j++) { if (bReduce)//降次幂 { if (pTemp[j].nExp < pTemp[j+1].nExp) { item.nCoef = pTemp[j].nCoef; item.nExp = pTemp[j].nExp; item.pNext = pTemp[j].pNext; pTemp[j].nCoef = pTemp[j+1].nCoef; pTemp[j].nExp = pTemp[j+1].nExp; pTemp[j].pNext = pTemp[j+1].pNext; pTemp[j+1].nCoef = item.nCoef; pTemp[j+1].nExp = item.nExp; pTemp[j+1].pNext = item.pNext; } } else//升次幂 { if (pTemp[j].nExp > pTemp[j+1].nExp) { item.nCoef = pTemp[j].nCoef; item.nExp = pTemp[j].nExp; item.pNext = pTemp[j].pNext; pTemp[j].nCoef = pTemp[j+1].nCoef; pTemp[j].nExp = pTemp[j+1].nExp; pTemp[j].pNext = pTemp[j+1].pNext; pTemp[j+1].nCoef = item.nCoef; pTemp[j+1].nExp = item.nExp; pTemp[j+1].pNext = item.pNext; } } } } //从数组还原到链表中 m_pPolynomial = pTemp[0].pNext; m_pPolynomial->pNext = NULL; pCurr = m_pPolynomial; for (i=0; i<nCount; i++) { pCurr->pNext = pTemp[i].pNext; pCurr = pCurr->pNext; pCurr->pNext = NULL; } delete[] pTemp; } int CPolynomial::GetCount() { int nCount = 0; tPOLYNOMIAL_ITEM* pCurr = m_pPolynomial; while(pCurr) { nCount++; pCurr = pCurr->pNext; } return nCount; } void CPolynomial::Input() { char a = 'b'; while('q' != a && 'Q' != a) { tPOLYNOMIAL_ITEM Item; cout<<"请输入系数:"<<endl; cin>>Item.nCoef; cout<<"请输入指数:"<<endl; cin>>Item.nExp; AddItem(Item.nCoef,Item.nExp); cout<<"任意键继续,\"q\"结束输入。"; cin>>a; } } void CPolynomial::Output() { tPOLYNOMIAL_ITEM* pCurr = m_pPolynomial; while(pCurr) { cout<<pCurr->nCoef<<"X["<<pCurr->nExp<<"]"; pCurr = pCurr->pNext; if(pCurr) { if(pCurr->nCoef < 0) {} else { cout<<"+"; } } } cout<<endl; } void CPolynomial::Simplified() { if (!m_pPolynomial) { return; } int nCount = GetCount(); tPOLYNOMIAL_ITEM* pTemp = new tPOLYNOMIAL_ITEM[nCount]; int index = 0; tPOLYNOMIAL_ITEM* pCurr = m_pPolynomial; while(pCurr) { pTemp[index].nCoef = pCurr->nCoef; pTemp[index].nExp = pCurr->nExp; pTemp[index].pNext = pCurr; index++; pCurr = pCurr->pNext; } //合并同类项,合并之后系数为0的项放在前面 int i=0, j=0; for (i=0; i<nCount; i++) { for (j=i+1; j<nCount; j++) { if (pTemp[i].nExp == pTemp[j].nExp) { pTemp[j].nCoef = pTemp[i].nCoef + pTemp[j].nCoef; pTemp[i].nCoef = 0; } } } m_pPolynomial = NULL; pCurr = NULL; bool bBegin = false;//首指针是否设置标志 for (i=0; i<nCount; i++) { if (bBegin)//首指针已经设置,后续系数不为0的往后添加 { if (0 == pTemp[i].nCoef)//系数为0的释放掉 { delete pTemp[i].pNext; pTemp[i].pNext = NULL; } else//系数不为0往后添加 { pCurr->pNext = pTemp[i].pNext; pCurr = pCurr->pNext; pCurr->pNext = NULL; } } else { if (0 == pTemp[i].nCoef)//系数为0的释放掉 { delete pTemp[i].pNext; pTemp[i].pNext = NULL; } else//第一个系数不为0的添加到首指针 { bBegin = true; m_pPolynomial = pTemp[i].pNext; m_pPolynomial->pNext = NULL; pCurr = m_pPolynomial; } } } delete[] pTemp; } double CPolynomial::Calculate() { double dbSum = 0; int nX = 0; cout<<"X = ? ,请输入X的值。"<<endl; cin>>nX; tPOLYNOMIAL_ITEM* pCurr = m_pPolynomial; while(pCurr) { dbSum = dbSum + pCurr->nCoef* pow(nX,pCurr->nExp); pCurr = pCurr->pNext; } cout<<"计算结果为: "<<dbSum<<endl; return dbSum; }
#include<iostream.h> #include "Polynomial.h" void main() { CPolynomial pone; CPolynomial ptwo; CPolynomial pthree; cout<<"多项式运算例子"<<endl<<endl; cout<<"请输入多项式1"<<endl; pone.Input(); cout<<"请输入多项式2"<<endl; ptwo.Input(); cout<<"多项式1:"<<endl; pone.Output(); cout<<"多项式2:"<<endl; ptwo.Output(); cout<<"两个多项式相加:"<<endl; CPolynomial::Polynomial_SUM(pthree,ptwo,pone); pthree.Sort(); pthree.Output(); cout<<"两个多项式相减:"<<endl; CPolynomial::Polynomial_DIFF(pthree,ptwo,pone); pthree.Sort(); pthree.Output(); cout<<"多项式与系数相乘:"<<endl; CPolynomial::Polynomial_QUA(pthree,ptwo,2); pthree.Sort(); pthree.Output(); cout<<"两个多项式相乘"<<endl; CPolynomial::Polynomial_QUA(pthree,ptwo,pone); pthree.Sort(); pthree.Output(); pthree.Calculate(); }