c++定义一个有理数类,实现有理数的加减乘除以及化简成最简分数等怎么编码?
发布网友
发布时间:2022-05-19 07:55
我来回答
共1个回答
热心网友
时间:2023-10-09 02:53
看看这个:
#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;
}