题目
C语言设计实现抽象数据类型"有理数"基本操作包括有理数的加法,减法,乘法,除法,以及求有理数的分子,分
最后是分母
最后是分母
提问时间:2021-01-09
答案
有理数即分数,
分数的数据类型是很简单的.
另外,求有理数的分子,分母的问题,因为给的有理数只能是有限小数,所以根本没有疑问.
但是如果改成:
求分子分母之和最小的,在一定误差范围内的分数,这个问题才有价值
比如:
0.3333333,如果误差为1e-4
那么1/3明显要比333333/10000000
下面给出一个实现(C++):
struct RatNum
{
int a,b;
static int GCD(int a,int b)
{
if(a<0)a=-a;
if(b<0)b=-b;
if(a==0)return b;
if(b==0)return a;
if(a<b)
{
int c=a;
a=b;
b=c;
}
unsigned int c;
while(c= a % b)
{
a = b ;
b = c;
}
return b;
}
explicit RatNum(int aa=0,int bb=1)
:a(aa),b(bb)
{
}
explicit RatNum(double num,double esp=0.00000001)
{
if(esp<0)esp=-esp;
for(b=1;;b++)
{
a=b*num+.5;
double k=a-b*num;
if(k<0)k=-k;
if(k<esp)break;
}
}
void norm()//约分
{
int c=GCD(a,b);
a/=c;
b/=c;
if(b<0)
{
a=-a;
b=-b;
}
}
RatNum operator/(const RatNum& f)const
{
RatNum d=*this;
d.a*=f.b;
d.b*=f.a;
return d;
}
RatNum operator-()const
{
RatNum ret=*this;
ret.a=-ret.a;
return ret;
}
void operator*=(const RatNum& f)
{
a*=f.a;
b*=f.b;
norm();
}
RatNum operator*(const RatNum& f)const
{
RatNum d=*this;
d*=f;
return d;
}
RatNum operator*(int q)const
{
RatNum d=*this;
d.a*=q;
return d;
}
void operator-=(const RatNum& f)
{
int bb=b;
a*=f.b;
b*=f.b;
a-=f.a*bb;
}
void operator+=(const RatNum& f)
{
int bb=b;
a*=f.b;
b*=f.b;
a+=f.a*bb;
}
void positive()
{
if (a<0)a=-a;
}
bool operator==(int n)
{
return a==n*b;
}
void operator=(int q)
{
a=q;
b=1;
}
};
分数的数据类型是很简单的.
另外,求有理数的分子,分母的问题,因为给的有理数只能是有限小数,所以根本没有疑问.
但是如果改成:
求分子分母之和最小的,在一定误差范围内的分数,这个问题才有价值
比如:
0.3333333,如果误差为1e-4
那么1/3明显要比333333/10000000
下面给出一个实现(C++):
struct RatNum
{
int a,b;
static int GCD(int a,int b)
{
if(a<0)a=-a;
if(b<0)b=-b;
if(a==0)return b;
if(b==0)return a;
if(a<b)
{
int c=a;
a=b;
b=c;
}
unsigned int c;
while(c= a % b)
{
a = b ;
b = c;
}
return b;
}
explicit RatNum(int aa=0,int bb=1)
:a(aa),b(bb)
{
}
explicit RatNum(double num,double esp=0.00000001)
{
if(esp<0)esp=-esp;
for(b=1;;b++)
{
a=b*num+.5;
double k=a-b*num;
if(k<0)k=-k;
if(k<esp)break;
}
}
void norm()//约分
{
int c=GCD(a,b);
a/=c;
b/=c;
if(b<0)
{
a=-a;
b=-b;
}
}
RatNum operator/(const RatNum& f)const
{
RatNum d=*this;
d.a*=f.b;
d.b*=f.a;
return d;
}
RatNum operator-()const
{
RatNum ret=*this;
ret.a=-ret.a;
return ret;
}
void operator*=(const RatNum& f)
{
a*=f.a;
b*=f.b;
norm();
}
RatNum operator*(const RatNum& f)const
{
RatNum d=*this;
d*=f;
return d;
}
RatNum operator*(int q)const
{
RatNum d=*this;
d.a*=q;
return d;
}
void operator-=(const RatNum& f)
{
int bb=b;
a*=f.b;
b*=f.b;
a-=f.a*bb;
}
void operator+=(const RatNum& f)
{
int bb=b;
a*=f.b;
b*=f.b;
a+=f.a*bb;
}
void positive()
{
if (a<0)a=-a;
}
bool operator==(int n)
{
return a==n*b;
}
void operator=(int q)
{
a=q;
b=1;
}
};
举一反三
已知函数f(x)=x,g(x)=alnx,a∈R.若曲线y=f(x)与曲线y=g(x)相交,且在交点处有相同的切线,求a的值和该切线方程.
我想写一篇关于奥巴马的演讲的文章,写哪一篇好呢?为什么好
奥巴马演讲不用看稿子.为什么中国领导演讲要看?
想找英语初三上学期的首字母填空练习……
英语翻译
最新试题
- 1谁帮我举一个380字左右的关于动物里母爱的故事?
- 2李生论善学者
- 3直线y=2x+2与抛物线y=x2+3x的交点坐标为_.
- 4It is difficult to find-______in a big city A.works B.a work C.job D.jobs
- 5在三角形abc中,已知内角a=派/3,边bc=2根号下3,设内角b=x,周长为y.
- 6写一句古诗词:含有山河草木或含有花鸟虫鱼四个字
- 7我国对电压允许偏差和频率允许偏差是如何规定的?
- 8原子是一个可再分割的实心球体.这句话对吗?为什么?
- 9如何测定锅炉水的氯离子
- 10He seemed very detached from what was going on.
热门考点
- 14x+5y=3.5 x-1=1.8-x-6y解这个方程组
- 2什么动物走路不长眼睛的
- 3文言文 节选自纪昀《阅微草堂笔记》
- 4已知奇函数f(x)当x>0时f(x)=|lnx|,则函数y=f(x)-sinx的零点个数
- 5年增长率0.1.10年后,我国国民生产总值与现在相比增加了多少百分比.帮我列下式,增长了多少百分比我不理解,什么百分比啊?搞得我头痛.
- 6为什么基因的表达和蛋白质有关
- 7大王怎么说齐国没有人呢?(改为双重复定句)
- 8伯努利定律的具体内容什么
- 9Soon,the popularity of basketball rose worldwide.翻译
- 10紧急!他笑着对我说:“过来,让我看看你的马草.”(改为转述句)