`

Rational

    博客分类:
  • c++
阅读更多
#ifndef RATIONAL_H
#define RATIONAL_H
#include<iosfwd>
class Rational{
public:
    int mNum;
    int mDen;
public:
    Rational(int numerator = 0,int denominator = 1);
    Rational operator-()const;
    friend Rational operator+(const Rational&,
                              const Rational&);
    friend Rational operator-(const Rational&,
                              const Rational&);
    friend Rational operator*(const Rational&,
                              const Rational&);
    friend Rational operator/(const Rational&,
                              const Rational&);
    friend std::ostream& operator<<(std::ostream&,const Rational&);
    friend std::istream&
    operator>>(std::istream&,Rational&);
    Rational& operator+=(const Rational&);
    Rational& operator-=(const Rational&);
    Rational& operator*=(const Rational&);
    Rational& operator/=(const Rational&);
    friend bool operator<(const Rational&,
                          const Rational&);
    friend bool operator>(const Rational&,
                          const Rational&);
    friend bool operator<=(const Rational&,
                           const Rational&);
    friend bool operator>=(const Rational&,
                           const Rational&);
    friend bool operator==(const Rational&,
                           const Rational&);
    friend bool operator!=(const Rational&,
                           const Rational&);
};

#endif // RATIONAL_H



#include<Rational.h>
#include<cassert>
#include<iostream>
using namespace std;

Rational::Rational(int numerator, int denominator):mNum(numerator),mDen(denominator)
{

}

Rational operator+(const Rational& r1,const Rational& r2)
{
    assert(r1.mDen!=0);assert(r2.mDen!=0);
    int den = r1.mDen*r2.mDen;
    int num = r1.mNum*r2.mDen+r2.mNum*r1.mDen;
    return Rational(num,den);
}

Rational operator-(const Rational& r1,const Rational& r2)
{
    assert(r1.mDen!=0);assert(r2.mDen!=0);
    int den = r1.mDen*r2.mDen;
    int num = r1.mNum*r2.mDen-r2.mNum*r1.mDen;
    return Rational(num,den);
}

Rational operator*(const Rational& r1,const Rational& r2)
{
    assert(r1.mDen!=0);assert(r2.mDen!=0);
    int den = r1.mDen*r2.mDen;
    int num = r1.mNum*r2.mDen*(r2.mNum*r1.mDen);
    return Rational(num,den);
}

Rational operator/(const Rational& r1,const Rational& r2)
{
    assert(r1.mDen!=0);assert(r2.mDen!=0);
    int den = r1.mDen*r2.mDen;
    int num = r1.mNum*r2.mDen/(r2.mNum*r1.mDen);
    return Rational(num,den);
}

std::ostream& operator<<(std::ostream& os,const Rational& r)
{
    assert(r.mDen!=0);
    os << r.mNum << "," << r.mDen;
    return os;
}

std::istream& operator>>(std::istream& is,const Rational& r)
{
    assert(r.mDen!=0);
    is >> r.mDen >> r.mNum;
}

Rational& Rational::operator+=(const Rational& r)
{
    assert(this->mDen!=0);assert(r.mDen!=0);
    this->mDen = this->mDen*r.mDen;
    this->mNum = this->mNum*r.mDen+r.mNum*this->mDen;
    return *this;
}

Rational& Rational::operator-=(const Rational& r)
{
    assert(this->mDen!=0);assert(r.mDen!=0);
    this->mDen = this->mDen*r.mDen;
    this->mNum = this->mNum*r.mDen-r.mNum*this->mDen;
    return *this;
}

Rational& Rational::operator*=(const Rational& r)
{
    assert(this->mDen!=0);assert(r.mDen!=0);
    this->mDen = this->mDen*r.mDen;
    this->mNum = this->mNum*r.mDen*(r.mNum*this->mDen);
    return *this;
}

Rational& Rational::operator/=(const Rational& r)
{
    assert(this->mDen!=0);assert(r.mDen!=0);
    this->mDen = this->mDen*r.mDen;
    this->mNum = this->mNum*r.mDen/(r.mNum*this->mDen);
    return *this;
}

bool operator<(const Rational& r1,Rational& r2)
{
    assert(r1.mDen!=0);assert(r2.mDen!=0);
    double num1 = r1.mNum/r1.mDen;
    double num2 = r2.mNum/r2.mNum;
    return num1<num2;
}

bool operator>(const Rational& r1,Rational& r2)
{
    assert(r1.mDen!=0);assert(r2.mDen!=0);
    double num1 = r1.mNum/r1.mDen;
    double num2 = r2.mNum/r2.mNum;
    bool t = r1<r2;
    return num1>num2;
}

bool operator<=(const Rational& r1,Rational& r2)
{
    assert(r1.mDen!=0);assert(r2.mDen!=0);
    double num1 = r1.mNum/r1.mDen;
    double num2 = r2.mNum/r2.mNum;
    return num1<=num2;
}

bool operator>=(const Rational& r1,Rational& r2)
{
    assert(r1.mDen!=0);assert(r2.mDen!=0);
    double num1 = r1.mNum/r1.mDen;
    double num2 = r2.mNum/r2.mNum;
    return num1<num2;
}

bool operator==(const Rational& r1,Rational& r2)
{
    assert(r1.mDen!=0);assert(r2.mDen!=0);
    double num1 = r1.mNum/r1.mDen;
    double num2 = r2.mNum/r2.mNum;
    return num1==num2;
}

bool operator!=(const Rational& r1,Rational& r2)
{
    assert(r1.mDen!=0);assert(r2.mDen!=0);
    double num1 = r1.mNum/r1.mDen;
    double num2 = r2.mNum/r2.mNum;
    return num1!=num2;
}

int main()
{
    Rational r1(6),r2(3),r3;
    r3=r1-r2;
    cout << r3 << endl;
    r3=r1+r2;
    cout << r3 << endl;
    r3=r1*r2;
    cout << r3 << endl;
    r3=r1/r2;
    cout << r3 << endl;
    r3+=r1;
    cout << r3 << endl;
    r3-=r1;
    cout << r3 << endl;
    r3*=r1;
    cout << r3 << endl;
    r3/=r1;
    cout << r3 << endl;
    bool b;
    b = r3<r1;
    cout << b << endl;
    b = r3>r1;
    cout << b << endl;
    //cin >> r3; error

}

分享到:
评论

相关推荐

    rational rose 汉化补丁

    Rational Rose是一款强大的软件建模工具,由IBM Rational公司开发,主要用于进行统一建模语言(UML)的建模工作。在中文环境下,用户可能需要Rational Rose的汉化补丁来实现界面语言的转换,使其更加友好和易用。这...

    Rational Rose-汉化补丁

    Rational Rose是一款强大的软件建模工具,主要用于统一建模语言(UML)的图形化表示。它由IBM公司开发,是软件开发过程中需求分析、系统设计、文档编写和项目管理的重要辅助工具。汉化补丁是为了让那些不熟悉英文...

    Rational Rose 2003 汉化包

    Rational Rose 2003 是一款经典的软件建模工具,由IBM开发,主要用于系统分析、设计和实现的可视化建模。它集成了统一建模语言(UML)的多种图表,帮助软件开发者以图形方式描绘软件架构,提高工作效率和沟通效果。...

    rational 全套licence 可用于7.1

    rational授权文件,可以用户7.1 PACKAGE LoadTest_Base rational 7.5 652CC8FB1271 \ COMPONENTS="LoadTestSuite:7.5:1 LTmaster:7.1:10000 \ LTgui:7.1:10000 LTvu:7.1:5550 LThttp:7.1:10000" PACKAGE TMvtpool...

    rational

    【IBM Rational Software Modeler Fix Pack 6.0.1.1 安装指南】 IBM Rational Software Modeler 是一款强大的建模工具,用于软件设计和开发。Fix Pack 是一种软件更新,用于修复产品中的已知问题和增强功能。本文档...

    rational rose 汉化版

    **Rational Rose汉化版**是一款专为软件工程师设计的可视化建模工具,它极大地简化了软件开发过程中的需求分析、系统设计与实现。Rational Rose的原版主要是英文界面,对于非英语背景的初学者而言,理解其功能和用法...

    有理数类实现 rational

    有理数类(Rational)在编程中是一个用于表示分数的类,通常用于精确计算,避免浮点数运算带来的精度损失。在这个实现中,我们有两个构造函数:一个默认构造函数和一个参数化构造函数。 1. **默认构造函数**: `...

    Rational Rose 2003 汉化版.rar

    Rational Rose 2003 是一款经典的软件建模工具,尤其在面向对象设计和统一建模语言(UML)领域具有广泛的应用。这款工具由IBM Rational公司开发,旨在帮助软件开发团队创建、管理和交流软件设计。汉化版的Rational ...

    如何使用测试工具Rational Purecoverage、Rational Purify、Rational Quantify、Rational Robot

    Rational公司提供了一系列强大的测试工具,如Rational Purecoverage、Rational Purify、Rational Quantify和Rational Robot,这些工具可以帮助开发者和测试人员有效地进行代码覆盖率分析、内存泄漏检测、性能优化...

    三大UML建模工具Visio、Rational Rose、PowerDesign的区别比较

    在UML建模工具中,Visio、Rational Rose和PowerDesign是三个非常知名的工具,它们各自具有独特的特性和适用场景。 Rational Rose是由IBM开发的,是UML建模的先驱之一。它全面支持UML的各种图表,包括类图、序列图、...

    分数的加减乘除运算Rational Numbers

    8.17 (Rational Numbers) Create a class called Rational for performing arithmetic with fractions. Write a program to test your class. Use integer variables to represent the private instance variables ...

    Rational Rose 2007(V7.0) 的许可证(license)

    Rational Rose 2007(V7.0) 是IBM公司推出的一款强大的统一建模语言(UML)工具,主要用于软件开发过程中的需求分析、系统设计、文档编写和项目管理。许可证(license)是使用这款专业软件的重要组成部分,它允许用户...

    IBM Rational 产品许可证(License)管理使用和优化

    第四种是基于 Eclipse 的 Rational Software Development Platform (SDP)的许可证,如 Rational Software Architect(RSA)、Rational Software Modeler(RSM)、Rational Application Developer(RAD)、Rational...

    IBM Rational DOORS 9.2 安装配置说明书(中文)

    iv Rational DOORS 安装指南 对集成的影响 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 将数据迁移至 Rational...

    IBM Rational 全套 License 支持 v7.1 有效期至 2020-1-6日

    PACKAGE LT_Playback_100000VU rational 7.5 51E6DD992302 \ COMPONENTS="LTvu:7.1:100000 LT_100000VT:8.0:10000" PACKAGE RSPS_TLA_500VU rational 1.5 F36CDEAC264E \ COMPONENTS="PerformanceStudioSuite:1.0:...

    使用 IBM Rational Systems Developer 和 Rational Software Architect 实现 UML 与 C++ 的转换

    学习如何使用 IBM:registered: Rational:registered: Software Architect 和 Rational:registered: Systems Developer 工具将 UML 建模适于开发 C++ 应用程序。本文描述了7.0版本支持 C++ 领域建模的特性。在此,...

    Rational Rose 2003 license 注册码

    Rational Rose 2003 是一款由IBM公司开发的著名软件建模工具,主要用于进行统一建模语言(UML)的设计和分析。在软件工程领域,它扮演着至关重要的角色,帮助开发者创建、管理和交流复杂的系统设计。Rational Rose ...

    IBM Rational 全套 License 支持 7.0、7.1 有效期至 2020-10-31日

    PACKAGE RSPS_Evaluation rational 1.5 1C0AB0D69C73 \ COMPONENTS="PerformanceStudioSuite:1.0:1 LTmaster:7.1:1 \ LTgui:7.1:10 LTvu:7.1:100 LTjolt:7.1:1 LTtux:7.1:1 LTsql:7.1:1 \ LThttp:7.1:1 LTsap:7.1...

    IBM Rational DOORS安装指南

    根据给定文件的标题、描述、标签以及部分内容,以下是关于IBM Rational DOORS安装的详细知识点: 1. Rational DOORS简介:Rational DOORS是一款广泛使用的软件需求管理工具,它支持产品开发过程中需求的捕获、分析...

Global site tag (gtag.js) - Google Analytics