我也试着写了一个:
#include <iostream.h>
#include <cmath>
class yicihanshu
{public:
yicihanshu(double=0,double=0); //声明带默认参数的构造函数。
yicihanshu addition (yicihanshu &); //声明多项式相加的函数。
void set(); //声明设置一次多项式的系数的函数。
void display(); //声明用于输出的函数。
double addx(double x) {return b*x+c;} //定义给定x计算多项式的值的函数。
void equation(); //声明解一次方程的函数。
~yicihanshu() {cout<<"该多项式已被销毁"<<endl;} //定义析构函数。
protected:
static int count;
double b;
double c;
};
int yicihanshu:: count=0; //对静态数据成员只能在类外进行初始化。
yicihanshu::yicihanshu(double ib,double ic) { b=ib; c=ic; }
yicihanshu yicihanshu:: addition (yicihanshu &y2) //定义多项式相加的函数。
{return yicihanshu(b+y2.b,c+y2.c);}
void yicihanshu::set()
{
cout<<"输入系数b和c:"<<endl;
cin>>b>>c;
}
void yicihanshu::display()
{
if(b==0&&c==0) cout<<0<<endl;
else if(b==0&&c!=0) cout<<c<<endl;
else if(c==0&&b!=0) cout<<b<<"x"<<endl;
else if(c<0&&b!=0) cout<<b<<"x"<<c<<endl;
else cout<<b<<"x+"<<c<<endl;
}
void yicihanshu::equation()
{
if(b==0)
{
if(c==0) cout<<"方程的解为全体实数!"<<endl;
else cout<<"该方程无解!"<<endl;
}
else
cout<<"方程的解为:"<<-c/b<<endl;
}
class ercihanshu : public yicihanshu
{public:
ercihanshu(double=0,double=0,double=0); //声明带默认参数的构造函数。
ercihanshu addition (ercihanshu &); //声明多项式相加的函数。
void set(); //声明设置一次多项式的系数的函数。
void display(); //声明用于输出的函数。
double addx(double); //声明给定x计算多项式的值的函数。
friend int flag(ercihanshu&); //声明判断判别式符号的友元函数。
int equation(); //声明解一次方程的函数。
~ercihanshu() {cout<<"该多项式已被销毁"<<endl;} //定义析构函数。
private:
double a;
};
ercihanshu::ercihanshu(double ia,double ib,double ic) :yicihanshu(ib,ic),a(ia){}
ercihanshu ercihanshu:: addition (ercihanshu &y2) //定义多项式相加的函数。注意到ax^2+bx+c=(ax+b)x+c !
{
return ercihanshu(a+y2.a,b+y2.b,c+y2.c);
}
void ercihanshu::set()
{
cout<<"输入系数a、b和c:"<<endl;
cin>>a>>b>>c;
}
void ercihanshu::display()
{
if(a==0&&b==0&&c==0) cout<<0<<endl;
else if(a==0&&b==0&&c!=0) cout<<c<<endl;
else if(a==0&&b!=0&&c==0) cout<<b<<"x"<<endl;
else if(a!=0&&b==0&&c==0) cout<<a<<"x^2"<<endl;
else if(a==0&&b!=0&&c!=0)
{
if(c<0) cout<<b<<"x"<<c<<endl;
else cout<<b<<"x+"<<c<<endl;
}
else if(a!=0&&b==0&&c!=0)
{
if(c<0) cout<<a<<"x^2"<<c<<endl;
else cout<<a<<"x^2+"<<c<<endl;
}
else if(a!=0&&b!=0&&c==0)
{
if(b<0) cout<<a<<"x^2"<<b<<"x"<<endl;
else cout<<a<<"x^2+"<<b<<"x"<<endl;
}
else
{
if(b<0&&c<0) cout<<a<<"x^2"<<b<<"x"<<c<<endl;
else if(b>0&&c<0) cout<<a<<"x^2+"<<b<<"x"<<c<<endl;
else if(b<0&&c>0) cout<<a<<"x^2"<<b<<"x+"<<c<<endl;
else cout<<a<<"x^2+"<<b<<"x+"<<c<<endl;
}
}
double ercihanshu::addx(double x)
{
return a*x*x+b*x+c;
}
int flag(ercihanshu& y)
{
double a1,b1,c1,d;
a1=y.a;b1=y.b;c1=y.c;
d=b1*b1-4*a1*c1;
if(d>0) return 1;
else if(d==0) return 0;
else return -1;
}
int ercihanshu::equation()
{
if(a==0)
{
yicihanshu::equation();
return 0;//返回0以表示a为0
}
else
{
double d,e;
d=-b/(2*a);
e=sqrt(b*b-4*a*c)/(2*a);
if(flag(*this)==1)
cout<<"两个不同实数解分别为:"<<endl<<d+e<<","<<d-e<<endl;
if(flag(*this)==0)
cout<<"两个相同实数解为:"<<endl<<d<<endl;
if(flag(*this)==-1)
cout<<"该方程无实数解!"<<endl;
}
}
int main()
{ercihanshu e1;
cout<<"e1:";e1.display();
e1.equation();
e1.set();
cout<<"e1:";e1.display();
e1.equation();
return 0;
}
温馨提示:内容为网友见解,仅供参考