1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
|
//driver Lab06_Rational_Driver.cpp
#include "RationalNumber.h"
int main()
{
// RationalNumber c( 1, 3 ), d( 2, 4 ), x;
RationalNumber c, d, x;
// test overloaded stream extraction operator
cout << "Enter a Rational Number (n/d): ";
cin >> c;
cout << "Enter a Rational Number (n/d): ";
cin >> d;
x = c + d; // test overloaded operators + and =
cout << c << " + " << d << " = " << x << endl;
x = c - d; // test overloaded operators - and =
cout << c << " - " << d << " = " << x << endl;
x = c * d; // test overloaded operators * and =
cout << c << " * " << d << " = " << x << endl;
x = c / d; // test overloaded operators / and =
cout << c << " / " << d << " = " << x << endl;
// test overloaded > operator
cout << c << ( ( c > d ) ? " > " : " <= " ) << d
<< " according to the overloaded > operator\n";
// test overloaded >= operator
cout << c << ( ( c >= d ) ? " >= " : " < " ) << d
<< " according to the overloaded >= operator\n";
// test overloaded < operator
cout << c << ( ( c < d ) ? " < " : " >= " ) << d
<< " according to the overloaded < operator\n";
// test overloaded <= operator
cout << c << ( ( c <= d ) ? " <= " : " > " ) << d
<< " according to the overloaded <= operator\n";
// test overloaded == operator
cout << c << ( ( c == d ) ? " == " : " != " ) << d
<< " according to the overloaded == operator\n";
// test overloaded != operator
cout << c << ( ( c != d ) ? " != " : " == " ) << d
<< " according to the overloaded != operator\n";
return 0;
} // end main
// RationalNumberClass.h
//
#include <iostream>
using namespace std;
//prevents multiple inclusions of header
#ifndef RATIONALNUMBER_H
#define RATIONALNUMBER_H
class RationalNumber
{
friend ostream &operator<<(ostream &, const RationalNumber & ); //overload stream insertion operator
friend istream &operator>>(istream &, RationalNumber & ); //overload stream extraction operator
public:
RationalNumber(int=1,int=1); //constructor
void setValues(int,int); //mutator function to set data members
void printRationalNumber(); //accessor display function - print fractions
void printRationalAsDouble(); //accessor display function - print decimal answer
int getNumerator(); //accessor read function
int getDenominator(); //accessor read function
RationalNumber operator-(RationalNumber); //overloaded subtr, binary
RationalNumber operator+(RationalNumber); //overloaded add, binary
RationalNumber operator*(RationalNumber); //overloaded multip, binary
RationalNumber operator/(RationalNumber); //overloaded div, binary
bool operator<(RationalNumber); //overloaded less than, binary
bool operator<=(RationalNumber); //overloaded less than or equal to, binary
bool operator>(RationalNumber); //overloaded greater than, binary
bool operator>=(RationalNumber); //overloaded greater than or equal to, binary
bool operator==(RationalNumber); //overloaded equality, binary
bool operator!=(RationalNumber); //overloaded not equal, binary
private:
int numerator; //data member
int denominator; //data member
void reduction(); //prototype for utility function - reduce fraction
}; //end class RationalNumber
#endif
//RationalNumber.cpp Exercise 11.10 Solution
//RationalNumber class member-function definitions
//
#include <iostream>
#include <iomanip>
#include "RationalNumber.h"
using namespace std;
//constructor
RationalNumber::RationalNumber(int numer, int denom)
{
setValues(numer,denom); //mutator function to set the values
reduction(); //reduce the fraction to lowest form
}
void RationalNumber::setValues(int numer,int denom) //mutator function to set values from object
{
numerator=numer;
denominator=denom;
}
void RationalNumber::reduction() //reduce the fraction to lowest form
{
int largest;
largest=numerator > denominator ? numerator : denominator;
int gcd = 0; //greatest common divisor
for (int loop = 2; loop <= largest; loop++)
{
if (numerator % loop == 0 && denominator % loop == 0)
{
gcd = loop;
}
}
if (gcd != 0)
{
numerator = numerator/gcd;
denominator = denominator/gcd;
} //end if
}
int RationalNumber::getNumerator() //access function
{
return numerator;
}
int RationalNumber::getDenominator() //access function
{
return denominator;
}
RationalNumber RationalNumber::operator-(RationalNumber operand2) //subtract the fractions
{
int newDenom = denominator * operand2.getDenominator();
int newNumA = numerator * operand2.getDenominator();
int newNumB = operand2.getNumerator() * denominator;
int newNumTot = newNumA - newNumB;
RationalNumber y(newNumTot, newDenom);
return y;
}
RationalNumber RationalNumber::operator+(RationalNumber operand2) //add the fractions
{
int newDenom = denominator * operand2.getDenominator();
int newNumA = numerator * operand2.getDenominator();
int newNumB = operand2.getNumerator() * denominator;
int newNumTot = newNumA + newNumB;
RationalNumber y(newNumTot, newDenom);
return y;
}
RationalNumber RationalNumber::operator*(RationalNumber &operand2) //multiply the fractions
{
int newDenom = denominator * operand2.getDenominator();
int newNum = numerator * operand2.getNumerator();
RationalNumber y(newNum, newDenom);
return y;
}
RationalNumber RationalNumber::operator/(RationalNumber &operand2) //divide the fractions
{
int newDenom = denominator * operand2.getNumerator();
int newNum = numerator * operand2.getDenominator();
RationalNumber y(newNum, newDenom);
return y;
}
bool RationalNumber::operator<(RationalNumber &operand2) //overloaded less than, binary
{
int newNumA = numerator * operand2.getDenominator();
int newNumB = operand2.getNumerator() * denominator;
if (newNumA < newNumB)
return true;
else
return false;
}
bool RationalNumber::operator<=(RationalNumber &operand2) //overloaded less than or equal to, binary
{
int newNumA = numerator * operand2.getDenominator();
int newNumB = operand2.getNumerator() * denominator;
if (newNumA <= newNumB)
return true;
else
return false;
}
bool RationalNumber::operator>(RationalNumber &operand2) //overloaded greater than, binary
{
int newNumA = numerator * operand2.getDenominator();
int newNumB = operand2.getNumerator() * denominator;
if (newNumA > newNumB)
return true;
else
return false;
}
bool RationalNumber::operator>=(RationalNumber &operand2) //overloaded greater than or equal to, binary
{
int newNumA = numerator * operand2.getDenominator();
int newNumB = operand2.getNumerator() * denominator;
if (newNumA >= newNumB)
return true;
else
return false;
}
bool RationalNumber::operator==(RationalNumber &operand2) //overloaded equality, binary
{
int newNumA = numerator * operand2.getDenominator();
int newNumB = operand2.getNumerator() * denominator;
if (newNumA = newNumB)
return true;
else
return false;
}
bool RationalNumber::operator!=(RationalNumber &operand2) //overloaded not equal, binary
{
int newNumA = numerator * operand2.getDenominator();
int newNumB = operand2.getNumerator() * denominator;
if (newNumA != newNumB)
return true;
else
return false;
}
ostream &operator<<(ostream &output, const RationalNumber &number ) //overload stream insertion operator
{
output << number.numerator << "/" << number.denominator;
return output;
}
istream &operator>>(istream &input, RationalNumber &number ) //overload stream extraction operator
{
input >> setw(1) >> number.numerator;
input.ignore();
input >> setw(1) >> number.denominator;
return input;
}
|