Polynomial addition Operation using C++

//#include "stdafx.h" 
#include<iostream>
using namespace std;
#include<stdlib.h>
#include"stdio.h"
struct Term
{
int exp; //指数
double coef; //系数

};

struct Node

{

Term data; //结点内容,为TERM结构

struct Node *next;

};

template <typename T>

class Polynomial //多项式类

{

public:

Polynomial(Node* p) :front(p),n(0) //构造函数

{

p->data.coef=-1;

p->data.exp=-1;

p->next=NULL;

}

~Polynomial() { } //析构函数

void Print(); //显示

void Delete(Node *p); //销毁多项式值为0的项

int Getlength(); //获取多项式长度

void Setn();

void Getfront();

void Row(); //将多项式降序排序

T Power(T n, T x); //计算x^n,用于赋值

void Plus(Polynomial &B,Polynomial &R); //相加

void Minus(Polynomial &B,Polynomial &R); //相减

void Oppo(); //系数相反数,用于相减

void Evalute(double x); //赋值

void Derivate(Polynomial &R); //求导

void Multiply(Polynomial &B,Polynomial &R); //相乘

void destroyLink(); //销毁多项式

void menuPrint(); //菜单

void createLink(int n); //创建多项式

void Over(); //退出

int n;

double result;

private:

Node *front;

};

//计算x^n,用于赋值

template<class T>

T Polynomial<T>::Power(T n, T x)

{

double result=1;

for(int i=1;i<=n;i++)

result=result*x;

return result;

}
template<class T>

int Polynomial<T>::Getlength()

{

Node *p=front;

if (p == NULL)

return 0;

while(p->next!=NULL)

{

n++;

p=p->next;

}

return n;

}

//显示

template<class T>

void Polynomial<T>::Print()

{

Row();

if((front==NULL)||(front->next==NULL))

{

cout<<"一元多项式为空"<<endl;

}

else

{

Node *p=front->next;

if(p->data.exp!=0)

cout<<"Poly= "<<p->data.coef<<"x^"<<p->data.exp;

else if(p->data.exp==0)

cout<<"Poly= "<<p->data.coef;

if(p->next!=NULL)

{

p=p->next;

while(1)

{

if((p->data.coef<0)&&(p->data.exp!=0))

cout<<p->data.coef<<"x^"<<p->data.exp;

else if((p->data.coef>0)&&(p->data.exp!=0))

cout<<"+"<<p->data.coef<<"x^"<<p->data.exp;

else if((p->data.coef<0)&&(p->data.exp==0))

cout<<p->data.coef;

else if((p->data.coef>0)&&(p->data.exp==0))

cout<<"+"<<p->data.coef;

if(p->next!=NULL)

p=p->next;

else

break;

}

}

}

cout<<endl;

}

//销毁多项式值为0的项
template<class T>

void Polynomial<T>::destroyLink()

{

Node* a=front->next;

if(!a) throw "一元多项式已为空!";//抛出异常

while(1)

{

if(a->next)

{

Node* b=a;

a=a->next;

delete b;

}

else

{

delete a;

break;

}

}

front->next=NULL;

}
template<class T>

void Polynomial<T>::Delete(Node *p)

{

Node *q=front

;

while(q->next!=p)

q=q->next;

p=q;

q=q->next;

delete q;

}
template<class T>

void Polynomial<T>::createLink(int n)

{

if(front->next!=NULL)

{

destroyLink();

}

Node *a=front;

for(int i=1;i<=n;i++)

{

a->next=new Node;

a=a->next;

a->next=NULL;

cout<<"请输入第"<<i<<"项的系数和指数:"<<endl;

cout<<"系数:";

cin>>(a->data).coef;

cout<<"指数:";

cin>>(a->data).exp;

if(a->data.exp<0)

{

cout<<"您输入有误,指数不允许为负值!"<<endl;

Delete(a);

i--;

continue;

}

if(a->data.coef==0)

{

cout<<"系数为零,重新输入!"<<endl;

Delete(a);

i--;

continue;

}

}

}


template<class T>

void Polynomial<T>::Setn()

{

n=Getlength();

}

template<class T>

void Polynomial<T>::Getfront()

{

Node *front=new Node;

}

//将多项式降序排序与合并同类项

template<class T>

void Polynomial<T>::Row()

{

Node *p=front->next;

Node *q=front->next;

Node *t=front->next;

while(1) //冒泡排序

{

p=t;

q=p->next;

while(q)

{

if(p->data.exp>q->data.exp)

{

if(q->next)

q=q->next;

else

break;

}

else if(p->data.exp==q->data.exp) //合并同类项

{

p->data.coef=p->data.coef+q->data.coef;

Node *temp=front;

while(temp->next!=q)

temp=temp->next;

if(q->next)

{

q=temp;

temp=temp->next;

q->next=temp->next;

delete temp;

q=q->next;

}

else

{

q=temp;

temp=temp->next;

delete temp;

q->next=NULL;

break;

}

}

else

{

Node *temp = new Node;

temp->data=q->data;

q->data=p->data;

p->data=temp->data;

delete temp;

if(q->next)

q=q->next;

else

break;

}

}

if(t->next)

{

t=t->next;

}

else

break;

}

Setn();

}


template<class T>

void Polynomial<T>::Plus(Polynomial &B,Polynomial &R)

{

Node* a=front->next;

Node* b=B.front->next;

Node* r=R.front;

while(1)

{

r->next=new Node;

r=r->next;

r->next=NULL;

r->data=a->data;

if(a->next)

a=a->next;

else

break;

}

while(1)

{

r->next=new Node;

r=r->next;

r->next=NULL;

r->data=b->data;

if(b->next)

b=b->next;

else

break;

}

R.Row();

}


int main()

{

Node *p=new Node;

Node *q=new Node;

Node *r=new Node;

Polynomial<double> A(p); //操作多项式A

Polynomial<double> B(q); //操作多项式B

Polynomial<double> R(r); //操作多项式R(存储结果)

int m=0;

int n=0;



cout<<"请输入你要运算的第一个一元多项式的项数:"<<endl;

cin>>m;

A.createLink(m);

cout<<"请输入你要运算的第二个一元多项式的项数:"<<endl;

cin>>n;

B.createLink(n);



if(!m||!n)

{

cout<<"您的多项式创建有误,请重新选择……"<<endl; //异常(以下省略)


}

A.Plus(B,R);

cout<<"相加的两个一元多项式为:"<<endl;

A.Print();

B.Print();

cout<<"相加后的结果为:"<<endl;

R.Print();





}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Polynomial类是一个常见的数学类,用于存储和操作多项式。以下是一个简单的Polynomial类的C++代码示例: ```c++ #include <iostream> #include <vector> using namespace std; class Polynomial { private: vector<double> coefficients; public: Polynomial() {} Polynomial(vector<double> coeffs) { coefficients = coeffs; } int degree() { return coefficients.size() - 1; } double operator[](int i) const { return coefficients[i]; } double& operator[](int i) { return coefficients[i]; } Polynomial operator+(const Polynomial& other) const { vector<double> result(max(degree(), other.degree()) + 1); for (int i = 0; i <= degree(); i++) { result[i] += coefficients[i]; } for (int i = 0; i <= other.degree(); i++) { result[i] += other.coefficients[i]; } return Polynomial(result); } Polynomial operator-(const Polynomial& other) const { vector<double> result(max(degree(), other.degree()) + 1); for (int i = 0; i <= degree(); i++) { result[i] += coefficients[i]; } for (int i = 0; i <= other.degree(); i++) { result[i] -= other.coefficients[i]; } return Polynomial(result); } Polynomial operator*(const Polynomial& other) const { vector<double> result(degree() + other.degree() + 1); for (int i = 0; i <= degree(); i++) { for (int j = 0; j <= other.degree(); j++) { result[i + j] += coefficients[i] * other.coefficients[j]; } } return Polynomial(result); } friend ostream& operator<<(ostream& os, const Polynomial& p) { for (int i = p.degree(); i >= 0; i--) { if (p[i] == 0) { continue; } if (i == p.degree()) { os << p[i] << "x^" << i; } else if (i == 1) { os << " + " << p[i] << "x"; } else { os << " + " << p[i] << "x^" << i; } } return os; } }; int main() { vector<double> coeffs1 = {1, 2, 3}; vector<double> coeffs2 = {4, 5, 6}; Polynomial p1(coeffs1); Polynomial p2(coeffs2); cout << "p1 = " << p1 << endl; cout << "p2 = " << p2 << endl; Polynomial p3 = p1 + p2; Polynomial p4 = p1 - p2; Polynomial p5 = p1 * p2; cout << "p1 + p2 = " << p3 << endl; cout << "p1 - p2 = " << p4 << endl; cout << "p1 * p2 = " << p5 << endl; return 0; } ``` 这个Polynomial类支持多项式的加、减和乘法,并且可以输出多项式的字符串表示。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值