导航:首页 > 编程知识 > 多项式如何编程

多项式如何编程

发布时间:2023-04-22 07:38:12

⑴ c++编程 多项式的乘法

#include <iostream>
#include<algorithm>
using namespace std;

class Polynomial;
class Term{//多项式的每一项
friend Polynomial;
public:
float coef;//系数
int exp;//指数
};

class Polynomial{//多项式类
friend ostream & operator<<(ostream &o,const Polynomial & poly);
public:
Polynomial();
Polynomial(const Polynomial & poly);
~Polynomial();
Polynomial operator+(const Polynomial & poly);//多项式加法
Polynomial operator*(const Polynomial & poly);//多项式乘法
float Eval(float x);//数x代入多项式求值
void NewTerm(float coef,int exp);//添加一项,若有相同的指数项,则合并
private:
void insertTerm(const Term & term);//项的有序插入
private:
Term *termArray;//非零系数项数组
int capacity;//数组大小
int terms;//非零系数的项数
};

Polynomial::Polynomial()
{
this->terms=0;
this->capacity=10;
termArray=new Term[this->capacity];
}

Polynomial::Polynomial(const Polynomial & b)
{
this->terms=0;
this->capacity=b.capacity;
termArray = new Term[this->capacity];
for(int i=0;i<b.terms;i++){
NewTerm(b.termArray[i].coef,b.termArray[i].exp);
}
}

Polynomial::~Polynomial()
{
delete [] termArray;
}

Polynomial Polynomial::operator+(const Polynomial & b)
{
Polynomial c;
int aPos=0;
int bPos=0;
while(aPos<terms && bPos<b.terms){
if(termArray[aPos].exp == b.termArray[bPos].exp){
float coef=termArray[aPos].coef+b.termArray[bPos].coef;
if(coef)c.NewTerm(coef,termArray[aPos].exp);
aPos++;bPos++;
}else if(termArray[bPos].exp < b.termArray[bPos].exp){
c.NewTerm(b.termArray[bPos].coef,b.termArray[bPos].exp);
bPos++;
}else{
c.NewTerm(termArray[aPos].coef,termArray[aPos].exp);
aPos++;
}
}
while (aPos < terms){
c.NewTerm(termArray[aPos].coef,termArray[aPos].exp);
aPos++;
}
while (bPos < b.terms){
c.NewTerm(b.termArray[bPos].coef,b.termArray[bPos].exp);
bPos++;
}
return c;
}

Polynomial Polynomial::operator*(const Polynomial & b)
{
Polynomial c;
for(int i=0; i<terms; i++){
for(int j=0; j<b.terms; j++){
float coef = termArray[i].coef*b.termArray[j].coef;
int exp = termArray[i].exp + b.termArray[j].exp;
c.NewTerm(coef,exp);
}
}
return c;
}
void Polynomial::NewTerm(float coef, int exp)
{
if(terms == capacity){
capacity *= 2;
Term *tmp = new Term[capacity];
(termArray,termArray+terms,tmp);
delete [] termArray;
termArray = tmp;
}
Term ATerm;
ATerm.coef=coef;ATerm.exp=exp;
insertTerm(ATerm);
}
void Polynomial::insertTerm(const Term & term)
{
int i;
for(i=0; i<terms && term.exp<termArray[i].exp; i++){
}
if(term.exp == termArray[i].exp){
termArray[i].coef += term.coef;
if(!termArray[i].coef){
for(int j=i; j<terms-1; j++)
termArray[j]= termArray[j+1];
terms--;
}
}else{
for(int j=terms-1; j>=i;j--)
termArray[j+1]=termArray[j];
termArray[i] = term;
terms++;
}
}

float Polynomial::Eval(float x)
{
float res=0.0;
for(int i=0;i<terms; i++){
res += termArray[i].coef * pow(x,termArray[i].exp);
}
return res;
}

ostream & operator<<(ostream & o,const Polynomial & poly)
{
for(int i=0;i<poly.terms-1;i++){
o<<poly.termArray[i].coef<<"x^"<<poly.termArray[i].exp<<" + ";
}
o<<poly.termArray[poly.terms-1].coef<<"x^"<<poly.termArray[poly.terms-1].exp;
return o;
}

void test()
{
Polynomial p1;
p1.NewTerm(3,2);
p1.NewTerm(2.1,3);

Polynomial p2;
p2.NewTerm(1,2);
p2.NewTerm(1,3);
p2.NewTerm(5,1);

cout<<"("<<p1<<") + ("<<p2<<") = "<<p1+p2<<endl;
cout<<"F(x=2) = "<<(p1+p2).Eval(2)<<endl;
cout<<"("<<p1<<") * ("<<p2<<") = "<<p1 * p2<<endl;
}

int main()
{
test();
system("Pause");
return 0;
}
#include <iostream>
#include<algorithm>
using namespace std;

class Polynomial;
class Term{//多项式的每一项
friend Polynomial;
public:
float coef;//系数
int exp;//指数
};
class Polynomial{//多项式类
friend ostream & operator<<(ostream &o,const Polynomial & poly);
public:
Polynomial();
Polynomial(const Polynomial & poly);
~Polynomial();
Polynomial operator+(const Polynomial & poly);//多项式加法
Polynomial operator*(const Polynomial & poly);//多项式乘法
float Eval(float x);//数x代入多项式求值
void NewTerm(float coef,int exp);//添加一项,若有相同的指数项,则合并
private:
void insertTerm(const Term & term);//项的有序插入
private:
Term *termArray;//非零系数项数组
int capacity;//数组大小
int terms;//非零系数的项数
};
Polynomial::Polynomial()
{
this->terms=0;
this->capacity=10;
termArray=new Term[this->capacity];
}
Polynomial::Polynomial(const Polynomial & b)
{
this->terms=0;
this->capacity=b.capacity;
termArray = new Term[this->capacity];
for(int i=0;i<b.terms;i++){
NewTerm(b.termArray[i].coef,b.termArray[i].exp);
}
}

Polynomial::
~Polynomial()
{
delete [] termArray;
}

Polynomial Polynomial::operator+(const Polynomial & b)
{
Polynomial c;
int aPos=0;
int bPos=0;
while(aPos<terms && bPos<b.terms){
if(termArray[aPos].exp == b.termArray[bPos].exp){
float coef=termArray[aPos].coef+b.termArray[bPos].coef;
if(coef)c.NewTerm(coef,termArray[aPos].exp);
aPos++;bPos++;
}else if(termArray[bPos].exp < b.termArray[bPos].exp){
c.NewTerm(b.termArray[bPos].coef,b.termArray[bPos].exp);
bPos++;
}else{
c.NewTerm(termArray[aPos].coef,termArray[aPos].exp);
aPos++;
}
}
while (aPos < terms){
c.NewTerm(termArray[aPos].coef,termArray[aPos].exp);
aPos++;
}
while (bPos < b.terms){
c.NewTerm(b.termArray[bPos].coef,b.termArray[bPos].exp);
bPos++;
}
return c;
}

Polynomial Polynomial::operator*(const Polynomial & b)
{
Polynomial c;
for(int i=0; i<terms; i++){
for(int j=0; j<b.terms; j++){
float coef = termArray[i].coef*b.termArray[j].coef;
int exp = termArray[i].exp + b.termArray[j].exp;
c.NewTerm(coef,exp);
}
}
return c;
}
void Polynomial::NewTerm(float coef, int exp)
{
if(terms == capacity){
capacity *= 2;
Term *tmp = new Term[capacity];
(termArray,termArray+terms,tmp);
delete [] termArray;
termArray = tmp;
}
Term ATerm;
ATerm.coef=coef;ATerm.exp=exp;
insertTerm(ATerm);
}
void Polynomial::insertTerm(const Term & term)
{
int i;
for(i=0; i<terms && term.exp<termArray[i].exp; i++){
}
if(term.exp == termArray[i].exp){
termArray[i].coef += term.coef;
if(!termArray[i].coef){
for(int j=i; j<terms-1; j++)
termArray[j]= termArray[j+1];
terms--;
}
}else{
for(int j=terms-1; j>=i;j--)
termArray[j+1]=termArray[j];
termArray[i] = term;
terms++;
}
}

float Polynomial::Eval(float x)
{
float res=0.0;
for(int i=0;i<terms; i++){
res += termArray[i].coef * pow(x,termArray[i].exp);
}
return res;
}

ostream & operator<<(ostream & o,const Polynomial & poly)
{
for(int i=0;i<poly.terms-1;i++){
o<<poly.termArray[i].coef<<"x^"<<poly.termArray[i].exp<<" + ";
}
o<<poly.termArray[poly.terms-1].coef<<"x^"<<poly.termArray[poly.terms-1].exp;
return o;
}

void test()
{
Polynomial p1;
p1.NewTerm(3,2);
p1.NewTerm(2.1,3);

Polynomial p2;
p2.NewTerm(1,2);
p2.NewTerm(1,3);
p2.NewTerm(5,1);

cout<<"("<<p1<<") + ("<<p2<<") = "<<p1+p2<<endl;
cout<<"F(x=2) = "<<(p1+p2).Eval(2)<<endl;
cout<<"("<<p1<<") * ("<<p2<<") = "<<p1 * p2<<endl;
}
int main()
{

test();

system("Pause");

return 0;
}
测试结果:
Cpp代码
(2.1x^3 + 3x^2) + (1x^3 + 1x^2 + 5x^1) = 3.1x^3 + 4x^2 + 5x^1
F(x=2) = 50.8
(2.1x^3 + 3x^2) * (1x^3 + 1x^2 + 5x^1) = 2.1x^6 + 5.1x^5 + 13.5x^4 + 15x^3
请按任意键继续. . .
(2.1x^3 + 3x^2) + (1x^3 + 1x^2 + 5x^1) = 3.1x^3 + 4x^2 + 5x^1
F(x=2) = 50.8
(2.1x^3 + 3x^2) * (1x^3 + 1x^2 + 5x^1) = 2.1x^6 + 5.1x^5 + 13.5x^4 + 15x^3
请按任意键继续. . .

阅读全文

与多项式如何编程相关的资料

热点内容
猥琐猫表情教程 浏览:599
android音频文件格式 浏览:458
漫画脸app哪里可以下载 浏览:959
购买欢乐升级欢乐豆 浏览:282
学习智能机器人用什么编程最好 浏览:655
苹果手机如何管控app 浏览:633
mn文件夹 浏览:590
安卓平板通用刷机包下载 浏览:751
安卓获取内部存储路径 浏览:880
写代码两台显示器 浏览:327
unitypackage压缩文件 浏览:493
奕心安卓 浏览:563
使用土地的有关证明文件包含哪些 浏览:493
数据标注哪里可以接 浏览:482
在家自学编程下什么学 浏览:705
最近很火的app软件是什么软件 浏览:862
ai文字工具 浏览:157
兰博玩游戏路径怎么选择正确文件 浏览:972
淘宝直通车恢复老版本 浏览:510
播放草莓的图片我都文件 浏览:55

友情链接