c++编程 多项式的乘法
#include<iostream>#include<cmath>usingnamespacestd;formula*head,*rear,*p;formula*set(...
#include<iostream>
#include<cmath>
using namespace std;
formula *head,*rear,*p;
formula *set();//设置多项式的函数
formula *deal(formula *m);//将多项式合并同类项并按照指数从小到大排列
void num(formula *m);//计算多项式的数值
void show(formula *m);//显示多项式
formula *mul(formula *m,formula *n);//多项式的乘法
struct formula
{
double a;
int i;
formula *next;
};//链表,用于形成多项式
void main()
{
system("color 3e");
formula *s1,*s2,*snum;
cout<<"程序开始了"<<endl;
s1=set();
cout<<"f(x)=";
show(s1);
cout<<"***************"<<endl;
s2=set();
cout<<"g(x)=";
show(s2);
system("cls");
cout<<"^^^^^^^^^^^^^^^^^^"<<endl;
cout<<"下面开始运算"<<endl;
cout<<endl;
cout<<"f(x)=";
show(s1);
cout<<endl;
cout<<"g(x)=";
show(s2);
cout<<endl;
snum=mul(s1,s2);cout<<"f(x)*g(x)=";
show(snum);
cout<<"此次运算结束"<<endl;
}
void show(formula *m)
{
formula *rem;
rem=m;
while(m->next!=NULL)
{
switch(m->i){
case 0:cout<<m->a<<"+";break;
case 1:cout<<m->a<<"x"<<m->i<<"+";break;
default:cout<<m->a<<"x^"<<m->i<<"+";break;
}
m=m->next;
}
switch(m->i)
{
case 0:cout<<m->a;break;
case 1:cout<<m->a<<"x"<<m->i;break;
default:cout<<m->a<<"x^"<<m->i;break;
}
m=rem;
cout<<endl;
}
formula *mul(formula *m,formula *n)
{
int k=0;
formula *str[100],*r,*t,*sum,*x,*y;
x=m->next;
y=n->next;
for(x;x!=NULL;x=x->next,k++)
{
str[k]=new formula;
r=str[k];
y=n->next;
for(y;y!=NULL;y=y->next)
{
t=new formula;
t->next=NULL;
t->a=x->a*y->a;
t->i=x->i+y->i;
r->next=t;
r=t;
}
}
r->next=NULL;
sum=str[0];
for(int z=1;z<k;z++)
sum=add(sum,str[k]);
return sum;
}
formula *set()
{
head=rear=new formula;
int j=1;
int y;
double x;
cout<<"输入多项式的项数"<<endl;
int n;
cin>>n;
for(j=1;j<=n;j++)
{
p=new formula;
cout<<"请输入第"<<j<<"项的系数和指数"<<endl;
cin>>x>>y;
p->a=x;
p->i=y;
rear->next=p;
rear=p;
}
rear->next=NULL;
p=head->next;
while(p!=NULL)
{
int t;
t=p->i;
formula *convey;
convey=p;
while(p->next!=NULL)
{
if (t==(p->next->i))
{
convey->a+=(p->next->a);
if (p->next==rear)
{
rear=p;
p->next=NULL;
}
else {
formula *del;
del=p->next;
p->next=del->next;
delete del;}
}
else
p=p->next;
}
p=convey;
p=p->next;
}
p=head->next;
while(p!=NULL)
{
formula *c;
c=p;
while(p->next!=NULL)
{
if((c->i)>(p->next->i))
{
int m;
m=c->i;
c->i=(p->next->i);
p->next->i=m;
double n;
n=c->a;
c->a=(p->next->a);
(p->next->a)=n;
}
p=p->next;
}
p=c;
p=p->next;
}
p=head->next;
return p;
delete head,rear;
}
formula *deal(formula *m)
{
formula *tem;
tem=m;
while(m!=NULL)
{
int t;
t=m->i;
formula *convey;
convey=m;
while(m->next!=NULL)
{
if (t==(m->next->i))
{
convey->a+=(m->next->a);
if (m->next->next==NULL)
{
m->next=NULL;
}
else {
formula *del;
del=m->next;
m->next=del->next;
delete del;}
}
else
m=m->next;
}
m=convey;
m=m->next;
}
m=tem;
while(m!=NULL)
{
formula *c;
c=m;
while(m->next!=NULL)
{
if((c->i)>(m->next->i))
{
int con;
con=c->i;
c->i=(m->next->i);
m->next->i=con;
double non;
non=c->a;
c->a=(m->next->a);
(m->next->a)=non;
}
m=m->next;
}
m=c;
m=m->next;
}
m=tem;
return m;
}
求乘法的实现,方法不限,可以不用我的实现方法,急求,谢谢 展开
#include<cmath>
using namespace std;
formula *head,*rear,*p;
formula *set();//设置多项式的函数
formula *deal(formula *m);//将多项式合并同类项并按照指数从小到大排列
void num(formula *m);//计算多项式的数值
void show(formula *m);//显示多项式
formula *mul(formula *m,formula *n);//多项式的乘法
struct formula
{
double a;
int i;
formula *next;
};//链表,用于形成多项式
void main()
{
system("color 3e");
formula *s1,*s2,*snum;
cout<<"程序开始了"<<endl;
s1=set();
cout<<"f(x)=";
show(s1);
cout<<"***************"<<endl;
s2=set();
cout<<"g(x)=";
show(s2);
system("cls");
cout<<"^^^^^^^^^^^^^^^^^^"<<endl;
cout<<"下面开始运算"<<endl;
cout<<endl;
cout<<"f(x)=";
show(s1);
cout<<endl;
cout<<"g(x)=";
show(s2);
cout<<endl;
snum=mul(s1,s2);cout<<"f(x)*g(x)=";
show(snum);
cout<<"此次运算结束"<<endl;
}
void show(formula *m)
{
formula *rem;
rem=m;
while(m->next!=NULL)
{
switch(m->i){
case 0:cout<<m->a<<"+";break;
case 1:cout<<m->a<<"x"<<m->i<<"+";break;
default:cout<<m->a<<"x^"<<m->i<<"+";break;
}
m=m->next;
}
switch(m->i)
{
case 0:cout<<m->a;break;
case 1:cout<<m->a<<"x"<<m->i;break;
default:cout<<m->a<<"x^"<<m->i;break;
}
m=rem;
cout<<endl;
}
formula *mul(formula *m,formula *n)
{
int k=0;
formula *str[100],*r,*t,*sum,*x,*y;
x=m->next;
y=n->next;
for(x;x!=NULL;x=x->next,k++)
{
str[k]=new formula;
r=str[k];
y=n->next;
for(y;y!=NULL;y=y->next)
{
t=new formula;
t->next=NULL;
t->a=x->a*y->a;
t->i=x->i+y->i;
r->next=t;
r=t;
}
}
r->next=NULL;
sum=str[0];
for(int z=1;z<k;z++)
sum=add(sum,str[k]);
return sum;
}
formula *set()
{
head=rear=new formula;
int j=1;
int y;
double x;
cout<<"输入多项式的项数"<<endl;
int n;
cin>>n;
for(j=1;j<=n;j++)
{
p=new formula;
cout<<"请输入第"<<j<<"项的系数和指数"<<endl;
cin>>x>>y;
p->a=x;
p->i=y;
rear->next=p;
rear=p;
}
rear->next=NULL;
p=head->next;
while(p!=NULL)
{
int t;
t=p->i;
formula *convey;
convey=p;
while(p->next!=NULL)
{
if (t==(p->next->i))
{
convey->a+=(p->next->a);
if (p->next==rear)
{
rear=p;
p->next=NULL;
}
else {
formula *del;
del=p->next;
p->next=del->next;
delete del;}
}
else
p=p->next;
}
p=convey;
p=p->next;
}
p=head->next;
while(p!=NULL)
{
formula *c;
c=p;
while(p->next!=NULL)
{
if((c->i)>(p->next->i))
{
int m;
m=c->i;
c->i=(p->next->i);
p->next->i=m;
double n;
n=c->a;
c->a=(p->next->a);
(p->next->a)=n;
}
p=p->next;
}
p=c;
p=p->next;
}
p=head->next;
return p;
delete head,rear;
}
formula *deal(formula *m)
{
formula *tem;
tem=m;
while(m!=NULL)
{
int t;
t=m->i;
formula *convey;
convey=m;
while(m->next!=NULL)
{
if (t==(m->next->i))
{
convey->a+=(m->next->a);
if (m->next->next==NULL)
{
m->next=NULL;
}
else {
formula *del;
del=m->next;
m->next=del->next;
delete del;}
}
else
m=m->next;
}
m=convey;
m=m->next;
}
m=tem;
while(m!=NULL)
{
formula *c;
c=m;
while(m->next!=NULL)
{
if((c->i)>(m->next->i))
{
int con;
con=c->i;
c->i=(m->next->i);
m->next->i=con;
double non;
non=c->a;
c->a=(m->next->a);
(m->next->a)=non;
}
m=m->next;
}
m=c;
m=m->next;
}
m=tem;
return m;
}
求乘法的实现,方法不限,可以不用我的实现方法,急求,谢谢 展开
3个回答
展开全部
#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];
copy(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];
copy(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
请按任意键继续. . .
#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];
copy(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];
copy(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
请按任意键继续. . .
展开全部
这个是以前写的作业,希望对你有帮助
#include<stdio.h>
#include<malloc.h>
struct shaguo
{
float coef;
int expn;
struct shaguo*next;
};
void main()
{
struct shaguo*head1,*head2,*p1,*p2,*p,*head3,*p3;
int n=1;
head3=(struct shaguo*)malloc(sizeof(struct shaguo));
p1=p2=head1=(struct shaguo*)malloc(sizeof(struct shaguo));
p1=p2=(struct shaguo*)malloc(sizeof(struct shaguo));
printf("请输入第一个多项式\n");
scanf("%f%d",&p1->coef,&p1->expn);
head1->next=p1;
while(p1->coef!=0&&p1->expn!=0)
{
p2=p1;
p1=(struct shaguo*)malloc(sizeof(struct shaguo));
scanf("%f%d",&p1->coef,&p1->expn);
p2->next=p1;
}
p2->next->next=NULL;
p1=p2=head2=(struct shaguo*)malloc(sizeof(struct shaguo));
p1=p2=(struct shaguo*)malloc(sizeof(struct shaguo));
printf("请输入第二个多项式\n");
scanf("%f%d",&p1->coef,&p1->expn);
head2->next=p1;
while(p1->coef!=0&&p1->expn!=0)
{
p2=p1;
p1=(struct shaguo*)malloc(sizeof(struct shaguo));
scanf("%f%d",&p1->coef,&p1->expn);
p2->next=p1;
}
p2->next->next=NULL;
p1=head1->next;
p2=head2->next;
p=(struct shaguo*)malloc(sizeof(struct shaguo));
int num=0;
while(p1->coef!=0&&p1->expn!=0)
{
p2=head2->next;
while(p2->coef!=0&&p2->expn!=0)
{
p->coef=p1->coef*p2->coef;
p->expn=p1->expn+p2->expn;
num++;
if(num==1)
{
head3->next=p;
p3=p;
}
else
{
p3->next=p;
p3=p;
}
p=(struct shaguo*)malloc(sizeof(struct shaguo));
p2=p2->next;
}
p1=p1->next;
}
p3->next=(struct shaguo*)malloc(sizeof(struct shaguo));
p3->next->coef=0;
p3->next->expn=0;
p3->next->next=NULL;
p=head3;
p3=head3->next;
p1=head3->next;
n=0;
while(p3->coef!=0&&p3->expn!=0)
{
p=p3;
p1=p3->next;
while(p1->coef!=0&&p1->expn!=0)
{
n++;
if(p3->expn==p1->expn)
{
p3->coef=p3->coef+p1->coef;
p1=p1->next;
free(p->next);
p->next=p1;
n=0;
}
if(n)
{
p1=p1->next;
p=p->next;
}
}
p3=p3->next;
}
p1=head3->next;
printf("两多项式相乘:\n");
while(p1->coef!=0&&p1->expn!=0)
{
printf("%f,%d ",p1->coef,p1->expn);
p1=p1->next;
}
}
#include<stdio.h>
#include<malloc.h>
struct shaguo
{
float coef;
int expn;
struct shaguo*next;
};
void main()
{
struct shaguo*head1,*head2,*p1,*p2,*p,*head3,*p3;
int n=1;
head3=(struct shaguo*)malloc(sizeof(struct shaguo));
p1=p2=head1=(struct shaguo*)malloc(sizeof(struct shaguo));
p1=p2=(struct shaguo*)malloc(sizeof(struct shaguo));
printf("请输入第一个多项式\n");
scanf("%f%d",&p1->coef,&p1->expn);
head1->next=p1;
while(p1->coef!=0&&p1->expn!=0)
{
p2=p1;
p1=(struct shaguo*)malloc(sizeof(struct shaguo));
scanf("%f%d",&p1->coef,&p1->expn);
p2->next=p1;
}
p2->next->next=NULL;
p1=p2=head2=(struct shaguo*)malloc(sizeof(struct shaguo));
p1=p2=(struct shaguo*)malloc(sizeof(struct shaguo));
printf("请输入第二个多项式\n");
scanf("%f%d",&p1->coef,&p1->expn);
head2->next=p1;
while(p1->coef!=0&&p1->expn!=0)
{
p2=p1;
p1=(struct shaguo*)malloc(sizeof(struct shaguo));
scanf("%f%d",&p1->coef,&p1->expn);
p2->next=p1;
}
p2->next->next=NULL;
p1=head1->next;
p2=head2->next;
p=(struct shaguo*)malloc(sizeof(struct shaguo));
int num=0;
while(p1->coef!=0&&p1->expn!=0)
{
p2=head2->next;
while(p2->coef!=0&&p2->expn!=0)
{
p->coef=p1->coef*p2->coef;
p->expn=p1->expn+p2->expn;
num++;
if(num==1)
{
head3->next=p;
p3=p;
}
else
{
p3->next=p;
p3=p;
}
p=(struct shaguo*)malloc(sizeof(struct shaguo));
p2=p2->next;
}
p1=p1->next;
}
p3->next=(struct shaguo*)malloc(sizeof(struct shaguo));
p3->next->coef=0;
p3->next->expn=0;
p3->next->next=NULL;
p=head3;
p3=head3->next;
p1=head3->next;
n=0;
while(p3->coef!=0&&p3->expn!=0)
{
p=p3;
p1=p3->next;
while(p1->coef!=0&&p1->expn!=0)
{
n++;
if(p3->expn==p1->expn)
{
p3->coef=p3->coef+p1->coef;
p1=p1->next;
free(p->next);
p->next=p1;
n=0;
}
if(n)
{
p1=p1->next;
p=p->next;
}
}
p3=p3->next;
}
p1=head3->next;
printf("两多项式相乘:\n");
while(p1->coef!=0&&p1->expn!=0)
{
printf("%f,%d ",p1->coef,p1->expn);
p1=p1->next;
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
这么多。傻子看懂。你找他吧
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询