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
|
#include <iostream>
#include <cmath>
using namespace std;
class rational
{
public:
void input();
//Pre-condition: User is prompted to enter numerator
//then denominator for a rational number
//Post-condition: calling rational object is filled with
//values entered by the user.
void display();
//Post-condition: displays the contents of the calling
//rational object in the following format: a / b
int compare(const rational&r2) const;
//Pre-condition: Address of a rational class passed.
//Post-condition: Returns 1 if calling object is greater than
//r2; 0 if r1 is equal to r2; -1 is r1 is less than r2.
private:
int GCD() const;
// Post-condition: Returns the "greatest common divisor"
//between the numerator and denominator of the calling
//rational object.
int a; // numerator
int b; // denominator; b != 0
};
int fillArray(rational r[], int size);
//Pre-Condition: address and size of rational array passed
//Post-Condition: returns the actual number of
//rational numbers entered
void displayArray(rational r[], int n);
//Pre-Condition: address of the array and actual number
//entered passed of rational numbers
//Post-Condition: displays n rational numbers
void selectionSort(rational r[], int n);
//Pre-condition: address of the array and actual number of
//rational numbers entered passed
//Post-condition: n rational numbers in the array
//are sorted in ascending order
int findSmallestRationalNumber(rational r[], int first, int n);
//Pre-Condition: address of array passed,
//subscript value of the first
//element of the unsorted sub-list, and the actual
//number of rational numbers entered passed.
//Post-Condition: returns subscript value of the
//smallest rational number in the unsorted sub-list of the array
void swap(rational &a, rational &b);
//Pre-Condition: Address of classes passed.
//Post-Condition: This functions swaps the numbers
int main()
{
const int SIZE = 10;
rational r[SIZE];
int n;
n = fillArray(r, SIZE);
cout << endl << "Before sort the array contains: \n";
displayArray(r, n);
selectionSort(r, n);
cout << endl << "After sort the array contains: \n";
displayArray(r, n);
system ("PAUSE");
return (0);
}
void rational :: input()
{
cout << "Please enter numerator and denominator: \n";
cin >> a >> b;
cout << endl;
}
void rational :: display()
{
int gcd = GCD();
if (b == 0)
{
cout << "Denominator cannot equal 0\n";
exit(1);
}
if (b < 0)
{
b = -(b);
a = -(a);
}
cout << a / gcd << "/" << b / gcd;
}
int rational :: compare(const rational&r2) const
{
double r1total, r2total;
r1total = (double) a/b;
r2total = (double) r2.a/r2.b;
if (r1total > r2total)
return 1;
else if (r1total == r2total)
return 0;
else
return -1;
}
int rational :: GCD() const
{
int n = abs(a);
int d = abs(b);
while(d != 0)
{
int temp = d;
d = n % d;
n = temp;
}
return n;
}
int fillArray(rational r[], int size)
{
int i = 0;
char more = 'Y';
cout<<"You may enter up to " << size <<" rational numbers:\n\n";
do
{
r[i].input();
cout << endl;
cout << "More rational numbers (Y/N)? ";
cin >> more;
cout << endl << endl;
++i;
}
while(more == 'Y'|| more == 'y');
return (i);
}
void displayArray(rational r[], int n)
{
cout << endl;
for(int i = 0; i < n; i++)
{
r[i].display();
cout << " ";
}
}
void selectionSort(rational r[], int n)
{
int i, pos;
for(i = 0; i < n; i++)
{
pos = findSmallestRationalNumber(r, i, n);
swap(r[i], r[pos]);
}
}
int findSmallestRationalNumber(rational r[], int first, int n)
{
int pos = first;
rational smallest = r[pos];
for(int i = first + 1; i < n; i++)
{
if(r[i].compare(smallest) == -1)
{
smallest = r[i];
pos = i;
}
}
return pos;
}
void swap(rational &a, rational &b)
{
rational temp;
temp = a;
a = b;
b = temp;
}
|