一道C++题求答案。急!在线等。

设计一次多项式bx+c的抽象数据类型,起名为yicihanshu,以上类型的数据部分分为两个浮点型系数项b和c与一个静态整形数据count,count记录该类的对象个数,操作部分为:
1、 初始化浮点型数据成员,b、c和count,每个数据成员的默认值为0。
2、 做两个多项式加法,即使对应的系数相加,并返回相加的结果。
3 、根据给定x的值计算多项式的值。
4、 计算方程bx+c=0的根。要求考虑b=0时的情况。
5 、按照bx+c的格式输出一次多项式,在输出时要注意去掉系数为0的项,并且当c的值为负时,其前不能出现加号。
6、调用析构函数时输出该函数并输出字符串“该多项式已被销毁”

设计以该类为基类的派生类,二次多项式ax^2+bx+c,假定起名为ercihanshu,
1 、初始化浮点型数据成员a,b和c与一个静态整形数据count,每个数据成员的默认值为0。
2 、重新定义加法函数,返回相加的结果。要求调用基类同名函数反回值。
3 、重新定义计算函数,根据给定x的值计算多项式的值。要求调用基类同名函数反回值。
4 、重新定义解方程函数,求ax^2+bx+c=0的两个实数根,对于有实根、无实根和不是实根方程(即a==0)这三种情况要返回不同的整数值,以便于工作调用函数做不同的处理。
5 、重新定义输出函数,按照ax^2+bx+c的格式(x2用x^2表示)输出二次多项式,在输出时要注意去掉系数为0的项,并且当b和c的值为负时,其前不能出现加号。
6、调用析构函数时输出该函数并输出字符串“该多项式已被销毁”

定义一个ercihanshu类的友元函数,判断其判别式 =b^2-4ac的符号并返回(1为正,0为0,-1为负),并为增加程序可读性,在解方程函数中调用该函数。

在main()函数中分别实施上述过程,并在初始化yicihanshu的对象时使用ercihanshu的对象赋值。

我也试着写了一个:
#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;
}
温馨提示:内容为网友见解,仅供参考
第1个回答  2010-04-19
我知道如何写,但是我忘记了求根的公式,数学不是很好,很久没用,忘记了,这个题目我想了2分钟,基本知道如何做,刚写的时候发现忘记求根的公式,那些条件什么的都忘记了,你发去我的空间或什么地方告诉我一下,我就可以帮你。
第2个回答  2010-04-19
如果不急,后天答复,a464982370@qq.com
第3个回答  2010-04-20
===========================================================
你这要求可真不少 不过总算是完成了 累死我了
绝对原创哦!!!本人测试过了 完全符合你的要求
===========================================================
代码有点长 慢慢看哦 又不懂的再问
长就是因为你要求太多了 呵呵
===========================================================
#include<iostream>
#include<cmath>
using namespace std;
class yicihanshu//一次函数
{
private:
float b,c;
static int count;
public:
yicihanshu(float b1=0,float c1=0):b(b1),c(c1)//构造函数
{
count++;
}
float *coeadd(yicihanshu a=0)//系数相加
{
float *p;
p=new float[2];
p[0]=a.b+b;
p[1]=a.c+c;
return p;
}
float tox(float x)//带入X求值
{
float sum;
sum=b*x+c;
return sum;
}
float root()//求根
{
float sum;
if(b==0)
return c;
else
{
sum=-c/b;
return sum;
}
}
void display()//显示式子
{
if(b==0)
{
cout<<c<<endl;
}
else if(b==1)
{
if(c==0)
cout<<"x"<<endl;
else if(c<0)
cout<<"x"<<c<<endl;
else
cout<<"x+"<<c<<endl;
}
else
{
if(c==0)
cout<<b<<"x"<<endl;
else if(c<0)
cout<<b<<"x"<<c<<endl;
else
cout<<b<<"x+"<<c<<endl;
}
}
~yicihanshu()//析构函数
{
cout<<"该多项式已被销毁"<<endl;
}
};
int yicihanshu::count=0;//初始化基类静态变量

/*基类和派生类成员函数名相同 功能也一样*/
class ercihanshu:public yicihanshu//二次函数
{
private:
float a,b,c;
static int count;
public:
ercihanshu(float a1=2.0,float b1=0,float c1=0):yicihanshu(b1,c1),a(a1),b(b1),c(c1)
{
count++;
}
friend int D(ercihanshu);//友元函数
float *coeadd(ercihanshu e=0)
{
float *p;
p=new float[3];
p[0]=e.a+a;
p[1]=e.yicihanshu::coeadd()[0]+yicihanshu::coeadd()[0];
p[2]=e.yicihanshu::coeadd()[1]+yicihanshu::coeadd()[1];
return p;
}
float tox(float x)
{
float sum;
sum=a*x*x+yicihanshu::tox(x);
return sum;
}
float **root(ercihanshu e)//由于有友元函数 必须输入一个对象 与基类函数有区别
{
float **p;
*p=new float[2];
for(int i=0;i<2;i++)
p[i]=new float[2];
if(a==0)
{
p[0][0]=1;
p[0][1]=yicihanshu::root();
p[1][0]=0;
return p;
}
else
{
switch(D(e))
{
case 1:p[0][0]=1;p[0][1]=(-b+(float) sqrt((float) b*b-4*a*c))/2*a;p[1][0]=1;p[1][1]=(-b-(float )sqrt((float) b*b-4*a*c))/2*a;return p;
case 0:p[0][0]=1;p[1][0]=1;p[0][1]=-b/2*a;p[1][1]=-b/2*a;return p;
case -1:p[0][0]=p[1][0]=0;return p;
}
}
}
void display()
{
if(a==0)
yicihanshu::display();
else if(a==1)
{
if(b=0)
{
if(c>=0)
cout<<"x^2+"<<c<<endl;
else if(c<0)
cout<<"x^2-"<<c<<endl;
}
else if(b>0)
{
cout<<"x^2+";
yicihanshu::display();
}
else
{
cout<<"x^-";
yicihanshu::display();
}
}
else
{
if(b=0)
{
if(c>=0)
cout<<a<<"x^2+"<<c<<endl;
else if(c<0)
cout<<a<<"x^2-"<<c<<endl;
}
else if(b>0)
{
cout<<a<<"x^2+";
yicihanshu::display();
}
else
{
cout<<a<<"x^-";
yicihanshu::display();
}
}
}

~ercihanshu()
{
cout<<"该多项式已被销毁"<<endl;
}

};
int ercihanshu::count=0;
int D(ercihanshu e)//友元函数 定义
{
if(e.b*e.b-4*e.a*e.c>0)
return 1;
else if(e.b*e.b-4*e.a*e.c==0)
return 0;
else
return -1;
}
=================================================
测试主程序
=================================================
void main()
{

yicihanshu b(2,5);
yicihanshu c(1,4);
ercihanshu d(2,3,4);
cout<<b.coeadd()[0]<<b.coeadd()[1]<<endl;
d.display();
system("pause");
};
===================================================
备注 :这代码中
4 、重新定义解方程函数,求ax^2+bx+c=0的两个实数根,对于有实根、无实根和不是实根方程(即a==0)这三种情况要返回不同的整数值,以便于工作调用函数做不同的处理。

我做了一个2维数组

p[0][0] p[1][0] 意义
0 0 无实根
1 1 有实根
1 0 a=0
======================================================
希望你满意 这可花费了我不少时间哦
希望我这一大段代码 没白打
第4个回答  2010-04-19
111111111111111111111111111111111111111111111111111111
相似回答