数据结构(续)
来源:互联网 发布:昆明暴恐 不反抗 知乎 编辑:程序博客网 时间:2024/06/07 10:48
抽象数据类型ADT:
ADT parameter
{数据:
double a,b,c;
操作:
parameter lesson(double al,double bl,double cl);//初始化成员函数
parameter plus(double m,double n,double l);//实现两个二项式相加
double value(double x,parameter T);//给定x求各二项式的值
double root1(parameter T);//求该二项式组成的方程的根
double root2(parameter T);
void output(pamameter T);//输出指定的格式
}
c++进行的实现
#include<iostream>
#include<cmath>
using namespace std;
class parameter //用c++中的类来实现
{
public:
parameter(double al=0,double bl=0,double cl=0){a=al;b=bl;c=cl;}//数据成员的初始化
parameter plus(parameter T); //两个多项式相加
double value(parameter T); //给定x求多项式的值
void root1(parameter T); //求该多项式组成的方程的根
void output(parameter T); //按指定格式进行输出
private: //数据部分
double a;
double b;
double c;
};
parameter parameter::plus(parameter T) //多项式相加的具体实现
{
return parameter(a+T.a,b+T.b,c+T.c);
}
double parameter::value(parameter T) //根据输入的x对多项式求值
{
int x;
cout<<"please input the value of x:"<<endl;
cin>>x;
return T.a*x*x+T.b*x+T.c;
}
void parameter::root1(parameter T) //求根
{
double x1,x2;
if(T.a==0){x1=(-T.c/T.b);x2=x1;} //当a=0时,方程只有一个解
else if((T.b*T.b-4*T.a*T.c)<0){cout<<"方程没有实数解"<<endl;exit(1);}
else if((T.b*T.b-4*T.a*T.c)>=0)
{x1=(-T.b+sqrt(T.b*T.b-4*T.a*T.c))/(2*T.a);x2=(-T.b-sqrt(T.b*T.b-4*T.a*T.c))/(2*T.a);
cout<<"方程的根为:"<<x1<<"和"<<x2<<endl;
}
}
//此处省略了第二个root2
void parameter::output(parameter T) //按要求输出
{
cout<<T.a<<"x**2+"<<T.b<<"x+"<<T.c<<endl;
}
void main() //进行检验的主函数
{
parameter c1(1.1,2.1,1.0),c2(2.0,3.0,4.0),c3;
cout<<c1.value(c1)<<endl;
c1.root1(c1);
c1.output(c1);
cout<<endl;
c3=c1.plus(c2);
c3.output(c3);
#include<cmath>
using namespace std;
class parameter //用c++中的类来实现
{
public:
parameter(double al=0,double bl=0,double cl=0){a=al;b=bl;c=cl;}//数据成员的初始化
parameter plus(parameter T); //两个多项式相加
double value(parameter T); //给定x求多项式的值
void root1(parameter T); //求该多项式组成的方程的根
void output(parameter T); //按指定格式进行输出
private: //数据部分
double a;
double b;
double c;
};
parameter parameter::plus(parameter T) //多项式相加的具体实现
{
return parameter(a+T.a,b+T.b,c+T.c);
}
double parameter::value(parameter T) //根据输入的x对多项式求值
{
int x;
cout<<"please input the value of x:"<<endl;
cin>>x;
return T.a*x*x+T.b*x+T.c;
}
void parameter::root1(parameter T) //求根
{
double x1,x2;
if(T.a==0){x1=(-T.c/T.b);x2=x1;} //当a=0时,方程只有一个解
else if((T.b*T.b-4*T.a*T.c)<0){cout<<"方程没有实数解"<<endl;exit(1);}
else if((T.b*T.b-4*T.a*T.c)>=0)
{x1=(-T.b+sqrt(T.b*T.b-4*T.a*T.c))/(2*T.a);x2=(-T.b-sqrt(T.b*T.b-4*T.a*T.c))/(2*T.a);
cout<<"方程的根为:"<<x1<<"和"<<x2<<endl;
}
}
//此处省略了第二个root2
void parameter::output(parameter T) //按要求输出
{
cout<<T.a<<"x**2+"<<T.b<<"x+"<<T.c<<endl;
}
void main() //进行检验的主函数
{
parameter c1(1.1,2.1,1.0),c2(2.0,3.0,4.0),c3;
cout<<c1.value(c1)<<endl;
c1.root1(c1);
c1.output(c1);
cout<<endl;
c3=c1.plus(c2);
c3.output(c3);
}
- 数据结构(续)
- 数据结构(续2)
- 数据结构(续3)
- 数据结构--排序(续)
- SEH相关数据结构(续)
- 数据结构之一(基本数据结构)
- 数据结构之二(续,高级hash)
- 数据结构之三大查找(续)
- Java数据结构和算法-数组(续)
- [数据结构]2010数据结构期中考试(c++)
- 01数据结构概述(郝斌数据结构)
- 数据结构一:数据结构概论(第一周)
- 数据结构学习笔记(1) 数据结构概论
- 《数据结构及应用》总结(1)------数据结构
- 数据结构看书笔记(一)--数据结构简介
- 数据结构(1)——大话数据结构
- 数据结构(2)——大话数据结构
- JavaScript数据结构(1):什么是数据结构
- C/S v.s. B/S
- 网上盛传的几个J2EE视频教程的地址
- 关于本机木马检测(转载)的命令
- C++语言编程基础教程日志-第一章--编程逻辑和技术
- Get HWND of the WPF Window
- 数据结构(续)
- 开始WSN旅程
- 计算器制作JAVA版(第二步,表达式结构检查)
- Test
- Themida.v1.9.5.0.XXX by shoooo
- sscanf的一些用法
- error LNK2001: unresolved external symbol VC++链接错误详解
- 用脑过度了头痛了两天
- 用 eclipse wtp 新建 ejb (2.1) 的问题。