永发信息网

编写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;
}
全部回答
  • 1楼网友:骨子里都是戏
  • 2021-03-15 04:57
#include &lt;iostream&gt; using namespace std; class fraction; istream&amp; operator&gt;&gt;(istream&amp;,fraction&amp;); ostream&amp; operator&lt;&lt;(ostream&amp;,fraction&amp;); class fraction{ int nume; int deno; public: friend istream&amp; operator&gt;&gt;(istream&amp;,fraction&amp;); friend ostream&amp; operator&lt;&lt;(ostream&amp;,fraction&amp;); 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&amp;x) { fraction c; c.nume=nume*x.deno+x.nume*deno; c.deno=deno*x.deno ; return c; } bool operator==(fraction&amp; x) { return nume*x.deno==x.nume*deno; } fraction&amp; operator++() { nume+=deno; return *this; } fraction operator++(int) { fraction y; y=*this; nume+=deno; return y; } bool operator&gt;(fraction&amp; x) { return nume*x.deno&gt;deno*x.nume; } }; istream&amp; operator&gt;&gt;(istream&amp; istr,fraction&amp; x) { char ch; cout&lt;&lt;"input a fraction:"; istr&gt;&gt;x.nume&gt;&gt;ch&gt;&gt;x.deno; if(x.deno ==0) { cerr&lt;&lt;"分母不得为零!"&lt;&lt;endl; exit(1); } x.fracsimp (); return istr; } ostream&amp; operator&lt;&lt;(ostream&amp; ostr,fraction&amp; x) { cout&lt;&lt;x.nume&lt;&lt;'/'&lt;&lt;x.deno&lt;&lt;endl; return ostr; } int main(void) { fraction a(5,8),b,c; cin&gt;&gt;b; b++; c=a+b; cout&lt;&lt;a; cout&lt;&lt;b; cout&lt;&lt;c; cout&lt;&lt;(a&gt;b?1:0)&lt;&lt;endl; return 0; }
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯