|
| 1 | +#include <iostream> |
| 2 | +#include <string> |
| 3 | +#include <sstream> |
| 4 | +#include <ctime> |
| 5 | +#include <iomanip> |
| 6 | +#include <vector> |
| 7 | +using namespace std; |
| 8 | + |
| 9 | +class Fraction; |
| 10 | +class RandomIntegerGenerator; |
| 11 | +class RandomFractionGenerator; |
| 12 | +class Integer; |
| 13 | +class FractionToLowestTermUIConverter; |
| 14 | +class FractionToDecimalUIConverter; |
| 15 | +class FractionToPercentageUIConverter; |
| 16 | +class FractionToStringUIConverter; |
| 17 | + |
| 18 | +class Fraction { |
| 19 | +private: |
| 20 | + long long _num; |
| 21 | + long long _den; |
| 22 | + |
| 23 | +public: |
| 24 | + Fraction() { _num = 0; _den = 1; } |
| 25 | + Fraction(long long num, long long den) { _num = num; _den = den; } |
| 26 | + |
| 27 | +public: |
| 28 | + long long getNum() const { return _num; } |
| 29 | + long long getDen() const { return _den; } |
| 30 | +}; |
| 31 | + |
| 32 | +class RandomIntegerGenerator { |
| 33 | +public: |
| 34 | + RandomIntegerGenerator() { |
| 35 | + srand(time(NULL)); |
| 36 | + } |
| 37 | + int next() { return rand(); } |
| 38 | + int next(int ceiling) { return rand() % ceiling; } |
| 39 | + int next(int left, int right) { return rand() % (right - left + 1) + left; } |
| 40 | +}; |
| 41 | + |
| 42 | +class RandomFractionGenerator { |
| 43 | +private: |
| 44 | + RandomIntegerGenerator _rig; |
| 45 | + |
| 46 | +public: |
| 47 | + Fraction next() { |
| 48 | + long long num = _rig.next(); |
| 49 | + long long den = _rig.next(); |
| 50 | + Fraction f(num, den); |
| 51 | + return f; |
| 52 | + } |
| 53 | + Fraction next(int ceiling) { |
| 54 | + long long num = _rig.next(ceiling); |
| 55 | + long long den = _rig.next(ceiling); |
| 56 | + Fraction f(num, den); |
| 57 | + return f; |
| 58 | + } |
| 59 | + Fraction next(int left, int right) { |
| 60 | + long long num = _rig.next(left, right); |
| 61 | + long long den = _rig.next(left, right); |
| 62 | + Fraction f(num, den); |
| 63 | + return f; |
| 64 | + } |
| 65 | +}; |
| 66 | + |
| 67 | +class Integer { |
| 68 | +public: |
| 69 | + static int gcd(int a, int b) { |
| 70 | + if (a == 0) return b; |
| 71 | + |
| 72 | + if (b == 0) return a; |
| 73 | + |
| 74 | + if (a == b) return a; |
| 75 | + |
| 76 | + if (a > b) return gcd(a - b, b); |
| 77 | + |
| 78 | + return gcd(a, b - a); |
| 79 | + } |
| 80 | +}; |
| 81 | + |
| 82 | +class FractionToLowestTermUIConverter { |
| 83 | +public: |
| 84 | + string Convert(const Fraction& f) { |
| 85 | + stringstream ss; |
| 86 | + long long gcd = Integer::gcd(f.getNum(), f.getDen()); |
| 87 | + long long num = f.getNum() / gcd; |
| 88 | + long long den = f.getDen() / gcd; |
| 89 | + |
| 90 | + if (num > den) { |
| 91 | + long long fullNum = num / den; |
| 92 | + ss << fullNum << " "; |
| 93 | + num = num / den; |
| 94 | + } |
| 95 | + |
| 96 | + if (num > 0) { |
| 97 | + ss << num; |
| 98 | + |
| 99 | + if (den != 1) ss << "/" << den; |
| 100 | + } |
| 101 | + |
| 102 | + string ans = ss.str(); |
| 103 | + return ans; |
| 104 | + } |
| 105 | +}; |
| 106 | + |
| 107 | +class FractionToDecimalUIConverter { |
| 108 | +private: |
| 109 | + int _precision; |
| 110 | + bool _isFixed; |
| 111 | + |
| 112 | +public: |
| 113 | + FractionToDecimalUIConverter(int precision = 3, bool isFixed = false) { |
| 114 | + this->_precision = precision; |
| 115 | + this->_isFixed = isFixed; |
| 116 | + } |
| 117 | + |
| 118 | +public: |
| 119 | + string Convert(const Fraction& f) { |
| 120 | + stringstream ss; |
| 121 | + float value = f.getNum() * 1.0 / f.getDen() * 1.0; |
| 122 | + |
| 123 | + ss << setprecision(_precision); |
| 124 | + if (_isFixed) ss << fixed; |
| 125 | + ss << value; |
| 126 | + |
| 127 | + string ans = ss.str(); |
| 128 | + return ans; |
| 129 | + } |
| 130 | +}; |
| 131 | + |
| 132 | +class FractionToPercentageUIConverter { |
| 133 | +private: |
| 134 | + int _precision; |
| 135 | + bool _isFixed; |
| 136 | + |
| 137 | +public: |
| 138 | + FractionToPercentageUIConverter(int precision = 3, bool isFixed = false) { |
| 139 | + this->_precision = precision; |
| 140 | + this->_isFixed = isFixed; |
| 141 | + } |
| 142 | + |
| 143 | +public: |
| 144 | + string Convert(const Fraction& f) { |
| 145 | + stringstream ss; |
| 146 | + float value = f.getNum() * 100.0 / f.getDen(); |
| 147 | + |
| 148 | + ss << setprecision(_precision); |
| 149 | + if (_isFixed) ss << fixed; |
| 150 | + ss << value << "%"; |
| 151 | + |
| 152 | + string ans = ss.str(); |
| 153 | + return ans; |
| 154 | + } |
| 155 | +}; |
| 156 | + |
| 157 | +class FractionToStringUIConverter { |
| 158 | +public: |
| 159 | + string Convert(const Fraction& f) { |
| 160 | + stringstream ss; |
| 161 | + ss << f.getNum() << "/" << f.getDen(); |
| 162 | + string ans = ss.str(); |
| 163 | + return ans; |
| 164 | + } |
| 165 | +}; |
| 166 | + |
| 167 | +int main() { |
| 168 | + const int left = 15; |
| 169 | + const int right = 25; |
| 170 | + |
| 171 | + RandomIntegerGenerator rig; |
| 172 | + int n = rig.next(left, right); |
| 173 | + cout << "There are " << n << " fractions: " << endl; |
| 174 | + |
| 175 | + const int minNumDen = 1; |
| 176 | + const int maxNumDen = 50; |
| 177 | + RandomFractionGenerator rfg; |
| 178 | + |
| 179 | + vector<Fraction> mylist; |
| 180 | + |
| 181 | + for (int i = 0; i < n; i++) { |
| 182 | + Fraction f = rfg.next(minNumDen, maxNumDen); |
| 183 | + mylist.push_back(f); |
| 184 | + } |
| 185 | + |
| 186 | + FractionToStringUIConverter stringConverter; |
| 187 | + for (int i = 0; i < mylist.size(); i++) { |
| 188 | + string result = stringConverter.Convert(mylist[i]); |
| 189 | + cout << result << ", "; |
| 190 | + } |
| 191 | + cout << endl << endl; |
| 192 | + |
| 193 | + cout << "These are fractions that have been converted to the lowest term: " << endl; |
| 194 | + FractionToLowestTermUIConverter lowestTermConverter; |
| 195 | + for (int i = 0; i < mylist.size(); i++) { |
| 196 | + string result = lowestTermConverter.Convert(mylist[i]); |
| 197 | + cout << result << ", "; |
| 198 | + } |
| 199 | + cout << endl << endl; |
| 200 | + |
| 201 | + cout << "These are numbers that fractions have been converted to decimal: " << endl; |
| 202 | + int precision = 4; |
| 203 | + bool isFixed = true; |
| 204 | + |
| 205 | + FractionToDecimalUIConverter decimalConverter(precision, isFixed); |
| 206 | + for (int i = 0; i < mylist.size(); i++) { |
| 207 | + string result = decimalConverter.Convert(mylist[i]); |
| 208 | + cout << result << ", "; |
| 209 | + } |
| 210 | + cout << endl << endl; |
| 211 | + |
| 212 | + cout << "These are numbers that fractions have been converted to percentage: " << endl; |
| 213 | + FractionToPercentageUIConverter percentageConverter(precision, isFixed); |
| 214 | + for (int i = 0; i < mylist.size(); i++) { |
| 215 | + string result = percentageConverter.Convert(mylist[i]); |
| 216 | + cout << result << ", "; |
| 217 | + } |
| 218 | + cout << endl; |
| 219 | + |
| 220 | + cin.get(); |
| 221 | +} |
0 commit comments