加分,在线等 c++ polynomial class(多项式类)

帮个忙,谢谢啦,在线等。补分的定义一个多项式类(classpolynomial)输入指数和系数,要求output的指数从高到低,(degree3,coeff:70910,... 帮个忙,谢谢啦,在线等。补分的
定义一个多项式类(class polynomial )
输入指数和系数,要求 output 的指数从高到低,(degree 3,coeff:7 0 9 10, output: 7x^3+9x+10)
输入一个X的值,计算出结果
两个polynomial 相加和相减。
展开
 我来答
帐号已注销
推荐于2016-07-12 · TA获得超过301个赞
知道小有建树答主
回答量:223
采纳率:0%
帮助的人:207万
展开全部
#include <iostream>
using namespace std;

class Polynomial{
public:
void Input();
void Output();
Polynomial operator+(const Polynomial&);
Polynomial operator-(const Polynomial&);
private:
int degree;
int *coeff;
};

void Polynomial::Input(){
cout<<"请输入多项式指数degree:";
cin>>degree;
coeff=new int[degree+1];
cout<<"请从高到低输入多项式系数:";
for(int i=degree;i>=0;i--)
cin>>coeff[i];
}

void Polynomial::Output(){
for(int i=degree;i>=0;i--)
{
if(i==degree)
{
if(coeff[i]==1)
cout<<"x^"<<i;
else if(coeff[i]==-1)
cout<<"-x^"<<i;
else 
cout<<coeff[i]<<"x^"<<i;
}
else if(i==1)
{
if(coeff[i]==0)
continue;
else if(coeff[i]>0)
cout<<"+"<<coeff[i]<<"x";
else if(coeff[i]<0)
cout<<"-"<<(-coeff[i])<<"x";
}
else if(i==0)
{
if(coeff[i]==0)
cout<<endl;
else if(coeff[i]>0)
cout<<"+"<<coeff[i]<<endl;
else if(coeff[i]<0)
cout<<"-"<<(-coeff[i])<<endl;
}
else if(i>0&&i<degree)
{
if(coeff[i]==0)
continue;
else if(coeff[i]==1)
cout<<"+x^"<<i;
else if(coeff[i]==-1)
cout<<"-x^"<<i;
else if(coeff[i]>0)
cout<<"+"<<coeff[i]<<"x^"<<i;
else if(coeff[i]<0)
cout<<"-"<<(-coeff[i])<<"x^"<<i;
}

}
}

Polynomial Polynomial::operator+(const Polynomial &rhs){
Polynomial sum;
int MaxDegree=degree>rhs.degree?degree:rhs.degree;
int MinDegree=degree<rhs.degree?degree:rhs.degree;
sum.degree=MaxDegree;
sum.coeff=new int[sum.degree+1];
if(rhs.degree==MaxDegree)
{
for(int i=0;i<rhs.degree+1;i++)
sum.coeff[i]=rhs.coeff[i];
}
else{
for(int i=0;i<degree+1;i++)
sum.coeff[i]=coeff[i];
}
for(int i=0;i<MinDegree+1;i++)
sum.coeff[i]=coeff[i]+rhs.coeff[i];
return sum;
}

Polynomial Polynomial::operator-(const Polynomial &rhs){
Polynomial sub;
int MaxDegree=degree>rhs.degree?degree:rhs.degree;
int MinDegree=degree<rhs.degree?degree:rhs.degree;
sub.degree=MaxDegree;
sub.coeff=new int[sub.degree+1];
if(rhs.degree==MaxDegree)
{
for(int i=0;i<MaxDegree+1;i++)
sub.coeff[i]=rhs.coeff[i];
}
else{
for(int i=0;i<MaxDegree+1;i++)
sub.coeff[i]=coeff[i];
}
for(int i=0;i<MinDegree+1;i++)
sub.coeff[i]=coeff[i]-rhs.coeff[i];
return sub;

}

int main()
{
Polynomial a,b,c,d;
a.Input();
b.Input();
c=a+b;
d=a-b;
a.Output();
b.Output();
c.Output();
d.Output();
return 0;
}
满意请采纳哦!输出考虑了负系数或者0和1的情况,不懂的可以问,比较全面了。
更多追问追答
追问
还有付一个X的值,计算的
追答
字数超了,楼主给我你的QQ邮箱我发给你。
百度网友ce9825aa2
2013-10-20 · 超过30用户采纳过TA的回答
知道答主
回答量:62
采纳率:0%
帮助的人:76.4万
展开全部

不知道你满意不。=。=饿,修改完毕

#include <iostream>
using namespace std;
class polynomial
{
private:
int num[100];
int len;
public:
polynomial(int* a,int l)
{
int i;
len = l;
for(i=0;i<l;i++)
num[i] = a[l-i-1];
}
polynomial()
{
memset(num,0,sizeof(num));
len = 0;
}
int getx(int X)
{
int sum,i,x;
sum =0;
x=1;
for(i=0;i<len;i++)
{
sum+=num[i]*x;
x*=X;
}
return sum;
}
void Output()
{
int i;
for(i=len-1;i>=0;i--)
{
if(num[i])
{
if(num[i]>0 && i != len-1)
cout<<"+";
if(!(i!= 0 && num[i]==1))
cout<<num[i];
if(i)
cout<<"x";
if(i>1)
cout<<"^"<<i;
}
}
cout<<endl;
}
polynomial operator+ (polynomial a)
{
polynomial New;
int i;
New.len = len >a.len ? len : a.len;
for(i=0;i<New.len;i++)
New.num[i] = num[i]+a.num[i];
return New;
}
polynomial operator- (polynomial a)
{
polynomial New;
int i;
New.len = len >a.len ? len : a.len;
for(i=0;i<New.len;i++)
New.num[i] = num[i]-a.num[i];
return New;
}
};
int main()
{
int a[100];
int n,i;
cin>>n;
for(i=0;i<n;i++)
cin>>a[i];
polynomial Test(a,n);
polynomial Test3;
Test.Output();
cin>>n;
cout<<Test.getx(n)<<endl;
Test3 = Test + Test;
Test3.Output();
Test3 = Test - Test - Test;
Test3.Output();
system("pause");
return 0;
}
更多追问追答
追问
New.len = len >a.len ? len : a.len;

这行是什么意思来的
追答
把2个多项式中项数比较多的作为新的多项式的项数
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
yyjie999
2013-10-20 · TA获得超过199个赞
知道小有建树答主
回答量:197
采纳率:100%
帮助的人:180万
展开全部
#include <iostream>
#include <sstream>
#include <cmath>
using namespace std;

#define MAX_ITEM 50
class polynomial
{
public:
polynomial(int degree, int coeff[]) {
this->degree = degree;
for (int i = degree; i >= 0; --i) {
this->coeff[i] = coeff[i];
}
}
void output() const {
bool isFirstItem = true;
for (int i = 0; i < item_num(); ++i) {
if (coeff[i]) {
if (!isFirstItem) {
if (coeff[i] > 0)
cout<<"+";
} else {
isFirstItem = false;
}

int cur_degree = degree -i;

if (cur_degree == 0) {
cout<<coeff[i];
} else if (cur_degree ==1) {
if (coeff[i] == 1) {
cout<<"X";
} else if (coeff[i] == -1) {
cout<<"-X";
} else {
cout<<coeff[i]<<"X";
}
} else {
if (coeff[i] == 1) {
cout<<"X^"<<cur_degree;
} else if (coeff[i] == -1) {
cout<<"-X^"<<cur_degree;
} else {
cout<<coeff[i]<<"X^"<<cur_degree;
}
}
}
}
cout<<endl;
}
friend polynomial operator+(const polynomial& p1, const polynomial& p2) {
int arr[MAX_ITEM];
int deg = 0;
if (p1.degree > p2.degree) {
deg = p1.degree;
int pos = p1.degree - p2.degree;

for (int i = 0; i < deg+1; ++i) {
if (i < pos) {
arr[i] = p1.coeff[i];
} else {
arr[i] = p1.coeff[i] + p2.coeff[i-pos];
}
}

} else {
deg = p2.degree;
int pos = p2.degree - p1.degree;
for (int i = 0; i < deg+1; ++i) {
if (i < pos) {
arr[i] = p2.coeff[i];
} else {
arr[i] = p2.coeff[i] + p1.coeff[i-pos];
}
}
}
return polynomial(deg, arr);
}
friend polynomial operator-(const polynomial& p1, const polynomial& p2) {
int arr[MAX_ITEM];
int deg = 0;
if (p1.degree > p2.degree) {
deg = p1.degree;
int pos = p1.degree - p2.degree;

for (int i = 0; i < deg+1; ++i) {
if (i < pos) {
arr[i] = p1.coeff[i];
} else {
arr[i] = p1.coeff[i] - p2.coeff[i-pos];
}
}

} else {
deg = p2.degree;
int pos = p2.degree - p1.degree;
for (int i = 0; i < deg+1; ++i) {
if (i < pos) {
arr[i] = p2.coeff[i];
} else {
arr[i] = p2.coeff[i] - p1.coeff[i-pos];
}
}
}
return polynomial(deg, arr);
}
int item_num() const {
return degree + 1;
}

int f(int X) {
int res = 0;
for (int i = 0; i < degree + 1; ++i) {
res += pow(X, degree-i) * coeff[i];
}
return res;
}
private:
int coeff[MAX_ITEM];
int degree;
};

int main()
{
int degree;
int coeff[MAX_ITEM] = {0};

cout<<"poly1's degree:";
cin>>degree;
if (degree < 0) {cout<<"Error"<<endl; return -1;}
for (int i = 0; i < degree+1; ++i) {
cout<<"\tcoeff:";
cin>>coeff[i];
}

polynomial poly1(degree, coeff);
cout<<"poly1:"<<endl;
poly1.output();

cout<<"poly2's degree:";
cin>>degree;
if (degree < 0) {cout<<"Error"<<endl; return -1;}

for (int i = 0; i < degree+1; ++i) {
cout<<"\tcoeff:";
cin>>coeff[i];
}

polynomial poly2(degree, coeff);
cout<<"poly2:"<<endl;
poly2.output();

polynomial poly3 = poly1 + poly2;
cout<<"poly1 + poly2:"<<endl;
poly3.output();

int X;
cout<<"set the value of X:";
cin>>X;

int res = poly3.f(X);;
cout<<"res: "<<res<<endl;
return 0;
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(1)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式