关于分数的类

/*  Copyright  (c)  2016

*    All rights reserved

*    文件名称:3.cpp

*   作者:刘丽

*   完成日期:2016年 4 月 5日

*   版本号: v1.0

*

*   问题描述:建立一个关于分数的类

*/

#include <iostream>

using namespace std;
class CFraction
{
  private:
      int nume;//分子
      int deno;//分母
      int s=1;//倍数
  public:
      CFraction(int nu,int de);//构造函数,初始化用
      void set (int nu,int de);//置值,改变值时用
      void input();//按照“nu/de”的格式,如“5/2”的形式输入
      void simplify();//化简(使分子分母没有公因子)
      void amplify(int n);//放大n倍,如2/3放大5倍为10/3
      void output(int style);//输出:以8/6为例,style为0时,原样输出8/6;
                              //style为1时,输出化简后形式4/3;
                              //style为2时,输出1(1/3)形式,表示一有三分之一
                              //style为3时,用小数形式输出,如1.3333;
                              //默认方式0

};
CFraction::CFraction(int nu=0,int de=1):nume(nu),deno(de){
}
void CFraction::set (int nu=0,int de =1)
{
    char a;
    cin>>nume>>a>>deno;
}
void CFraction::input()
{
    char a;
    cin>>nume>>a>>deno;
}
void CFraction::simplify()
{
    int n;
    n=deno;
    if(deno>nume)
      n=nume;
    for(;n>0;n--)
    {
        if(nume%n==0&&deno%n==0)
        {
            nume/=n;
            deno/=n;
            s*=n;
            break;
        }

    }

}
void CFraction::amplify(int n)
{
    nume*=n;
    s*=n;
}
void CFraction::output(int style=0)
{
    int m=0;
    if(nume>deno)
        m=nume%deno;
    switch(style)
    {
        case 0:cout<<s*nume<<"/"<<s*deno<<endl;break;
        case 1:if(nume%deno==0)
                cout<<nume/deno<<endl;
             else
                cout<<nume<<"/"<<deno<<endl;break;
        case 2:if(m==0)
                cout<<nume<<"/"<<deno<<endl;
               else
                cout<<nume/deno<<"("<<m<<"/"<<deno<<")"<<endl;break;
        case 3:cout<<1.0*nume/deno<<endl;break;

    }

}
int main()
{
    CFraction c1;
    int style,n;
    c1.input();
    cout<<"输入表达方式"<<endl;
    cin>>style;
    c1.output(style);
    cout<<"想要放大多少倍:"<<endl;
    cin>>n;
    c1.amplify(n);
    c1.simplify();
    cout<<"输入表达方式"<<endl;
    cin>>style;
    c1.output(style);



    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个简单的分数 `Fraction` 的实现: ```python class Fraction: def __init__(self, numerator, denominator=1): if denominator == 0: raise ZeroDivisionError("Denominator cannot be zero.") self.numerator = numerator self.denominator = denominator def __str__(self): return f"{self.numerator}/{self.denominator}" def __repr__(self): return f"Fraction({self.numerator}, {self.denominator})" def __eq__(self, other): if isinstance(other, Fraction): return self.numerator * other.denominator == self.denominator * other.numerator elif isinstance(other, int): return self.numerator == other * self.denominator else: return NotImplemented def __lt__(self, other): if isinstance(other, Fraction): return self.numerator * other.denominator < self.denominator * other.numerator elif isinstance(other, int): return self.numerator < other * self.denominator else: return NotImplemented def __le__(self, other): if isinstance(other, Fraction): return self.numerator * other.denominator <= self.denominator * other.numerator elif isinstance(other, int): return self.numerator <= other * self.denominator else: return NotImplemented def __add__(self, other): if isinstance(other, Fraction): return Fraction(self.numerator * other.denominator + other.numerator * self.denominator, self.denominator * other.denominator) elif isinstance(other, int): return Fraction(self.numerator + other * self.denominator, self.denominator) else: return NotImplemented def __sub__(self, other): if isinstance(other, Fraction): return Fraction(self.numerator * other.denominator - other.numerator * self.denominator, self.denominator * other.denominator) elif isinstance(other, int): return Fraction(self.numerator - other * self.denominator, self.denominator) else: return NotImplemented def __mul__(self, other): if isinstance(other, Fraction): return Fraction(self.numerator * other.numerator, self.denominator * other.denominator) elif isinstance(other, int): return Fraction(self.numerator * other, self.denominator) else: return NotImplemented def __truediv__(self, other): if isinstance(other, Fraction): return Fraction(self.numerator * other.denominator, self.denominator * other.numerator) elif isinstance(other, int): return Fraction(self.numerator, self.denominator * other) else: return NotImplemented ``` 这个支持分数的加、减、乘、除运算,以及比较运算符 `<`, `<=`, `==`。注意到我们在实现运算符方法时,对于可能出现的型不匹配情况,我们返回了 `NotImplemented`,这是为了让 Python 解释器知道我们没有实现这种情况下的运算,从而让它去尝试反向调用运算符方法。例如,如果 `a` 是一个分数,`b` 是一个整数,那么 `a + b` 会先尝试调用 `a.__add__(b)`,如果返回了 `NotImplemented`,则会尝试调用 `b.__radd__(a)`。如果 `b` 也没有实现 `__radd__` 方法,那么就会抛出 `TypeError` 异常。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值