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
|
//driver.cpp
#include "Fraction.h"
#include <iostream>
#include <cassert>
using namespace std;
int main()
{
bool chosen = false; //while ctrlStruct bvarInit
while(!chosen)
{
cout << "Choose a function:" << endl
<< "1:Convert a Fraction to a decimal value" << endl
<< "2:Convert an integer to a Fraction value" << endl
<< "3:Add two Fractions together" << endl
<< "4:Quit" << endl
<< "Enter a choice: ";
int choice; //switch ctrlStruct ivarDecl
int fint; //intToFract ivarDecl
Fraction fract, //dependent FvarDecl
fract2; //addFract FvarDecl
cin >> choice;
switch(choice)
{
case 1:
cin >> fract; //dependent FvarInit
cout << fract.fractToDeci() << endl;
chosen = true; //while ctrlStruct bvarMutn
break;
case 2:
cout << "Enter an integer: ";
cin >> fint; //intToFract ivarInit
fract = Fraction(fint);
cout << fract;
chosen = true; //while ctrlStruct bvarMutn
break;
case 3:
cout << "First fraction:" << endl;
cin >> fract;
cout << "Second fraction:" << endl;
cin >> fract2;
cout << (fract + fract2);
chosen = true; //while ctrlStruct bvarMutn
break;
case 4:
chosen = true;
break;
}
}
return 0;
}
//Fraction.h
#include <iostream>
using namespace std;
class Fraction
{
private:
int num;
int den;
void reduce();
public:
void print(ostream& os = cout); //Prints a Fraction in form num / den
void read(istream& is = cin); //Sets a Fraction to inputed values
Fraction();
Fraction(int num);
Fraction(int num, int den);
Fraction(const Fraction& fract);
double fractToDeci(); //Converts a Fraction to a double
void addFract(Fraction fract); //Adds two fractions
friend ostream& operator<<(ostream& os, const Fraction& f);
friend istream& operator>>(istream& is, Fraction& f);
friend Fraction operator+(const Fraction& f1, const Fraction& f2);
};
//the class definition file: Fraction.cpp
#include "Fraction.h"
#include <iostream>
#include <cassert>
using namespace std;
void Fraction::reduce()
{
int tempBig, tempSml, tempMod;
if (num > den)
{
tempBig = num,
tempSml = den;
do
tempMod = tempBig % tempSml,
tempBig = tempSml,
tempSml = tempMod;
while ( tempBig % tempSml != 0);
num /= tempMod,
den /= tempMod;
}
else if (num < den)
{
tempBig = den,
tempSml = num;
do
tempMod = tempBig % tempSml,
tempBig = tempSml,
tempSml = tempMod;
while ( tempBig % tempSml != 0);
den /= tempMod,
num /= tempMod;
}
else if (num == den)
num = 1,
den = 1;
}
void Fraction::print(ostream& os)
{
os << num << "/" << den << endl;
}
void Fraction::read(istream& is)
{
cout << "Enter the numerator: ";
is >> num;
cout << "Enter the denominator: ";
is >> den;
assert(den != 0);
reduce();
}
Fraction::Fraction()
{
num = 0,
den = 1;
}
Fraction::Fraction(int n)
{
num = n;
den = 1;
}
Fraction::Fraction(int n, int d)
{
num = n,
den = d;
assert(den != 0);
}
Fraction::Fraction(const Fraction& fract)
{
num = fract.num,
den = fract.den;
}
double Fraction::fractToDeci()
{
double dfract = num / static_cast <double> (den);
return dfract;
}
void Fraction::addFract(Fraction fract)
{
num = num * fract.den + den * fract.num;
den = den * fract.den;
reduce();
}
ostream& operator<<(ostream& os, Fraction& f)
{
f.print(os);
return os;
}
istream& operator>>(istream& is, Fraction& f)
{
f.read(is);
return is;
}
Fraction operator+(const Fraction& f1, const Fraction& f2)
{
Fraction fsum;
fsum.addFract(f1);
fsum.addFract(f2);
return fsum;
}
|