编写C++语言程序通过运算符重载,实现分数的有关运算(加减乘除,大小比较,输入输出)
答案:2 悬赏:0 手机版
解决时间 2021-03-16 02:23
- 提问者网友:藍了天白赴美
- 2021-03-15 02:34
编写C++语言程序通过运算符重载,实现分数的有关运算(加减乘除,大小比较,输入输出)
最佳答案
- 五星知识达人网友:走死在岁月里
- 2021-03-15 04:02
#include
using namespace std;
class Fraction;
istream& operator>>(istream&,Fraction&);
ostream& operator<<(ostream&,Fraction&);
class Fraction{
int nume;
int deno;
public:
friend istream& operator>>(istream&,Fraction&);
friend ostream& operator<<(ostream&,Fraction&);
void FracSimp()
{
int m,n,r;
m=nume;n=deno;
r=m%n;
while(r)
{
m=n;n=r;
r=m%n;
}
if(n!=1)
{
nume/=n;
deno /=n;
}
}
Fraction():nume(0),deno(1){}
Fraction(int n,int d):nume(n),deno(d){FracSimp();}
void InNume(int n){nume=n;}
void InDeno(int d){deno=d;}
int GetNume(){return nume;}
int GetDeno(){return deno;}
Fraction operator+(Fraction&x)
{
Fraction c;
c.nume=nume*x.deno+x.nume*deno;
c.deno=deno*x.deno ;
return c;
}
bool operator==(Fraction& x)
{
return nume*x.deno==x.nume*deno;
}
Fraction& operator++()
{
nume+=deno;
return *this;
}
Fraction operator++(int)
{
Fraction y;
y=*this;
nume+=deno;
return y;
}
bool operator>(Fraction& x)
{
return nume*x.deno>deno*x.nume;
}
};
istream& operator>>(istream& istr,Fraction& x)
{
char ch;
cout<<"Input a fraction:";
istr>>x.nume>>ch>>x.deno;
if(x.deno ==0)
{
cerr<<"分母不得为零!"<
exit(1);
}
x.FracSimp ();
return istr;
}
ostream& operator<<(ostream& ostr,Fraction& x)
{
cout<
return ostr;
}
int main(void)
{
Fraction a(5,8),b,c;
cin>>b;
b++;
c=a+b;
cout return 0;
}
using namespace std;
class Fraction;
istream& operator>>(istream&,Fraction&);
ostream& operator<<(ostream&,Fraction&);
class Fraction{
int nume;
int deno;
public:
friend istream& operator>>(istream&,Fraction&);
friend ostream& operator<<(ostream&,Fraction&);
void FracSimp()
{
int m,n,r;
m=nume;n=deno;
r=m%n;
while(r)
{
m=n;n=r;
r=m%n;
}
if(n!=1)
{
nume/=n;
deno /=n;
}
}
Fraction():nume(0),deno(1){}
Fraction(int n,int d):nume(n),deno(d){FracSimp();}
void InNume(int n){nume=n;}
void InDeno(int d){deno=d;}
int GetNume(){return nume;}
int GetDeno(){return deno;}
Fraction operator+(Fraction&x)
{
Fraction c;
c.nume=nume*x.deno+x.nume*deno;
c.deno=deno*x.deno ;
return c;
}
bool operator==(Fraction& x)
{
return nume*x.deno==x.nume*deno;
}
Fraction& operator++()
{
nume+=deno;
return *this;
}
Fraction operator++(int)
{
Fraction y;
y=*this;
nume+=deno;
return y;
}
bool operator>(Fraction& x)
{
return nume*x.deno>deno*x.nume;
}
};
istream& operator>>(istream& istr,Fraction& x)
{
char ch;
cout<<"Input a fraction:";
istr>>x.nume>>ch>>x.deno;
if(x.deno ==0)
{
cerr<<"分母不得为零!"<
}
x.FracSimp ();
return istr;
}
ostream& operator<<(ostream& ostr,Fraction& x)
{
cout<
}
int main(void)
{
Fraction a(5,8),b,c;
cin>>b;
b++;
c=a+b;
cout return 0;
}
全部回答
- 1楼网友:骨子里都是戏
- 2021-03-15 04:57
#include <iostream>
using namespace std;
class fraction;
istream& operator>>(istream&,fraction&);
ostream& operator<<(ostream&,fraction&);
class fraction{
int nume;
int deno;
public:
friend istream& operator>>(istream&,fraction&);
friend ostream& operator<<(ostream&,fraction&);
void fracsimp()
{
int m,n,r;
m=nume;n=deno;
r=m%n;
while(r)
{
m=n;n=r;
r=m%n;
}
if(n!=1)
{
nume/=n;
deno /=n;
}
}
fraction():nume(0),deno(1){}
fraction(int n,int d):nume(n),deno(d){fracsimp();}
void innume(int n){nume=n;}
void indeno(int d){deno=d;}
int getnume(){return nume;}
int getdeno(){return deno;}
fraction operator+(fraction&x)
{
fraction c;
c.nume=nume*x.deno+x.nume*deno;
c.deno=deno*x.deno ;
return c;
}
bool operator==(fraction& x)
{
return nume*x.deno==x.nume*deno;
}
fraction& operator++()
{
nume+=deno;
return *this;
}
fraction operator++(int)
{
fraction y;
y=*this;
nume+=deno;
return y;
}
bool operator>(fraction& x)
{
return nume*x.deno>deno*x.nume;
}
};
istream& operator>>(istream& istr,fraction& x)
{
char ch;
cout<<"input a fraction:";
istr>>x.nume>>ch>>x.deno;
if(x.deno ==0)
{
cerr<<"分母不得为零!"<<endl;
exit(1);
}
x.fracsimp ();
return istr;
}
ostream& operator<<(ostream& ostr,fraction& x)
{
cout<<x.nume<<'/'<<x.deno<<endl;
return ostr;
}
int main(void)
{
fraction a(5,8),b,c;
cin>>b;
b++;
c=a+b;
cout<<a;
cout<<b;
cout<<c;
cout<<(a>b?1:0)<<endl;
return 0;
}
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯