Lab 7 Functions Super-many classes

Source: Internet
Author: User
Tags arithmetic operators define function gcd

#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::d ivide (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::d Oublevalue () {
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-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 + + +--

Postfix + + +--

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 Functions Super-many classes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.