第8周项目3(1)分数类中的运算符重载

/*。
*Copyright(c)2014,烟台大学计算机学院
*All right reserved,
*文件名:test.cpp
*作者:毕玉堂
*完成日期:2015年5月7日
*版本号:v1.0
*
问题描述:
*输入描述:
*程序输出:
*/
#include <iostream>
#include<Cmath>
#include<cstdlib>
using namespace std;
int gcd(int m, int n);
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);//置值,改变值时用
    CFraction operator+(const CFraction &c);
    CFraction operator-(const CFraction &c);
    CFraction operator*(const CFraction &c);
    CFraction operator/(const CFraction &c);
    bool operator>(const CFraction &c);
    bool operator<(const CFraction &c);
    bool operator==(const CFraction &c);
    bool operator!=(const CFraction &c);
    bool operator>=(const CFraction &c);
    bool operator<=(const CFraction &c);
    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
};
void CFraction::set(int nu,int de)
{
    if(de!=0)
    {
        nume=nu;
        deno=de;
    }
}
CFraction CFraction::operator+(const CFraction &c)
{
    CFraction a;
    a.nume=nume*c.deno+c.nume*deno;
    a.deno=deno*c.deno;
    a.simplify();
    return a;
}
CFraction CFraction::operator-(const CFraction &c)
{
    CFraction a;
    a.nume=nume*c.deno-c.nume*deno;
    a.deno=deno*c.deno;
    a.simplify();
    return a;
}
CFraction CFraction::operator*(const CFraction &c)
{
    CFraction a;
    a.nume=nume*c.nume;
    a.deno=deno*c.deno;
    a.simplify();
    return a;
}
CFraction CFraction::operator/(const CFraction &c)
{
    CFraction a;
    a.nume=nume*c.deno;
    a.deno=deno*c.nume;
    a.simplify();
    return a;
}
bool CFraction::operator>(const CFraction &c)
{
   if((nume*c.deno>c.nume*deno&&deno*c.deno>0)||(nume*c.deno<c.nume*deno&&deno*c.deno<0))
        return true;
   return false;
}
bool CFraction::operator<(const CFraction &c)
{
   if((nume*c.deno<c.nume*deno&&deno*c.deno>0)||(nume*c.deno>c.nume*deno&&deno*c.deno<0))
        return true;
   return false;
}
bool CFraction::operator==(const CFraction &c)
{
   if(*this!=c)
        return false;
   return true;
}
bool CFraction::operator!=(const CFraction &c)
{
   if(*this>c||*this<c)
        return true;
   return false;
}
bool CFraction::operator>=(const CFraction &c)
{
   if(*this<c)
        return false;
   return true;
}
bool CFraction::operator<=(const CFraction &c)
{
   if(*this>c)
        return false;
   return true;
}
void CFraction::input()
{
    int nu,de;
    char a;
    cout<<"输入分数(m/n形式)";
    cin>>nu>>a>>de;
    if(a!='/')
    {
        cout<<"输入形式错误";
    }
    else if(de==0)
    {
        cout<<"输入形式错误";
    }
    nume=nu;
    deno=de;
}
void CFraction::simplify()
{
    int n=gcd(deno, nume);
    deno/=n;     // 化简
    nume/=n;
}

// 求m,n的最大公约数
int gcd(int m, int n) //这个函数可以定义为类的成员函数,也可以为一般函数
{
    int r;
    while(n!=0)
    {
        r=m%n;
        m=n;
        n=r;
    }
    return m;
}

void CFraction::amplify(int n)
{
    nume*=n;
}
void CFraction::output(int style)
{
    int n;
    switch(style)
    {
    case 0:
        cout<<"原样:" <<nume<<'/'<<deno<<endl;
        break;
    case 1:
        n=gcd(deno, nume);
        cout<<"化简形式: "<<nume/n<<'/'<<deno/n<<endl;     //输出化简形式,并不是要化简
        break;
    case 2:
        cout<<"带分数形式:" <<nume/deno<<'('<<nume%deno<<'/'<<deno<<')'<<endl;
        break;
    case 3:
        cout<<"近似值:" <<nume/double(deno)<<endl;
        break;
    default:
        cout<<"默认原样:" <<nume<<'/'<<deno<<endl;
    }
}
int main()
{
    CFraction x(1,3),y(-5,10),s;
    cout<<"分数x=1/3      y=-5/10"<<endl;
    s=x+y;
    cout<<"x+y=";
    s.output(0);
    s=x-y;
    cout<<"x-y=";
    s.output(0);
    s=x*y;
    cout<<"x*y=";
    s.output(0);
    s=x/y;
    cout<<"x/y=";
    s.output(0);

    x.output(0);
    if (x>y) cout<<"大于"<<endl;
    if (x<y) cout<<"小于"<<endl;
    if (x==y) cout<<"等于"<<endl;
    y.output(0);
    cout<<endl;
    return 0;
}

运行结果:


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值