c++定义一个有理数类,实现有理数的加减乘除以及化简成最简分数等怎么编码?

 我来答
笃侠6A
2018-01-10 · TA获得超过3734个赞
知道大有可为答主
回答量:3205
采纳率:75%
帮助的人:3230万
展开全部
看看这个:
#include<iostream>
using namespace std;

class Rationalnumber; //提前声明
ostream& operator<<(ostream& out,Rationalnumber& ofs);

class Rationalnumber //分数
{
int fz,fm; //分子,分母
int Gcd(); //求fz和fm的最大公约数
public:
Rationalnumber(); //无参构造函数,创建1/1对象
Rationalnumber(int pfz,int pfm=1);//pfz为分子,pfm为分母
void Set(int pfz,int pfm); //设置值:pfz为分子,pfm为分母
void Display(); //输出形如"x/y"的分数
void Division();//约分
double ToDec(); //化为小数

Rationalnumber operator+(Rationalnumber& ofs);// 重载+
Rationalnumber operator-(Rationalnumber& ofs);// 重载-
Rationalnumber operator*(Rationalnumber& ofs);// 重载*
Rationalnumber operator/(Rationalnumber& ofs);// 重载/

Rationalnumber& operator+=(Rationalnumber& ofs);
Rationalnumber& operator-=(Rationalnumber& ofs);
Rationalnumber& operator*=(Rationalnumber& ofs);
Rationalnumber& operator/=(Rationalnumber& ofs);

friend ostream& operator<<(ostream& out,Rationalnumber& ofs);

}; //End Of class Rationalnumber

int Rationalnumber::Gcd(){
int x=abs(fz),y=abs(fm),r;
r=x % y;
while( r!=0 ){
x=y; y=r; r=x % y;
}
return y;
}

Rationalnumber::Rationalnumber(){ //无参构造函数,创建1/1对象
fz=1; fm=1;
}

Rationalnumber::Rationalnumber(int pfz,int pfm){//pfz为分子,pfm为分母
fz=pfz; fm=pfm;
}

void Rationalnumber::Set(int pfz,int pfm){ //设置值:pfz为分子,pfm为分母
fz=pfz; fm=pfm;
Division(); //约分
}

void Rationalnumber::Display(){//输出形如"x/y"的分数
cout <<fz <<'/' <<fm <<endl;
}

void Rationalnumber::Division(){//约分
int t=Gcd();
fz/=t; fm/=t;
if(fm<0) { fz=-fz; fm=-fm; } //使负号在分子上
}

double Rationalnumber::ToDec(){ //化为小数
return double(fz)/fm;
}

Rationalnumber Rationalnumber::operator+(Rationalnumber& ofs){ //重载+
Rationalnumber t;
t.fz=fz*ofs.fm+ofs.fz*fm;
t.fm=fm*ofs.fm;
t.Division(); //约分
return t;
}

Rationalnumber Rationalnumber::operator-(Rationalnumber& ofs){ //重载-
Rationalnumber t;
t.fz=fz*ofs.fm-ofs.fz*fm;
t.fm=fm*ofs.fm;
t.Division(); //约分
return t;
}

Rationalnumber Rationalnumber::operator*(Rationalnumber& ofs){ //重载*
Rationalnumber t;
t.fz=fz*ofs.fz; t.fm=fm*ofs.fm;
t.Division(); //约分
return t;
}

Rationalnumber Rationalnumber::operator/(Rationalnumber& ofs){ //重载/
Rationalnumber t;
t.fz=fz*ofs.fm; t.fm=fm*ofs.fz;
t.Division(); //约分
return t;
}

Rationalnumber& Rationalnumber::operator+=(Rationalnumber& ofs){
Rationalnumber t(*this);
fz=t.fz*ofs.fm+ofs.fz*t.fm;
fm=t.fm*ofs.fm;
Division(); //约分
return *this;
}

Rationalnumber& Rationalnumber::operator-=(Rationalnumber& ofs){
Rationalnumber t(*this);
fz=t.fz*ofs.fm-ofs.fz*t.fm;
fm=t.fm*ofs.fm;
Division(); //约分
return *this;
}

Rationalnumber& Rationalnumber::operator*=(Rationalnumber& ofs){
Rationalnumber t(*this);
fz=t.fz*ofs.fz; fm=t.fm*ofs.fm;
Division(); //约分
return *this;
}

Rationalnumber& Rationalnumber::operator/=(Rationalnumber& ofs){
Rationalnumber t(*this);
fz=t.fz*ofs.fm; fm=t.fm*ofs.fz;
Division(); //约分
return *this;
}

ostream& operator<<(ostream& out,Rationalnumber& ofs){
ofs.Display();
return out;
}

void main()
{
Rationalnumber fs1(1,2),fs2(1,3),fs3,fs4,fs5,fs6;
fs3=fs1+fs2;
fs4=fs1-fs2;
fs5=fs1*fs2;
fs6=fs1/fs2;
cout <<"fs1=" <<fs1;
cout <<"fs2=" <<fs2;
cout <<"fs3=fs1+fs2=" <<fs3;
cout <<"fs4=fs1-fs2=" <<fs4;
cout <<"fs5=fs1*fs2=" <<fs5;
cout <<"fs6=fs1/fs2=" <<fs6;
}
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式