练手_分数加减乘除

本文介绍了一个C++程序示例,展示了如何为分数结构体重载基本算术运算符(加、减、乘、除),并实现分数的简化处理。通过自定义输入输出操作符,使得程序能够读取和输出分数形式的数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1. #include <iostream>
  2. using namespace std;
  3. /*
  4.  *学会了怎么重载>>和<<
  5.  *功能还不是很全面,只支持最简单的-_-另外输入输出也没判断错..而且只能输入正的...why?见下:
  6.  *纯属练手
  7.  */
  8. bool sng;
  9. struct fac
  10. {
  11.     int h,r;
  12.     inline int gcd(int a,int b){return b?gcd(b,a%b):a;}
  13.     inline void make(){int tmp=gcd(h,r);h/=tmp,r/=tmp;}
  14.     friend inline istream& operator>>(istream& in,fac& f)
  15.         {
  16.             char c;
  17.             f.r=0;
  18.             while(f.r==0) {in>>f.h>>c>>f.r;if(f.r==0)cout<<"Invalid input!/n";}
  19.             return in;
  20.         }
  21.     friend inline ostream& operator<<(ostream& out,const fac& f)
  22.         {
  23.             if(sng)out<<'-';
  24.             if(f.r==1)
  25.                 out<<f.h;
  26.             else
  27.                 if(f.r==f.h)
  28.                     out<<1;
  29.                 else
  30.                     if(f.h==0)
  31.                         out<<0;
  32.                     else
  33.                         out<<f.h<<'/'<<f.r;
  34.             return out;
  35.         }
  36.     inline fac operator+(const fac& b)
  37.         {
  38.             fac tmp;
  39.             tmp.r=r*b.r/gcd(r,b.r);
  40.             tmp.h=tmp.r/r*h+tmp.r/b.r*b.h;
  41.             tmp.make();
  42.             return tmp;
  43.         }
  44.     inline fac operator-(const fac& b)
  45.         {
  46.             fac tmp;bool sign=false;
  47.             tmp.r=r*b.r/gcd(r,b.r);
  48.             tmp.h=tmp.r/r*h-tmp.r/b.r*b.h;
  49.             if(tmp.h<0)sng=true,tmp.h*=-1;
  50.             tmp.make();
  51.             return tmp;
  52.         }
  53.     inline fac operator*(const fac& b)
  54.         {
  55.             fac tmp;
  56.             tmp.r=r*b.r;
  57.             tmp.h=h*b.h;
  58.             if(tmp.r*tmp.h<0)sng=true,tmp.r=abs(tmp.r),tmp.h=abs(tmp.h);
  59.             tmp.make();
  60.             return tmp;
  61.         }
  62.     inline fac operator/(const fac& b)
  63.         {
  64.             fac tmp;
  65.             tmp.r=r*b.h;
  66.             tmp.h=h*b.r;
  67.             if(tmp.r*tmp.h<0)sng=true,tmp.r=abs(tmp.r),tmp.h=abs(tmp.h);
  68.             tmp.make();
  69.             return tmp;
  70.         }
  71. };
  72. int main()
  73. {
  74.     fac a,b;
  75.     while(cin>>a>>b)
  76.     {
  77.         sng=false;
  78.         cout<<a+b<<endl;
  79.                 cout<<a-b<<endl;
  80.                 sng=false;
  81.                 cout<<a*b<<endl;
  82.                 sng=false;
  83.                 cout<<a/b<<endl;
  84.     }
  85.     return 0;
  86. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值