VC多項式運算

2021-07-01 21:38:20 字數 3362 閱讀 7200

多項式運算主要是係數和指數的處理,本文用乙個簡單的例子實現該功能。

首先,新建控制台應用程式

然後,新建類

標頭檔案

// polynomial.h: inte***ce 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#if _msc_ver > 1000

#pragma once

#endif // _msc_ver > 1000

struct tpolynomial_item

};class cpolynomial

return *this;

} //兩個多項式相加

static void polynomial_sum(cpolynomial& itemres,cpolynomial & itema,cpolynomial & itemb)

pcurr = itemb.m_ppolynomial;

while(pcurr)

itemres.simplified();

} //乙個多項式與乙個係數相乘

static void polynomial_qua(cpolynomial& itemres,cpolynomial & itema,int nfactor)

itemres.simplified();

} //多項式a 減去 多項式b

static void polynomial_diff(cpolynomial& itemres,cpolynomial & itema,cpolynomial & itemb)

pcurr = itemb.m_ppolynomial;

while(pcurr)

itemres.simplified();

} //兩個多項式相乘

static void polynomial_qua(cpolynomial& itemres,cpolynomial & itema,cpolynomial & itemb)

pcurra = pcurra->pnext;

} itemres.simplified();

}};#endif // !defined(afx_polynomial_h__06b9bccd_1789_4834_9fe7_bfc8101d00cf__included_)

原始檔

// polynomial.cpp: implementation of the cpolynomial class.

////

#include#include "polynomial.h"

#include//

// construction/destruction

//cpolynomial::cpolynomial()

cpolynomial::~cpolynomial()

void cpolynomial::clear()

m_ppolynomial = null;

}void cpolynomial::additem(int ncoef,int nexp)

while(pcurr->pnext)

}//沒有找到同類項,新增到最後乙個

pcurr->pnext = new tpolynomial_item;

pcurr->pnext->ncoef = ncoef;

pcurr->pnext->nexp = nexp;

pcurr->pnext->pnext = null;

} else//如果多項式為空,則新增到首指標 }

void cpolynomial::sort(bool breduce)

if (!m_ppolynomial->pnext)//如果只有乙個,直接返回

int ncount = getcount();

int index = 0;

tpolynomial_item* ptemp = new tpolynomial_item[ncount];

tpolynomial_item* pcurr = m_ppolynomial;

while(pcurr)

//冒泡法排序

int i=0;

tpolynomial_item item;

for (i=1; iptemp[j+1].nexp)

}} }

//從陣列還原到鍊錶中

m_ppolynomial = ptemp[0].pnext;

m_ppolynomial->pnext = null;

pcurr = m_ppolynomial;

for (i=0; ipnext = ptemp[i].pnext;

pcurr = pcurr->pnext;

pcurr->pnext = null;

} delete ptemp;

}int cpolynomial::getcount()

return ncount;

}void cpolynomial::input()

}void cpolynomial::output()

else

}} cout

ptemp[index].nexp = pcurr->nexp;

ptemp[index].pnext = pcurr;

index++;

pcurr = pcurr->pnext;

} //合併同類項,合併之後係數為0的項放在前面

int i=0, j=0;

for (i=0; ipnext = ptemp[i].pnext;

pcurr = pcurr->pnext;

pcurr->pnext = null;

}} else

else//第乙個係數不為0的新增到首指標

多項式運算

多項式運算 順序棧實現 在demo1.cpp基礎上增加 彌補異常處理 以及多位數運算缺點 include include include define ok 1 define error 2 define overflow 1 define ini size 100 typedef int elem...

多項式運算封裝

update on 21.12.30 新增了polyeva 修補了polymod處多測時可能產生的 bug。update on 22.2.7 重 寫 前一版太醜了 改為完全封裝版本 使用std vector存放多項式係數,運算在命名空間polynomial 裡 實現的並不優秀,但應該很穩 霧 全部提...

2 10多項式及其運算

1 多項式的表達與建立 matlab約定n次多項式用乙個長度為n 1的係數行向量來表示,按降冪排列,缺少的冪次項係數為0.2 多項式的運算 多項式的運算由四則運算符和運算函式組成,主要如下 1 多項式加減運算,維數必須相同 2 多項式乘運算 利用函式c conv a,b 3 多項式除運算 利用函式 ...