class polynomial{
friend polynomial operator +(const polynomial &p1,const polynomial &p2);
friend polynomial operator -(const polynomial &p1,const polynomial &p2);
friend polynomial operator *(const polynomial &p1,const polynomial &p2);
friend void priIAndc(unsigned int element,double value);
friend void view(polynomial &);
friend void viewAll(polynomial *p);
private:
double coef[30];
unsigned int degree;
public:
//构造函数
polynomial(double c=0.0,unsigned int exponent=0);
//常成员函数
void clear();
void add_to_coef(); //添加项式
unsigned int pdegree()const{return degree;}
polynomial derivation()const; //求导
polynomial diff()const;
double operator ()(double x);
double operator [](int i);
};
--------------------------------------------------------------------------------
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include <iostream.h>
#include "ploy.h"
polynomial::polynomial(double c,unsigned int exponent)
{
clear();
for(unsigned int i=0;i<=exponent;i++)
coef[i]=c;
degree=exponent;
}
//输出多项式时必须考虑多项式系数为正、为0、和为1的情况
void priIAndc(unsigned int element,double value)
{
if(value) //系数不是"0"
{
if(element) //指数不是"0"
{
if(value==1) //系数是"1",不用输出
cout<<"";
else if(value==-1) //系数是"-1",输出"-"
cout<<"-";
else cout<<value;
cout<<"X";
if(element==1) //指数是1的话,就不用输出"x^1"了
cout<<"";
else cout<<"^"<<element;
}
else
cout<<value;
}
}
void view(polynomial &p)
{
for(int i=p.degree;i>=0;i--)
{
priIAndc(i,p[i]);
if(i)
if(p[i-1]>0)
cout<<"+";
}
cout<<endl;
}
void viewAll(polynomial *p)
{
cout<<"p[0]=";
view(*p);
p++;
cout<<"p[1]=";
view(*p);
}
//////////
polynomial operator +(const polynomial &p1,const polynomial &p2)
{
polynomial np;
cout<<"p1+p2=";
np.degree=((p1.degree>=p2.degree)?p1.degree:p2.degree);
for(unsigned int i=0;i<=np.degree;i++)
np.coef[i]=p1.coef[i]+p2.coef[i];
return np;
}
polynomial operator -(const polynomial &p1,const polynomial &p2)
{
polynomial np;
cout<<"p1-p2=";
np.degree=((p1.degree>=p2.degree)?p1.degree:p2.degree);
for(unsigned int i=0;i<=np.degree;i++)
np.coef[i]=p1.coef[i]-p2.coef[i];
return np;
}
double polynomial::operator [](int i)
{
i=i<29?i:29;
return this->coef[i];
}
polynomial operator *(const polynomial &p1,const polynomial &p2)
{
polynomial tmp;
cout<<"p1*p2=";
tmp.degree=p1.degree+p2.degree;
if(tmp.degree>29)
cout<<"The sum of (p1,p2) must <=29 error!";
for(unsigned int i=0;i<=tmp.degree;i++)
tmp.coef[i]=0;
for(i=0;i<=p1.degree;i++)
for(unsigned int j=0;j<=p2.degree;j++)
tmp.coef[i+j]+=p1.coef[i]*p2.coef[j];
return tmp;
}
//清空多项式
void polynomial::clear()
{
for(unsigned int i=0;i<=this->degree;i++)
this->coef[i]=0;
this->degree=0;
}
//给多项式添加项
void polynomial::add_to_coef()
{
unsigned int newindex;
double newcoef;
char endchar='a';
cout<<"输入指数和系数"<<endl;
while(endchar!='y')
{
cin>>newindex>>newcoef;
cout<<"exit input?(y/n)";
cin>>endchar;
degree=(degree>=newindex)?degree:newindex;
//调整最大项指数
this->degree%=29;
this->coef[newindex]+=newcoef;
}
}
double polynomial::operator ()(double x)
{
double sum=0.0;
double tmp=1/x;
for(unsigned int i=0;i<=this->degree;i++)
{
tmp=x*tmp;
sum+=this->coef[i]*tmp;
}
cout<<"f("<<x<<")="<<sum<<endl;
return sum;
}
polynomial polynomial::derivation() const
{
polynomial np;
np.clear();
np.degree=this->degree-1;
for(unsigned int i=0;i<=np.degree;i++)
np.coef[i]=(i+1)*this->coef[i+1];
return np;
}
polynomial polynomial::diff() const
{
polynomial np;
np.clear();
np.degree=this->degree+1;
for(unsigned int i=1;i<=np.degree;i++)
np.coef[i]=this->coef[i-1]/i;
np.coef[0]=1;
return np;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
--------------------------------------------------------------------------------
//main.cpp
#include "ploy.h"
#include <iostream>
///////////////////////////////////
void dispMenu(); //输出菜单
char get_command(); //返回命令
unsigned int set_current(); //设置当前多项式
void dispHelp();
////////////////////////////////////////
#include <iostream>
#include <string.h>
#ifndef DEBUG
#define DEBUG
#define MEUN_NUM 15 //Menu 的元素个数
using namespace std;
const char *Menu[]={"\n-----------菜单----------",
"",
" h-显示程序帮助文件",
" s-设置当前多项式",
" 1-调用add_to_coef函数",
" c-调用clear函数",
" v-显示当前多项式",
" a-显示所有多项式",
" d-对当前多项式求导并显示",
" j-对当前多项式积分并显示",
" e-用()操作符计算当前多项式的值",
" +-显示A+B的和",
" -- 显示A-B的差",
" *-显示A*B的积",
" q-退出"};
void dispMenu(){
char str[200];
for(int i=0;i<MEUN_NUM;i++){
strcpy(str,Menu[i]);
cout<<" "<<str<<endl;
}
}
void dispHelp(){
system("cls");
cout<<"*************************************************************************\n"
<<"* 这是我的第二份c++作业,对多项式进行各种运算\n"
<<"* 程序在初始化时创建了两个多项式!\n"
<<"* 程序的功能包括设置当前多项式修、改多项式的某项系数\n"
<<"* 多项式加(+)、减(-)、乘(*)运算、积分和求导运算\n"
<<"* 多项式赋值求值计算"<<endl
<<"* 菜单前面的字母代表相应的操作\n"
<<"* 谢谢使用本程序,若在使用中发现什么漏洞,请发送邮件到yzx_xue@163.com\n"
<<"**************************************************************************\n";
cout<<"Press any key to countine...";
cin.get();
}
char get_command(){
char command;
cin>>command;
return command;
}
unsigned int set_current(){
unsigned int set_current;
cout<<"请输入(1,2)设置为当前多项式!";
cin>>set_current;
return ((set_current>0&&set_current<3)?set_current-1:0);
}
/////////////////////////////////////////
using namespace std;
void main()
{
polynomial p1(2.0,10),p2(-2.0,5);
polynomial p[2]={p1,p2};
unsigned int current_index=0;
char command;
dispHelp();
do
{
dispMenu();
command=get_command();
switch(command)
{
case '1':
view(p[current_index]);
p[current_index].add_to_coef();
break;
case 's':
current_index=set_current();
break;
case 'v':
view(p[current_index]);
break;
case 'a':
viewAll(p);
break;
case 'd':
view(p[current_index]);
cout<<"导数=";
view(p[current_index].derivation());
break;
case 'j':
view(p[current_index]);
cout<<"积分=";
view(p[current_index].diff());
break;
case 'c':
view(p[current_index]);
p[current_index].clear();
break;
case 'e':
{
view(p[current_index]);
double x;
cout<<"输入x"<<endl;
cin>>x;
p[current_index](x);
}
break;
case 'h':
dispHelp();
break;
case '+':
viewAll(p);
view(p[0]+p[1]);
break;
case '-':
viewAll(p);
view(p[0]-p[1]);
break;
case '*':
viewAll(p);
view(p[0]*p[1]);
break;
case 'q': //Do nothing.
break;
default: cout<<"Invalid command."<<endl;
break;
}//end of switch
} while(command!='q'); //end of do
}//end of main()
#endif
(多项式加\减\相乘\求导程序)
参考资料:http://blog.csdn.net/yzxtc/archive/2006/04/23/673962.aspx?P_AVPASS=PHDGBITAVPASST