分数类

/*
*Corpyright (c)2013,烟台大学计算机学院
*All right reseved.
*作者:张梦佳
*完成日期:2014年3月30日
*版本号:v1.0
*输入描述:
*问题描述:矩形累的初始化!
*程序输出:
*问题分析:
*算法设计:
*/

#include <iostream>
#include <cmath>
#include <cstring>
using namespace std;
class CFraction
{
private:
    int nume;  // 分子
    int deno;  // 分母
public:
    CFraction(int nu=0,int de=1)
    {
        nume=nu;
        deno=de;
    }  //构造函数,初始化用
    void set(int nu=0,int de=1);    //置值,改变值时用
    void input();	 //按照"nu/de"的格式,如"5/2"的形式输入
    void simplify();//化简(使分子分母没有公因子)
    void simplify1();
    void amplify(int n);	 //放大n倍,如2/3放大5倍为10/3
    void output(int style=0);	//输出:以8/6为例,style为0时,原样输出8/6;
                                //style为1时,输出化简后形式4/3;
                                //style为2时,输出1(1/3)形式,表示一又三分之一;
                                //style为3时,用小数形式输出,如1.3333;
                                //默认方式0
};
void CFraction::simplify1()
{
    int i=0;
    if(nume>deno&&nume%deno!=0)
    {
        i=nume/deno-1;
        cout<<i<<"("<<nume-deno*i<<"/"<<deno<<")"<<endl;
    }
    else if(nume<deno&&nume%deno!=0)
    {
        cout<<"("<<nume<<"/"<<deno<<")"<<endl;
    }
    else
    {
        cout<<nume/deno<<endl;
    }

}
void CFraction::set(int nu,int de)
{
    nume=nu;
    deno=de;
}
void CFraction::input()
{
    int a,b;
    string sss;
    cout<<"请输入:"<<endl;
    cin>>a>>sss>>b;
    if(sss!="/")
    {
        input();
    }
    else
    {
        set(a,b);
    }

}
void CFraction::simplify()
{
    int s,q;
    if(nume>deno)
    {
        for(int i=deno;i>0;i--)
        {
            s=deno%i;
            q=nume%i;
            if(s==0&&q==0)
                {
                    cout<<"("<<nume/i<<"/"<<deno/i<<")"<<endl;
                    break;
                }

        }
    }
    else
    {
        if(nume<deno)
    {
        for(int i=nume;i>0;i--)
        {
            s=deno%i;
            q=nume%i;
            if(s==0&&q==0)
            {
                 cout<<"("<<nume/i<<"/"<<deno/i<<")"<<endl;
                 break;
            }

        }
    }
    }
}
void CFraction::amplify(int n)
{
    nume=nume*n;
    cout<<"("<<nume<<"/"<<deno<<")"<<endl;
}
void CFraction::output(int style)
{
    if(style==0)
    {
        cout<<nume<<"/"<<deno<<endl;
    }
    else if(style==1)
    {
        simplify();
    }
    else if(style==2)
    {
       simplify1();
    }
    else
    {
        cout<<nume<<"/"<<deno<<endl;
    }
}
int main()
{
    CFraction t1;
    t1.input();
    t1.output(0);
    t1.output(1);
    t1.output(2);
    t1.output(3);
    cout<<"请输入要乘的倍数!"<<endl;
    int n;
    cin>>n;
    t1.amplify(n);
    return  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、付费专栏及课程。

余额充值