第八周项目三(1):分数类中的运算符重载

本文介绍了一个分数类的设计与实现,通过运算符重载实现了分数的加减乘除及比较操作,并提供了输入、化简和输出等功能。

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

问题及代码:

/*
*Copyright (c)2014,烟台大学计算机与控制工程学院
*All rights reserved.
*文件名称: 分数类中的运算符重载  .cpp
*作    者:白云飞
*完成日期:2015年4月25日
*版 本 号:v1.0
*
*问题描述:实现分数类中的运算符重载,在分数类中可以完成分数的加减乘除(运算后再化简)、比较(6种关系)的运算。
*程序输入:分子和分母
*程序输出:计算结果
*/
#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);   //构造函数,初始化用
    CFraction operator+( CFraction &c);
    CFraction operator-( CFraction &c);
    CFraction operator*( CFraction &c);
    CFraction operator/( CFraction &c);
    bool operator>(CFraction &c);
    bool operator<(CFraction &c);
    bool operator>=(CFraction &c);
    bool operator<=(CFraction &c);
    bool operator==(CFraction &c);
    bool operator!=(CFraction &c);
    void input();               //按照"nu/de"的格式,如"5/2"的形式输入
    void simplify();            //化简(使分子分母没有公因子)
    void output();
};
CFraction::CFraction(int nu,int de)
{
    if(de!=0)
    {
        nume=nu;
        deno=de;
    }
    else
    {
        cerr<<"输入错误!";
        exit(0);
    }
}
CFraction CFraction::operator+( CFraction &c)
{
    CFraction t;
    t.nume=nume*c.deno+c.nume*deno;
    t.deno=deno*c.deno;
    t.simplify();
    return t;
}
CFraction CFraction::operator-( CFraction &c)
{
    CFraction t;
    t.nume=nume*c.deno-c.nume*deno;
    t.deno=deno*c.deno;
    t.simplify();
    return t;
}
CFraction CFraction::operator*( CFraction &c)
{
    CFraction t;
    t.nume=nume*c.nume;
    t.deno=deno*c.deno;
    t.simplify();
    return t;
}
CFraction CFraction::operator/( CFraction &c)
{
    CFraction t;
    t.nume=nume*c.deno;
    t.deno=deno*c.nume;
    t.simplify();
    return t;

}
bool CFraction::operator>(CFraction &c)
{
    int nume1,nume2;
    nume1=nume*c.deno;
    nume2=c.nume*deno;
    if(nume1-nume2>0) return true;
    else return false;
}
bool CFraction::operator<(CFraction &c)
{
    int nume1,nume2;
    nume1=nume*c.deno;
    nume2=c.nume*deno;
    if(nume1-nume2<0) return true;
    else return false;
}
bool CFraction::operator>=(CFraction &c)
{
    return !(*this<c);
}
bool CFraction::operator<=(CFraction &c)
{
    return !(*this>c);
}
bool CFraction::operator==(CFraction &c)
{
    if(*this!=c) return false;
    else return true;
}
bool CFraction::operator!=(CFraction &c)
{
    if (*this>c || *this<c) return true;
    return false;
}
void CFraction::input()
{
    int nu,de;
    char m;
    cout<<"请输入b分数(分子/分母):";
    cin>>nu>>m>>de;
    if(m!='/')
        cout<<"输入格式错误";
    else if(de==0)
        cout<<"分母为零,输入错误.";
    else
    {
        nume=nu;
        deno=de;
    }
}
void CFraction::simplify()
{
    int n=gcd(deno, nume);
    deno/=n;     // 化简
    nume/=n;
}
int gcd(int m, int n) //这个函数可以定义为类的成员函数,也可以为一般函数
{
    int r;
    if (m<n)
    {
        r=m;
        m=n;
        n=r;
    }
    while(r=m%n)  // 求m,n的最大公约数
    {
        m=n;
        n=r;
    }
    return n;
}
void CFraction::output()
{
    cout<<"("<<nume<<"/"<<deno<<")"<<endl;
}
int main()
{
    CFraction a(10,12),b,c;
    cout<<"a=10/12 "<<endl;
    b.input();
    if (a>b) cout<<"a>b"<<endl;
    if (a<b) cout<<"a<b"<<endl;
    if (a>=b) cout<<"a>=b"<<endl;
    if (a<=b) cout<<"a<=b"<<endl;
    if (a==b) cout<<"a=b"<<endl;
    if (a!=b) cout<<"a!=b"<<endl;
    c=a+b;
    cout<<"a+b=";
    c.output();
    c=a-b;
    cout<<"a-b=";
    c.output();
    c=a*b;
    cout<<"a*b=";
    c.output();
    c=a/b;
    cout<<"a/b=";
    c.output();
    return 0;
}


运行结果:


学习心得:

在写bool型判断a!=b的运算符重载函数时,写的是if(*this!=c)return true;else return false;一直是错的,后来单步到那步才知道是这错了,后来改成(*this>c||*this<c)return true;才改对。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值