#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. RH LINUX5.5 RAW绑定

    ****************ORACLE 11G RAC***********************Disk /dev/sdb: 2147 MB, 2147483648 bytes67 head ...

  2. hihocoder 1391 [扫描线]

    /* 题意: 两方对阵,互发导弹.防护罩可以让导弹原速反向. 每一枚导弹有发射时间航行时间伤害值. 防护罩也有开启时间和防御时间. 其中一方防护罩开启时间已知,求另一方防护罩合理安排开启时间使得己方受 ...

  3. [CF245H] Queries for Number of Palindromes (容斥原理dp计数)

    题目链接:http://codeforces.com/problemset/problem/245/H 题目大意:给你一个字符串s,对于每次查询,输入为一个数对(i,j),输出s[i..j]之间回文串 ...

  4. (String)151. Reverse Words in a String

    Given an input string, reverse the string word by word. For example,Given s = "the sky is blue& ...

  5. zmap使用笔记

    zmap使用笔记 zmap, 一个网络端口开放性的快速扫描工具.至于这个工具的特色,配置参数,和比的工具的对比,不做介绍.只记录一下近期使用过程中,遇到的问题.软件版本:2.1.1 传言与现实 传言: ...

  6. openldap主机访问控制(基于ip)

    http://blog.oddbit.com/2013/07/22/generating-a-membero/ http://gsr-linux.blogspot.jp/2011/01/howto-o ...

  7. oneCMDB

    OneCMDB开源地址: http://www.oschina.net/p/onecmdb/, 官方网站:http://www.onecmdb.org/wiki/index.php?title=Mai ...

  8. Winform数据导出Execl小工具

    前台界面.cs文件 using System; using System.Collections.Generic; using System.ComponentModel; using System. ...

  9. awk用法

    目前虽然有很多工具可以代替awk,但是呢我还是认为awk还是非常重要,比如有时候load数据到hive,mysql发现数据有点问题,这样可以先对比文件和库中数据是否一致,这样awk就发挥用处了,还有从 ...

  10. 关于Nios II的启动分析(转载)

    原文地址:http://hi.baidu.com/goatdai/item/cc33671545d89243e75e06ad 常用到的存储器包括SDRMA.SRAM.FLASH.Onchip_memo ...