下面是源程序:为了方便调试,附件里传了那个程序的三个源文件.
///////////C12_6.h/////////
//////////类的接口文件////
#ifndef __C12_6__
#define __C12_6__
#include <iostream>
using namespace std;
namespace wei
{
class Polynomial
{
public:
Polynomial();
Polynomial(int n);//n为最高幂指数
~Polynomial();
int size();// 返回项数
void resize(int n);//重新设定数组大小
void set(double*,int);//为数组填入数据
friend ostream& operator<<(ostream& outs,Polynomial& p);
friend Polynomial& operator +(Polynomial& P1,Polynomial& p2);
friend Polynomial& operator +(const double m, Polynomial p2);
friend Polynomial& operator +( Polynomial P1,const double m);
friend Polynomial& operator -(Polynomial& P1,Polynomial& p2);
friend Polynomial& operator -(const double m, Polynomial p2);
friend Polynomial& operator -( Polynomial P1,const double m);
friend Polynomial& operator *(Polynomial& P1,Polynomial& p2);
friend Polynomial& operator *(const double m, Polynomial p2);
friend Polynomial& operator *( Polynomial P1,const double m);
void operator =(Polynomial p1);
private:
double* data;//存放系数 次数从0到n
int n;//记录数据数量(即项数)
};
}
#endif
///////////////////C12_6.cpp////////////
///////////////////类的实现/////////////
#include <iostream>
#include "c12_6.h"
using namespace std;
namespace wei
{
Polynomial::Polynomial()
{
n=50;
data=new double[n];
}
Polynomial::Polynomial(int n)//n为最高幂指数
{
this->n=n+1;
data=new double[n];
}
Polynomial::~Polynomial()
{
delete []data;
}
int Polynomial:: size()
{
return n;
}// 返回项数
void Polynomial::resize(int m)
{
n=m;
data=new double[n];
}
void Polynomial::set(double* m,int sz)
{
n=sz;
data=new double[n];
for(int i=0;i<sz;i++)
data[i]=m[i];
}
Polynomial& operator +(Polynomial& P1,Polynomial& p2)
{
Polynomial temp;
temp=(P1.size()>=p2.size())?P1:p2;
if(P1.size()>=p2.size())
for(int i=0;i<p2.size();i++)
temp.data[i]=P1.data[i]+p2.data[i];
else
for(int i=0;i<P1.size();i++)
temp.data[i]=P1.data[i]+p2.data[i];
return temp;
}
Polynomial& operator +(const double m, Polynomial p2)
{
p2.data[0]+=m;
return p2;
}
Polynomial& operator +( Polynomial P1,const double m)
{
P1.data[0]+=m;
return P1;
}
Polynomial& operator -(Polynomial& P1,Polynomial& p2)//p1-p2
{
Polynomial temp;
if(P1.size()>=p2.size())//p1项数多
{
temp=P1;
for(int i=0;i<p2.size();i++)
temp.data[i]-=p2.data[i];
}
else//p2项数多
{
temp=p2;
for(int i=0;i<temp.size();i++)
temp.data[i]=-temp.data[i];
for(int j=0;j<P1.size();j++)
temp.data[j]+=p2.data[j];
}
return temp;
}
Polynomial& operator -(const double m, Polynomial p2)
{
for(int i=0;i<p2.size();i++)
p2.data[i]=-p2.data[i];
p2.data[0]+=m;
return p2;
}
Polynomial& operator -( Polynomial P1,const double m)
{
P1.data[0]-=m;
return P1;
}
Polynomial& operator *(Polynomial& P1,Polynomial& p2)
{
Polynomial temp;
temp.resize(P1.size()+p2.size());
int i,j,k;
int s=P1.size()+p2.size();
for(i=0;i<s;i++)
temp.data[i]=0.0;
for(i=0;i<s;i++)
for(j=0;j<p2.size();j++)
for(k=0;k<P1.size();k++)
{
if((j+k)==i)
temp.data[i]+=p2.data[j]*P1.data[k];
}
return temp;
}
Polynomial& operator *(const double m, Polynomial p2)
{
for(int i=0;i<p2.size();i++)
p2.data[i]*=m;
return p2;
}
Polynomial& operator *( Polynomial P1,const double m)
{
for(int i=0;i<P1.size();i++)
P1.data[i]*=m;
return P1;
}
void Polynomial::operator =(Polynomial p1)
{
n=p1.size();
data=new double[n];
for(int i=0;i<n;i++)
data[i]=p1.data[i];
}
ostream& operator<<(ostream& outs,Polynomial& p)
{
for(int i=0;i<p.size();i++)
{
if(p.data[i]!=0.0)
{
outs<<p.data[i];
for(int j=0;j<i;j++)
outs<<"*x";
if(i<p.size()-1 && p.data[i]>0.0)
outs<<" + ";
}
}
outs<<endl;
return outs;
}
}
/////////////////////////测试程序/////////////////
/////////////////////////main.cpp/////////////////
#include <iostream>
#include "C12_6.h"
using namespace std;
using namespace wei;
void main()
{
Polynomial p1(3),p2(2),p3(5);
//暂时记p1为三次多项式,p2为二次
double* temp=new double[p1.size()];
double* b=new double[p2.size()];
cout<<"按升幂依次输入第一个多项式系数:"<<endl;
for(int i=0;i<p1.size();i++)
cin>>temp[i];
p1.set(temp,p1.size());
cout<<"按升幂依次输入第二个多项式系数:"<<endl;
for(int j=0;j<p2.size();j++)
cin>>b[j];
p2.set(b,p2.size());
cout<<"你输入的第一个多项式是: ";
cout<<p1<<"你输入的第二个多项式是: "<<p2;
// cout<<p1-p2<<endl;
// cout<<p1+p2<<endl;
// cout<<p1*p2<<endl;
// cout<<p1+3.0<<endl;
// cout<<p1-3.0<<endl;
// cout<<3.0-p1<<endl;
// cout<<3.0*p1<<endl;
// cout<<p1*3.0<<endl;
// cout<<3.0+p1<<endl;
delete []temp;
delete []b;
}
[此贴子已经被作者于2007-6-13 12:25:05编辑过]