|
| 1 | +#include <iostream> |
| 2 | +#include <ctime> |
| 3 | +#include <sstream> |
| 4 | +#include <string> |
| 5 | +#include <regex> |
| 6 | +#include <tuple> |
| 7 | +#include <fstream> |
| 8 | +#include <vector> |
| 9 | + |
| 10 | +using namespace std; |
| 11 | + |
| 12 | +class Integer; |
| 13 | +class Fraction; |
| 14 | +class FractionToLowestTermUIConverter; |
| 15 | +class RandomIntegerGenerator; |
| 16 | +class RandomFractionGenerator; |
| 17 | +class FractionToStringDataConverter; |
| 18 | +class FractionDataReader; |
| 19 | + |
| 20 | +class Integer { |
| 21 | +public: |
| 22 | + static int gcd(int a, int b) { |
| 23 | + if (a == 0) return b; |
| 24 | + if (b == 0) return a; |
| 25 | + if (a == b) return a; |
| 26 | + if (a > b) return gcd(a - b, b); |
| 27 | + return gcd(a, b - a); |
| 28 | + } |
| 29 | +}; |
| 30 | + |
| 31 | +class Fraction { |
| 32 | +private: |
| 33 | + long long _num; |
| 34 | + long long _den; |
| 35 | +public: |
| 36 | + long long Numerator() const { return _num; } |
| 37 | + long long Denominator() const { return _den; } |
| 38 | + |
| 39 | + void setNum(long long num) { _num = num; } |
| 40 | + void setDen(long long den) { _den = den; } |
| 41 | + |
| 42 | + Fraction() { _num = 0; _den = 1; } |
| 43 | + Fraction(long long num, long long den) { _num = num; _den = den; } |
| 44 | + |
| 45 | + static Fraction addTwoFractions(const Fraction& a, const Fraction& b) { |
| 46 | + Fraction sum; |
| 47 | + |
| 48 | + sum.setNum(a.Denominator() * b.Numerator() + a.Numerator() * b.Denominator()); |
| 49 | + sum.setDen(a.Denominator() * b.Denominator()); |
| 50 | + long long gcd = Integer::gcd(sum.Numerator(), sum.Denominator()); |
| 51 | + sum.setNum(sum.Numerator() / gcd); |
| 52 | + sum.setDen(sum.Denominator() / gcd); |
| 53 | + return sum; |
| 54 | + } |
| 55 | +}; |
| 56 | + |
| 57 | +class FractionToLowestTermUIConverter { |
| 58 | +public: |
| 59 | + string Convert(const Fraction& f) { |
| 60 | + stringstream ss; |
| 61 | + long long gcd = Integer::gcd(f.Numerator(), f.Denominator()); |
| 62 | + long long num = f.Numerator() / gcd; |
| 63 | + long long den = f.Denominator() / gcd; |
| 64 | + |
| 65 | + if (num > den) { |
| 66 | + long long fullNum = num / den; |
| 67 | + ss << fullNum << " "; |
| 68 | + num = num % den; |
| 69 | + } |
| 70 | + |
| 71 | + if (num > 0) { |
| 72 | + ss << num; |
| 73 | + |
| 74 | + if (den != 1) ss << "/" << den; |
| 75 | + } |
| 76 | + |
| 77 | + string ans = ss.str(); |
| 78 | + return ans; |
| 79 | + } |
| 80 | +}; |
| 81 | + |
| 82 | +class RandomIntegerGenerator { |
| 83 | +public: |
| 84 | + RandomIntegerGenerator() { |
| 85 | + srand(time(NULL)); |
| 86 | + } |
| 87 | + int next() { return rand(); } |
| 88 | + int next(int ceiling) { return rand() % ceiling; } |
| 89 | + int next(int left, int right) { return rand() % (right - left + 1) + left; } |
| 90 | +}; |
| 91 | + |
| 92 | +class RandomFractionGenerator { |
| 93 | +private: |
| 94 | + RandomIntegerGenerator _rig; |
| 95 | + |
| 96 | +public: |
| 97 | + Fraction next() { |
| 98 | + long long num = _rig.next(); |
| 99 | + long long den = _rig.next(); |
| 100 | + Fraction f(num, den); |
| 101 | + return f; |
| 102 | + } |
| 103 | + Fraction next(int ceiling) { |
| 104 | + long long num = _rig.next(ceiling); |
| 105 | + long long den = _rig.next(ceiling); |
| 106 | + Fraction f(num, den); |
| 107 | + return f; |
| 108 | + } |
| 109 | + Fraction next(int left, int right) { |
| 110 | + long long num = _rig.next(left, right); |
| 111 | + long long den = _rig.next(left, right); |
| 112 | + Fraction f(num, den); |
| 113 | + return f; |
| 114 | + } |
| 115 | +}; |
| 116 | + |
| 117 | +class FractionToStringDataConverter { |
| 118 | +public: |
| 119 | + string convert(const Fraction& f) { |
| 120 | + stringstream ss; |
| 121 | + ss << f.Numerator() << "/" << f.Denominator(); |
| 122 | + string result = ss.str(); |
| 123 | + return result; |
| 124 | + } |
| 125 | + |
| 126 | + bool isValidFraction(string value) { |
| 127 | + bool result; |
| 128 | + const string patterns = "\\b\\d+/[1-9][0-9]*\\b"; |
| 129 | + regex fractionsFormat(patterns); |
| 130 | + result = regex_match(value, fractionsFormat); |
| 131 | + return result; |
| 132 | + } |
| 133 | + |
| 134 | + Fraction convertBack(string value) { |
| 135 | + bool isValid = isValidFraction(value); |
| 136 | + if (isValid) { |
| 137 | + int slash = value.find('/'); |
| 138 | + string numString = value.substr(0, slash); |
| 139 | + string denString = value.substr(slash + 1, value.length() - slash - 1); |
| 140 | + |
| 141 | + long long num = stoi(numString); |
| 142 | + long long den = stoi(denString); |
| 143 | + |
| 144 | + Fraction f(num, den); |
| 145 | + return f; |
| 146 | + } |
| 147 | + else { |
| 148 | + throw runtime_error("Wrong format"); |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + bool tryConvertBack(string buffer, Fraction& f) { |
| 153 | + bool result = true; |
| 154 | + bool isValid = isValidFraction(buffer); |
| 155 | + if (isValid) { |
| 156 | + f = convertBack(buffer); |
| 157 | + } |
| 158 | + else { result = false; } |
| 159 | + return result; |
| 160 | + } |
| 161 | +}; |
| 162 | + |
| 163 | +class FractionDataReader { |
| 164 | +private: |
| 165 | + string _connectingString; |
| 166 | +public: |
| 167 | + FractionDataReader(string connectingString) { |
| 168 | + _connectingString = connectingString; |
| 169 | + } |
| 170 | + |
| 171 | + vector<Fraction>GetAll() { |
| 172 | + ifstream input(_connectingString); |
| 173 | + vector<Fraction> f; |
| 174 | + |
| 175 | + while (!input.eof()) { |
| 176 | + string line; |
| 177 | + getline(input, line); |
| 178 | + Fraction a; |
| 179 | + bool checkOK; |
| 180 | + FractionToStringDataConverter fConvert; |
| 181 | + try { |
| 182 | + checkOK = fConvert.tryConvertBack(line, a); |
| 183 | + } |
| 184 | + catch (runtime_error e) { |
| 185 | + checkOK = false; |
| 186 | + cout << e.what() << endl; |
| 187 | + } |
| 188 | + if (checkOK) { |
| 189 | + f.push_back(a); |
| 190 | + } |
| 191 | + } |
| 192 | + input.close(); |
| 193 | + return f; |
| 194 | + } |
| 195 | +}; |
| 196 | + |
| 197 | +class FractionDataWriter { |
| 198 | +private: |
| 199 | + string _connectingString; |
| 200 | +public: |
| 201 | + FractionDataWriter(string connectingString) { |
| 202 | + _connectingString = connectingString; |
| 203 | + } |
| 204 | + |
| 205 | + void Write(const Fraction& f) { |
| 206 | + ofstream output(_connectingString, ios::app); |
| 207 | + FractionToStringDataConverter fConvert; |
| 208 | + string line = fConvert.convert(f); |
| 209 | + output << line << endl; |
| 210 | + output.close(); |
| 211 | + } |
| 212 | +}; |
| 213 | + |
| 214 | +int main() { |
| 215 | + FractionDataReader reader("data.txt"); |
| 216 | + vector<Fraction> Mylist = reader.GetAll(); |
| 217 | + |
| 218 | + FractionToStringDataConverter fts; |
| 219 | + FractionToLowestTermUIConverter lts; |
| 220 | + RandomFractionGenerator rfg; |
| 221 | + Fraction f; |
| 222 | + FractionDataWriter writer("data.txt"); |
| 223 | + |
| 224 | + cout << "All of the fractions in the list: " << endl; |
| 225 | + for (int i = 0; i < Mylist.size(); i++) { |
| 226 | + cout << lts.Convert(Mylist[i]) << ", "; |
| 227 | + } |
| 228 | + |
| 229 | + cout << endl << "Two random fractions:" << endl; |
| 230 | + for (int i = 0; i < 2; i++) { |
| 231 | + f = rfg.next(1, 99); |
| 232 | + cout << fts.convert(f) << " "; |
| 233 | + Mylist.push_back(f); |
| 234 | + writer.Write(f); |
| 235 | + } |
| 236 | + Fraction sum; |
| 237 | + for (int i = 0; i < Mylist.size(); i++) { |
| 238 | + sum = Fraction::addTwoFractions(sum, Mylist[i]); |
| 239 | + } |
| 240 | + cout << endl << "The total of two fractions is: " << lts.Convert(sum) << endl; |
| 241 | + |
| 242 | + cout << endl << "Press any key to continue..." << endl; |
| 243 | + cin.get(); |
| 244 | + return 0; |
| 245 | +} |
0 commit comments