展开全部
最近在学C++,也是新手,不喜勿喷,仅供参考(注:在Dev C++下试验能正常运行)。三个文件:头文件、类定义文件和主函数文件(析构函数没写了,你自己写下吧)。
// 头文件名:polynomial.h
# ifndef POLYNOMIAL_H_
# define POLYNOMIAL_H_
class Polynomial{
public:
static int count; // 类外访问只能访问公有成员,因此需定义在public中
int n;
double *pcoef;
void InputCoef(); // 输入系数
void Output() const;
double Calculate(double x) const;
// 运算符重载
Polynomial & operator +(Polynomial &p1);
Polynomial & operator -(Polynomial &p1);
Polynomial & operator *(Polynomial &p1);
Polynomial & operator =(Polynomial &p1);
// 构造函数
Polynomial(int iniN) { n = iniN; count +=1;}
Polynomial() { n = 0; count +=1;}
Polynomial(Polynomial &p) { n = p.n; count +=1;}
};
# endif
// 类成员定义文件
# include <iostream>
using namespace std;
# include "polynomial.h"
# include <cmath>
// 静态数据成员的初始化
int Polynomial::count= 0;
void Polynomial::InputCoef() // 该函数不能被定义为常函数成员,因为它要改变pcoef指针。
{
pcoef = new double[n+1]; // 最高次为n,系数共有n+1项:次数从0-n.
int i;
cout<<"您输入的多项式次数为:"<<n<<".请按次数从高到低输入多项式的"<<n+1<<"个系数:";
for (i=0;i<=n;i++)
{
cin>> *(pcoef+i);
}
}
void Polynomial::Output() const
{
int i;
if (n==0) return;
cout<<"多项式为:";
for (i=n;i>=0;i--)
{
if (*(pcoef+n-i) == 0)
continue;
else if (*(pcoef+n-i) <0)
{
if (*(pcoef+n-i) == -1) cout<<'-';
else cout<<*(pcoef+n-i);
}
else
{
if ((n-i)!=0) cout<<'+';
if (*(pcoef+n-i) == 1 && i == 0 ) cout<<*(pcoef+n-i);
else if (*(pcoef+n-i) != 1) cout<<*(pcoef+n-i);
}
if (i == 1) cout<<'x';
else if (i == 0) continue;
else cout<<"x^"<<i;
}
cout<<endl;
return;
}
double Polynomial::Calculate(double x) const
{
double total=0;
int i;
for (i=0;i<=n;i++)
{
total += *(pcoef+i)*pow(x, n-i);
}
return total;
}
// 运算符重载
Polynomial & Polynomial::operator +(Polynomial &p1)
{
int i, max_n, min_n;
max_n = n>p1.n?n:p1.n;
Polynomial result(max_n);
result.pcoef = new double[max_n+1];
for (i=0;i<=max_n;i++)
{
if (n>=i)
*(result.pcoef+max_n-i) = *(pcoef+n-i);
else
*(result.pcoef+max_n-i) = 0;
}
for (i=0;i<=p1.n;i++)
{
*(result.pcoef+max_n-i) += *(p1.pcoef+p1.n-i);
}
return result;
}
Polynomial & Polynomial::operator -(Polynomial &p1)
{
int i, max_n, min_n;
max_n = n>p1.n?n:p1.n;
Polynomial result(max_n);
result.pcoef = new double[max_n+1];
for (i=0;i<=max_n;i++)
{
if (n>=i)
*(result.pcoef+max_n-i) = *(pcoef+n-i);
else
*(result.pcoef+max_n-i) = 0;
}
for (i=0;i<=p1.n;i++)
{
*(result.pcoef+max_n-i) -= *(p1.pcoef+p1.n-i);
}
return result;
}
Polynomial & Polynomial::operator *(Polynomial &p1)
{
Polynomial result;
int i, j, result_n;
result.n = n + p1.n;
result.pcoef = new double[result_n+1];
// 将每个元素均赋值为0
for (i=0;i<=result_n;i++) *(result.pcoef+i) = 0;
for (i=0;i<=p1.n;i++)
for (j=0;j<=n;j++)
*(result.pcoef+i+j) = *(result.pcoef+i+j) + *(p1.pcoef+i) * *(pcoef+j);
return result;
}
Polynomial & Polynomial::operator =(Polynomial &p1) // 看来有地址的地方不能用值传递
{
n = p1.n;
pcoef = p1.pcoef;
return *this;
}
// 主文件:
# include <iostream>
using namespace std;
# include "polynomial.h"
int main()
{
int n;
Polynomial obj1(3);
cout<<"目前对象数为:"<<Polynomial::count<<endl; //静态数据成员是类的所有对象共有的,所以应该用Polynomial::n
obj1.InputCoef();
obj1.Output();
cout<<"当x="<<2<<"时多项式的值为:"<<obj1.Calculate(2)<<endl;
Polynomial obj2(4);
obj2.InputCoef();
obj2.Output();
// 求和
Polynomial obj3;
obj3 = obj1 + obj2;
obj3.Output();
// 求差
obj3 = obj1 - obj2;
obj3.Output();
// 求积
obj3 = obj1 * obj2;
obj3.Output();
return 0;
}
// 头文件名:polynomial.h
# ifndef POLYNOMIAL_H_
# define POLYNOMIAL_H_
class Polynomial{
public:
static int count; // 类外访问只能访问公有成员,因此需定义在public中
int n;
double *pcoef;
void InputCoef(); // 输入系数
void Output() const;
double Calculate(double x) const;
// 运算符重载
Polynomial & operator +(Polynomial &p1);
Polynomial & operator -(Polynomial &p1);
Polynomial & operator *(Polynomial &p1);
Polynomial & operator =(Polynomial &p1);
// 构造函数
Polynomial(int iniN) { n = iniN; count +=1;}
Polynomial() { n = 0; count +=1;}
Polynomial(Polynomial &p) { n = p.n; count +=1;}
};
# endif
// 类成员定义文件
# include <iostream>
using namespace std;
# include "polynomial.h"
# include <cmath>
// 静态数据成员的初始化
int Polynomial::count= 0;
void Polynomial::InputCoef() // 该函数不能被定义为常函数成员,因为它要改变pcoef指针。
{
pcoef = new double[n+1]; // 最高次为n,系数共有n+1项:次数从0-n.
int i;
cout<<"您输入的多项式次数为:"<<n<<".请按次数从高到低输入多项式的"<<n+1<<"个系数:";
for (i=0;i<=n;i++)
{
cin>> *(pcoef+i);
}
}
void Polynomial::Output() const
{
int i;
if (n==0) return;
cout<<"多项式为:";
for (i=n;i>=0;i--)
{
if (*(pcoef+n-i) == 0)
continue;
else if (*(pcoef+n-i) <0)
{
if (*(pcoef+n-i) == -1) cout<<'-';
else cout<<*(pcoef+n-i);
}
else
{
if ((n-i)!=0) cout<<'+';
if (*(pcoef+n-i) == 1 && i == 0 ) cout<<*(pcoef+n-i);
else if (*(pcoef+n-i) != 1) cout<<*(pcoef+n-i);
}
if (i == 1) cout<<'x';
else if (i == 0) continue;
else cout<<"x^"<<i;
}
cout<<endl;
return;
}
double Polynomial::Calculate(double x) const
{
double total=0;
int i;
for (i=0;i<=n;i++)
{
total += *(pcoef+i)*pow(x, n-i);
}
return total;
}
// 运算符重载
Polynomial & Polynomial::operator +(Polynomial &p1)
{
int i, max_n, min_n;
max_n = n>p1.n?n:p1.n;
Polynomial result(max_n);
result.pcoef = new double[max_n+1];
for (i=0;i<=max_n;i++)
{
if (n>=i)
*(result.pcoef+max_n-i) = *(pcoef+n-i);
else
*(result.pcoef+max_n-i) = 0;
}
for (i=0;i<=p1.n;i++)
{
*(result.pcoef+max_n-i) += *(p1.pcoef+p1.n-i);
}
return result;
}
Polynomial & Polynomial::operator -(Polynomial &p1)
{
int i, max_n, min_n;
max_n = n>p1.n?n:p1.n;
Polynomial result(max_n);
result.pcoef = new double[max_n+1];
for (i=0;i<=max_n;i++)
{
if (n>=i)
*(result.pcoef+max_n-i) = *(pcoef+n-i);
else
*(result.pcoef+max_n-i) = 0;
}
for (i=0;i<=p1.n;i++)
{
*(result.pcoef+max_n-i) -= *(p1.pcoef+p1.n-i);
}
return result;
}
Polynomial & Polynomial::operator *(Polynomial &p1)
{
Polynomial result;
int i, j, result_n;
result.n = n + p1.n;
result.pcoef = new double[result_n+1];
// 将每个元素均赋值为0
for (i=0;i<=result_n;i++) *(result.pcoef+i) = 0;
for (i=0;i<=p1.n;i++)
for (j=0;j<=n;j++)
*(result.pcoef+i+j) = *(result.pcoef+i+j) + *(p1.pcoef+i) * *(pcoef+j);
return result;
}
Polynomial & Polynomial::operator =(Polynomial &p1) // 看来有地址的地方不能用值传递
{
n = p1.n;
pcoef = p1.pcoef;
return *this;
}
// 主文件:
# include <iostream>
using namespace std;
# include "polynomial.h"
int main()
{
int n;
Polynomial obj1(3);
cout<<"目前对象数为:"<<Polynomial::count<<endl; //静态数据成员是类的所有对象共有的,所以应该用Polynomial::n
obj1.InputCoef();
obj1.Output();
cout<<"当x="<<2<<"时多项式的值为:"<<obj1.Calculate(2)<<endl;
Polynomial obj2(4);
obj2.InputCoef();
obj2.Output();
// 求和
Polynomial obj3;
obj3 = obj1 + obj2;
obj3.Output();
// 求差
obj3 = obj1 - obj2;
obj3.Output();
// 求积
obj3 = obj1 * obj2;
obj3.Output();
return 0;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询