标签:friend main ring end ber stat [] input com
#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;
}
标签:friend main ring end ber stat [] input com
原文地址:http://www.cnblogs.com/sysu-eeman-yang/p/5987540.html