练手_分数加减乘除
2008-09-01 14:51
197 查看
#include <iostream>
using namespace std;
/*
*学会了怎么重载>>和<<
*功能还不是很全面,只支持最简单的-_-另外输入输出也没判断错..而且只能输入正的...why?见下:
*纯属练手
*/
bool sng;
struct fac
{
int h,r;
inline int gcd(int a,int b){return b?gcd(b,a%b):a;}
inline void make(){int tmp=gcd(h,r);h/=tmp,r/=tmp;}
friend inline istream& operator>>(istream& in,fac& f)
{
char c;
f.r=0;
while(f.r==0) {in>>f.h>>c>>f.r;if(f.r==0)cout<<"Invalid input!/n";}
return in;
}
friend inline ostream& operator<<(ostream& out,const fac& f)
{
if(sng)out<<'-';
if(f.r==1)
out<<f.h;
else
if(f.r==f.h)
out<<1;
else
if(f.h==0)
out<<0;
else
out<<f.h<<'/'<<f.r;
return out;
}
inline fac operator+(const fac& b)
{
fac tmp;
tmp.r=r*b.r/gcd(r,b.r);
tmp.h=tmp.r/r*h+tmp.r/b.r*b.h;
tmp.make();
return tmp;
}
inline fac operator-(const fac& b)
{
fac tmp;bool sign=false;
tmp.r=r*b.r/gcd(r,b.r);
tmp.h=tmp.r/r*h-tmp.r/b.r*b.h;
if(tmp.h<0)sng=true,tmp.h*=-1;
tmp.make();
return tmp;
}
inline fac operator*(const fac& b)
{
fac tmp;
tmp.r=r*b.r;
tmp.h=h*b.h;
if(tmp.r*tmp.h<0)sng=true,tmp.r=abs(tmp.r),tmp.h=abs(tmp.h);
tmp.make();
return tmp;
}
inline fac operator/(const fac& b)
{
fac tmp;
tmp.r=r*b.h;
tmp.h=h*b.r;
if(tmp.r*tmp.h<0)sng=true,tmp.r=abs(tmp.r),tmp.h=abs(tmp.h);
tmp.make();
return tmp;
}
};
int main()
{
fac a,b;
while(cin>>a>>b)
{
sng=false;
cout<<a+b<<endl;
cout<<a-b<<endl;
sng=false;
cout<<a*b<<endl;
sng=false;
cout<<a/b<<endl;
}
return 0;
}
using namespace std;
/*
*学会了怎么重载>>和<<
*功能还不是很全面,只支持最简单的-_-另外输入输出也没判断错..而且只能输入正的...why?见下:
*纯属练手
*/
bool sng;
struct fac
{
int h,r;
inline int gcd(int a,int b){return b?gcd(b,a%b):a;}
inline void make(){int tmp=gcd(h,r);h/=tmp,r/=tmp;}
friend inline istream& operator>>(istream& in,fac& f)
{
char c;
f.r=0;
while(f.r==0) {in>>f.h>>c>>f.r;if(f.r==0)cout<<"Invalid input!/n";}
return in;
}
friend inline ostream& operator<<(ostream& out,const fac& f)
{
if(sng)out<<'-';
if(f.r==1)
out<<f.h;
else
if(f.r==f.h)
out<<1;
else
if(f.h==0)
out<<0;
else
out<<f.h<<'/'<<f.r;
return out;
}
inline fac operator+(const fac& b)
{
fac tmp;
tmp.r=r*b.r/gcd(r,b.r);
tmp.h=tmp.r/r*h+tmp.r/b.r*b.h;
tmp.make();
return tmp;
}
inline fac operator-(const fac& b)
{
fac tmp;bool sign=false;
tmp.r=r*b.r/gcd(r,b.r);
tmp.h=tmp.r/r*h-tmp.r/b.r*b.h;
if(tmp.h<0)sng=true,tmp.h*=-1;
tmp.make();
return tmp;
}
inline fac operator*(const fac& b)
{
fac tmp;
tmp.r=r*b.r;
tmp.h=h*b.h;
if(tmp.r*tmp.h<0)sng=true,tmp.r=abs(tmp.r),tmp.h=abs(tmp.h);
tmp.make();
return tmp;
}
inline fac operator/(const fac& b)
{
fac tmp;
tmp.r=r*b.h;
tmp.h=h*b.r;
if(tmp.r*tmp.h<0)sng=true,tmp.r=abs(tmp.r),tmp.h=abs(tmp.h);
tmp.make();
return tmp;
}
};
int main()
{
fac a,b;
while(cin>>a>>b)
{
sng=false;
cout<<a+b<<endl;
cout<<a-b<<endl;
sng=false;
cout<<a*b<<endl;
sng=false;
cout<<a/b<<endl;
}
return 0;
}
相关文章推荐
- Objective-C 分数加减乘除
- c++分数加减乘除
- 实现分数类中的运算符重载,在分数类中可以完成分数的加减乘除(运算后再化简)、比较(6种关系)的运算
- 实现分数类中的运算符重载,在分数类中可以完成分数的加减乘除(运算后再化简)、比较(6种关系)的运算
- c++第八周【任务3】实现分数类中的运算符重载,在分数类中可以完成分数的加减乘除(运算后再化简)、求反、比较(6种关系)的运算。
- 第八周实验报告(三)完成分数的加减乘除、求反、比较
- 关于 分数全家福(加减乘除方法)java版含交互对话框
- 第十周(11.18-11.24)----分数计算----(2)对两个分数进行加减乘除
- 第7周-项目3-分数类中的运算符重载-分数的加减乘除
- 第八周项目三 分数类中的运算符重载-(1)分数的加减乘除
- OC 便利构造器 自定义初始化 分数加减乘除
- 第八周实验报告(任务三)【分数的加减乘除】
- 9.3分数类中的运算符重载,完成分数的加减乘除
- OC 创建一个Fraction类(分数)实现分数的加减乘除,比较大小、约分等方法。 要求:为类添加属性
- 创建一个分数类,实现分数的加减乘除
- 分数的加减乘除运算
- 第7周-项目3-分数类中的运算符重载-拓展分数的加减乘除(分数与整型 )
- 分数加减乘除法==自己加了乘除法
- Fraction类(分数)实现分数的加减乘除, ⽐比较⼤大⼩小、约分等⽅方法
- 求两个分数的加减乘除,并比较大小