#include<iostream>
#include<string>
#include<cmath>
using namespace std;

class Rational

{

public:

Rational();

Rational(long numerator, long denominator);

long getNumerator();

long getDenominator();

Rational add(Rational &secondRational);

Rational subtract(Rational &secondRational);

Rational multiply(Rational &secondRational);

Rational divide(Rational &secondRational);

int compareTo(Rational &secondRational);

bool equals(Rational &secondRational);

int intValue();

double doubleValue();

string toString();

// Define function operators for relational operators

bool operator<(Rational &secondRational);

bool operator<=(Rational &secondRational);

bool operator>(Rational &secondRational);

bool operator>=(Rational &secondRational);

bool operator!=(Rational &secondRational);

bool operator==(Rational &secondRational);

// Define function operators for arithmetic operators

Rational operator+(Rational &secondRational);

Rational operator-(Rational &secondRational);

Rational operator*(Rational &secondRational);

Rational operator/(Rational &secondRational);

// Define function operators for shorthand operators

Rational& operator+=(Rational &secondRational);

Rational& operator-=(Rational &secondRational);

Rational& operator*=(Rational &secondRational);

Rational& operator/=(Rational &secondRational);

// Define function operator [], 0 for numerator and 1 for denominator

long& operator[](const int &index);

// Define function operators for prefix ++ and --

Rational& operator++();

Rational& operator--();

// Define function operators for postfix ++ and --

Rational operator++(int dummy);

Rational operator--(int dummy);

// Define function operators for unary + and -

Rational operator+();

Rational operator-();

// Define the output and input operator

friend ostream &operator<<(ostream &stream, Rational rational);

friend istream &operator>>(istream &stream, Rational &rational);

// Define function operator for conversion

operator double();

private:

long numerator;

long denominator;

static long gcd(long n, long d);

};

Rational::operator double(){
   double a;
   a=1.0* this->numerator/this->denominator;
   return a;
  }

long Rational::gcd(long a,long b){
 if(a==0) return 1;
 if(a < 0) a = -a;
 if(b < 0) b = -b;
 if(a<b){
  long t;
  t=a;
  a=b;
  b=t;
 }
 while(a%b != 0){
  long t = a%b;
  a=b;
  b=t;
 }
 return b;
}

Rational::Rational(){
    this->numerator=0;
    this->denominator=1;
}

Rational::Rational(long numerator, long denominator){
    this->numerator = numerator;
    this->denominator = denominator;
    //cout<<gcd(8,12)<<endl;
}

long Rational::getNumerator(){
    return numerator;
}

long Rational::getDenominator(){
 
    return denominator;
}

Rational Rational::add(Rational &secondRational){
    Rational r;
    r.numerator = this->numerator *secondRational.denominator+ this->denominator * secondRational.numerator;
    r.denominator=this->numerator *secondRational.numerator;
    return r;
}

Rational Rational::subtract(Rational &secondRational){
    Rational r;
    r.numerator = -this->denominator *secondRational.numerator + this->numerator*secondRational.denominator;
    r.denominator=this->numerator *secondRational.numerator;
    return r;
}

Rational Rational::multiply(Rational &secondRational){
    Rational r;
    r.numerator = this->numerator * secondRational.numerator;
    r.denominator=this->denominator * secondRational.numerator;
    return r;
}

Rational Rational::divide(Rational &secondRational){
    Rational r;
    r.numerator = this->numerator * secondRational.denominator;
    r.denominator=this->denominator * secondRational.numerator;
    return r;
}

int Rational::compareTo(Rational &secondRational){
 if(this->denominator *secondRational.numerator - this->numerator*secondRational.denominator==0) return 0;
    else if(this->denominator *secondRational.numerator - this->numerator*secondRational.denominator<0) return 1;
    else return -1;
}

bool Rational::equals(Rational &secondRational){
    if(this->denominator *secondRational.numerator - this->numerator*secondRational.denominator==0) return true;
    else return false;
}

int Rational::intValue(){
    int t;
    t=int(this->numerator / this->denominator);
    return t;
}

double Rational::doubleValue(){
    double t;
    t=1.0*this->numerator / this->denominator;
    return t;
}

string Rational::toString(){
    string s;
    string s1;
    string s2;
    long a,b;
    int fuhao=0;
    long sm = gcd(this->numerator,this->denominator);
    a = this->numerator /sm;
    b = this->denominator /sm;
    if(a==0||b==0){
     return "0";
    }
    if(a<0) {
     fuhao++;
     a=(-1)*a;
    }
    if(b<0) {
     fuhao++;
     b=(-1)*b;
    }
    while(a!=0){
     char c=(a%10 +'0');
     s1 = c+s1;
     a=a/10;
    }
     while(b!=0){
      char c=( b%10 + '0');
     s2=c+s2;
     b=b/10;
    }
  
    if(s2=="1"){
     if(fuhao==1)
     s1="-"+s1;
     return s1;
    }
    else{
     s=s1+"/"+s2;
     if(fuhao==1) s="-"+s;
     return s;
    }
}

// Define function operators for relational operators

bool Rational::operator<(Rational &secondRational){
      if(this->denominator *secondRational.numerator - this->numerator*secondRational.denominator>0) return true;
      else return false;
  }

bool Rational::operator<=(Rational &secondRational){
      if(this->denominator *secondRational.numerator - this->numerator*secondRational.denominator>=0) return true;
      else return false;
  }

bool Rational::operator>(Rational &secondRational){
      if(this->denominator *secondRational.numerator - this->numerator*secondRational.denominator<0) return true;
      else return false;
  }

bool Rational::operator>=(Rational &secondRational){
      if(this->denominator *secondRational.numerator - this->numerator*secondRational.denominator<=0) return true;
      else return false;
  }

bool Rational::operator!=(Rational &secondRational){
      if(this->denominator *secondRational.numerator - this->numerator*secondRational.denominator!=0) return true;
      else return false;
  }

bool Rational::operator==(Rational &secondRational){
      if(this->denominator *secondRational.numerator - this->numerator*secondRational.denominator==0) return true;
      else return false;
  }

// Define function operators for arithmetic operators

Rational Rational::operator+(Rational &secondRational){
      Rational r;
      r.numerator = this->numerator *secondRational.denominator+ this->denominator*secondRational.numerator;
      r.denominator=this->numerator *secondRational.numerator;
      return r;
  }

Rational Rational::operator-(Rational &secondRational){
      Rational r;
      r.numerator = this->numerator *secondRational.denominator - this->denominator*secondRational.numerator;
      r.denominator=this->denominator *secondRational.denominator;
      return r;
  }

Rational Rational::operator*(Rational &secondRational){
      Rational r;
      r.numerator = this->numerator * secondRational.numerator;
      r.denominator = this->denominator * secondRational.denominator;
      return r;
  }

Rational Rational::operator/(Rational &secondRational){
      Rational r;
      r.numerator = this->numerator * secondRational.denominator;
      r.denominator = this->denominator * secondRational.numerator;
      return r;
  }

// Define function operators for shorthand operators

Rational& Rational::operator+=(Rational &secondRational){
      this->numerator = this->denominator*secondRational.numerator + this->numerator*secondRational.denominator;
      this->denominator=this->denominator*secondRational.denominator;
      return *this;
  }

Rational& Rational::operator-=(Rational &secondRational){
      this->numerator = this->numerator*secondRational.denominator - this->denominator*secondRational.numerator;
      this->denominator=this->denominator *secondRational.denominator;
      return *this;
 
  }

Rational& Rational::operator*=(Rational &secondRational){
      this->numerator = numerator*secondRational.numerator;
      this->denominator=this->denominator *secondRational.denominator;
      return *this;
  }

Rational& Rational::operator/=(Rational &secondRational){
      this->numerator = numerator*secondRational.denominator;
      this->denominator=this->denominator*secondRational.numerator;
      return *this;
  }                   
 
  long& Rational::operator[](const int &index){
    if(index==0) return this->numerator;
    if(index==1) return this->denominator;
  }
 
 
  Rational& Rational::operator++(){
    this->numerator = this->numerator + this->denominator;
    return *this;
  }

Rational& Rational::operator--(){
    this->numerator = this->numerator - this->denominator;
    return *this;
  }
 
  Rational Rational::operator++(int dummy){
    Rational temp(*this);
    temp.numerator = temp.numerator;
    this->numerator = this->numerator + this->denominator;
    return temp;
  }

Rational Rational::operator--(int dummy){
    Rational temp(*this);
    temp.numerator = temp.numerator ;
    this->numerator = this->numerator - this->denominator;
    return temp;
  }
 
  Rational Rational::operator+(){
    Rational temp;
    temp.numerator = this->numerator;
    temp.denominator =  this->denominator;
    return temp;
   
  }

Rational Rational::operator-(){
    Rational temp;
    temp.numerator = this->numerator * (-1);
    temp.denominator =  this->denominator;
    return temp;
  }

ostream &operator<<(ostream &stream, Rational rational){
//     long a = rational.numerator%rational.denominator;
//     if(a==0) cout<<rational.numerator/rational.denominator;
//     else {
//       long s =  Rational::gcd(rational.numerator,rational.denominator);
//       rational.numerator = rational.numerator / s;
//   rational.denominator = rational.denominator / s;
//       stream<<rational.numerator<<"/"<<rational.denominator;
//     }
  stream << rational.toString();
      return stream;
  }

istream &operator>>(istream &stream, Rational &rational){
     stream>>rational.numerator>>rational.denominator;
     return stream;  
  }
 
 
 
 
 
 
  int main()

{

// Create and initialize two rational numbers r1 and r2.

Rational r1(3, 2);

Rational r2(2, 3);

// Test relational operators

cout << r1 << " > " << r2 << " is " << (r1 > r2) << endl;

cout << r1 << " >= " << r1 << " is " << (r1 >= r1) << endl;

cout << r1 << " < " << r2 << " is " << (r1 < r2) << endl;

cout << r1 << " <= " << r1 << " is " << (r1 <= r1) << endl;

cout << r1 << " == " << r2 << " is " << (r1 == r2) << endl;

cout << r1 << " == " << r1 << " is " << (r1 == r1) << endl;

cout << r1 << " != " << r2 << " is " << (r1 != r2) << endl;

cout << r1 << " != " << r1 << " is " << (r1 != r1) << endl;

// Test toString, add, substract, multiply, and divide operators

cout << r1 << " + " << r2 << " = " << r1 + r2 << endl;

cout << r1 << " - " << r2 << " = " << r1 - r2 << endl;

cout << r1 << " * " << r2 << " = " << r1 * r2 << endl;

cout << r1 << " / " << r2 << " = " << r1 / r2 << endl;

// Test shorthand operators

Rational r3(1, 2);

r3 += r1;

cout << "r3 is " << r3 << endl;

r3 -= r1;

cout << "r3 is " << r3 << endl;

// Test function operator []

Rational r4(1, 2);

r4[0] = 3; r4[1] = 4;

cout << "r4 is " << r4 << endl;

// Test function operators for

// prefix ++ and --

// postfix ++ and --

r3 = r4++;

cout << "r3 is " << r3 << endl;

cout << "r4 is " << r4 << endl;

r3 = r4--;

cout << "r3 is " << r3 << endl;

cout << "r4 is " << r4 << endl;

r3 = ++r4;

cout << "r3 is " << r3 << endl;

cout << "r4 is " << r4 << endl;

r3 = --r4;

cout << "r3 is " << r3 << endl;

cout << "r4 is " << r4 << endl;

// Test function operator for conversion

cout << "1 + " << r4 << " is " << (1 + r4) << endl;

cout<<r4.toString()<<endl;

return 0;

}

lab 7 函数超级多的类的更多相关文章

  1. 回调函数中使用MFC类的成员或对话框控件的简单方法

    在MFC的很多程序中,常常需要在回调函数中调用MFC类的类成员变量.类成员函数,亦或者对话框控件的句柄.由于回调函数是基于C编程的Windows SDK的技术,而类成员又有this指针客观条件限制.. ...

  2. RTTI、虚函数和虚基类的实现方式、开销分析及使用指导(虚函数的开销很小,就2次操作而已)

    白杨 http://baiy.cn “在正确的场合使用恰当的特性” 对称职的C++程序员来说是一个基本标准.想要做到这点,首先要了解语言中每个特性的实现方式及其开销.本文主要讨论相对于传统 C 而言, ...

  3. JAVA之旅(五)——this,static,关键字,main函数,封装工具类,生成javadoc说明书,静态代码块

    JAVA之旅(五)--this,static,关键字,main函数,封装工具类,生成javadoc说明书,静态代码块 周末收获颇多,继续学习 一.this关键字 用于区分局部变量和成员变量同名的情况 ...

  4. 【重构】 代码的坏味道总结 Bad Smell (一) (重复代码 | 过长函数 | 过大的类 | 过长参数列 | 发散式变化 | 霰弹式修改)

    膜拜下 Martin Fowler 大神 , 开始学习 圣经 重构-改善既有代码设计 . 代码的坏味道就意味着需要重构, 对代码的坏味道了然于心是重构的比要前提; . 作者 : 万境绝尘 转载请注明出 ...

  5. C++实验二——函数重载、函数模板、简单类的定义和实现

    一.实验过程 函数重载编程练习 实验要求:编写重载函数add(),实现对int型,double型,complex型数据的加法.在main函数中定义不同类型的数据,调用测试. 代码实现: 先是简单的体验 ...

  6. 我写的RunTime函数之一,为类的某个属性赋值以及方法交换

      1,为属性赋值 #import <UIKit/UIKit.h> @interface UIViewController (RunTime) - (BOOL)setPropertyVal ...

  7. C++ 实验2:函数重载、函数模板、简单类的定义和实现

    1.函数重载编程 编写重载函数add(),实现对int型,double型,Complex型数据的加法.在main()函数中定义不同类型数据,调用测试. #include <iostream> ...

  8. 重构 之 总结代码的坏味道 Bad Smell (一) 重复代码 过长函数 过大的类 过长参数列 发散式变化 霰弹式修改

    膜拜下 Martin Fowler 大神 , 开始学习 圣经 重构-改善既有代码设计 . 代码的坏味道就意味着需要重构, 对代码的坏味道了然于心是重构的比要前提; . 作者 : 万境绝尘 转载请注明出 ...

  9. php里面用魔术方法和匿名函数闭包函数动态的给类里面添加方法

    1.认识  __set  (在给不可访问属性赋值时,__set() 会被调用) 也就是说你再访问一个类里面没有的属性,会出发这个方法 class A{ private $aa = '11'; publ ...

随机推荐

  1. 编译错误:error: multi-line comment

    编译错误:error: multi-line comment  这其实是有宏定义的地方的问题. 原因是宏定义非一行,在宏定义的行尾使用 '\' 连接符导致的. 所以这个地方的注释使用 /*   */ ...

  2. 【解决】SharePoint外部列表保存的日期/时间值不正确

    [问题描述]: 在SharePoint中创建一个外部列表后,通过工作流或直接通过外部列表中的新增向外部列表添加数据项.通过外部列表或数据库查看添加的数据项时发现日期类型字段的值都不正确,像是差了若干个 ...

  3. 5.Integer to Roman && Roman to Integer

    Roman chart: http://literacy.kent.edu/Minigrants/Cinci/romanchart.htm Integer to Roman Given an inte ...

  4. webstrom 2016 破解

    本篇文章经过笔者实验结果,网友均可放心使用 版权声明:本文为博主原创文章,未经博主允许不得转载. 工欲善其事,必先利其器 websrtom作为前端开发神器,现在是越来越多的用户使用.很多小白们下载好了 ...

  5. VBS数组

    定义一个数组: dim a(3).这里要注意在VBS里面数组不像其他的例如C,C#,JAVA等数组用[]作为数组标志.VBS采用的是().还需要注意的是,这里定义的数组包含a(0),a(1),a(2) ...

  6. entityframework 入门-来自微软

    必备条件 要完成本演练,需要安装 Visual Studio 2010 或 Visual Studio 2012. 如果使用的是 Visual Studio 2010,还需要安装 NuGet. 1.创 ...

  7. 【JavaScript】前端插件

    树形结构: http://www.jeasyui.com/documentation/index.php 网上有对这个插件的说明,总的来说这个插件将selected和checked作为两种状态: 1. ...

  8. Hibernate day04笔记

    整合log4j(了解) slf4j 核心jar : slf4j-api-1.6.1.jar .slf4j是日志框架,将其他优秀的日志第三方进行整合.      整合导入jar包     log4j 核 ...

  9. css3颜色

    镂空字的2种做法

  10. 如何在A用户下建立视图,这个视图是A的表与B的表进行关联的?

    这个前提条件是,同一个数据库,不同用户!!!如果是不同数据库,就要用dblink了 一开始,我直接创建视图,但是提示“权限不足”: 于是我是用A登陆,直接用select * from B.sa_tas ...