Unit8-problem-分数(补写)

/*Univercity:烟台大学
*@Class</A>计134~4
*@Author:薛富磊
*@Time:2014-4-28
*@Function:
*@Args:
*@Return:
*/
#include<iostream>
#include<Cmath>
using namespace std;
class CFraction
{
private:
    int nume;  // 分子
    int deno;  // 分母
public:
    //构造函数及运算符重载的函数声明
    CFraction(int nu=0,int de=1):nume(nu),deno(de) {}
    void simplify();//化最简
    void display();//显示
    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 CFraction::simplify()
{
    int a,b,c;
    //数学函数:fabs   原型:extern float fabs(float x);   
    //用法:#include <math.h>   功能:求浮点数x的绝对值
    //说明:计算|x|, 当x不为负时返回x,否则返回-x  
    a=fabs(nume);
    b=fabs(deno);
    //求最大公约数
    while(c=a%b)
    {
        a=b;
        b=c;
    }
    nume=nume/b;
    deno=deno/b;
    if(deno<0)
    {
        nume=-nume;
        deno=-deno;
    }
}
//显示
void CFraction::display()
{
    cout<<nume<<"/"<<deno<<endl;
}
//两个分数相加
CFraction CFraction::operator+(const CFraction &c)
{
    CFraction s;
    s.nume=nume*c.deno+c.nume*deno;
    s.deno=deno*c.deno;
    s.simplify();
    return s;
}
//两个分数相减
CFraction CFraction::operator-(const CFraction &c)
{
    CFraction s;
    s.nume=nume*c.deno-c.nume*deno;
    s.deno=deno*c.deno;
    s.simplify();
    return s;
}
//两个分数相乘
CFraction CFraction::operator*(const CFraction &c)
{
    CFraction s;
    s.nume=nume*c.nume;
    s.deno=deno*c.deno;
    s.simplify();
    return s;
}
//两个分数相除
CFraction CFraction::operator/(const CFraction &c)
{
    CFraction s;
    s.nume=nume*c.deno;
    s.deno=deno*c.nume;
    s.simplify();
    return s;
}
bool CFraction::operator>(const CFraction &c)
{
    int a,b;
    a=nume*c.deno;
    b=c.nume*deno;
    if((a-b)>0) return true;
    return false;
}
bool CFraction::operator<(const CFraction &c)
{
    int a,b;
    a=nume*c.deno;
    b=c.nume*deno;
    if ((a-b)<0) return true;
    return false;
}
bool CFraction::operator==(const CFraction &c)
{
    if(*this==c)return true;
    return false;
}
bool CFraction::operator!=(const CFraction &c)
{
    int a,b;
    a=nume*c.deno;
    b=c.nume*deno;
    if ((a-b)!=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) return false;
    return true;
}
//重载函数的实现及用于测试的main()函数
int main()
{
    CFraction x(2,5),y(-4,14),s;
    cout<<"分数x=2/5      y=-4/14"<<endl;
    s=x+y;
    cout<<"x+y=";
    s.display();
    s=x-y;
    cout<<"x-y=";
    s.display();
    s=x*y;
    cout<<"x*y=";
    s.display();
    s=x/y;
    cout<<"x/y=";
    s.display();

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值