hpnq

Антоха лепёха

Jul 18th, 2024 (edited)
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.48 KB | None | 0 0
  1. #include <iostream>
  2. #include <sstream>
  3. #include <string>
  4. //#include <numeric>
  5.  
  6. using namespace std;
  7. int gcd(int a, int b)
  8. {
  9.     // Everything divides 0
  10.     if (a == 0)
  11.         return b;
  12.     if (b == 0)
  13.         return a;
  14.  
  15.     // base case
  16.     if (a == b)
  17.         return a;
  18.  
  19.     // a is greater
  20.     if (a > b)
  21.         return gcd(a - b, b);
  22.     return gcd(a, b - a);
  23. }
  24. int lcm(int a, int b){
  25.     return a * b / gcd(a, b);
  26. }
  27.  
  28. class Fraction {
  29. public:
  30.     int whole;
  31.     int numerator;
  32.     int denominator;
  33.  
  34.     Fraction(int w, int n, int d) : whole(w), numerator(n), denominator(d) {
  35.         normalize();
  36.     }
  37.  
  38.     Fraction(int n, int d) : whole(0), numerator(n), denominator(d) {
  39.         normalize();
  40.     }
  41.     void reduce(){
  42.         numerator += denominator * whole;
  43.         whole = 0;
  44.         int d = gcd(abs(numerator), abs(denominator));
  45.         numerator = numerator / d;
  46.         denominator = denominator / d;
  47.     }
  48.     void normalize() {
  49.         if (denominator < 0) {
  50.             numerator = -numerator;
  51.             denominator = -denominator;
  52.         }
  53.         reduce();
  54.  
  55.         whole += numerator / denominator;
  56.         numerator = abs(numerator % denominator);
  57.  
  58.         if (numerator == 0) {
  59.             denominator = 1;
  60.         }
  61.  
  62.         if (whole == 0 && numerator < 0) {
  63.             whole = -1;
  64.             numerator = abs(numerator);
  65.         }
  66.     }
  67.  
  68.     string toString() const {
  69.         stringstream ss;
  70.         if (whole != 0 || numerator == 0) {
  71.             ss << whole;
  72.         }
  73.         if (numerator != 0) {
  74.             if (whole != 0) {
  75.                 ss << " ";
  76.             }
  77.             ss << numerator << "/" << denominator;
  78.         }
  79.         return ss.str();
  80.     }
  81.  
  82.     static Fraction fromString(const string& str) {
  83.         istringstream iss(str);
  84.         int w = 0, n = 0, d = 1;
  85.         if (str.find(' ') != string::npos) {
  86.             iss >> w;
  87.             char slash;
  88.             iss >> n >> slash;
  89.             iss >> d;
  90.         } else if (str.find('/') != string::npos) {
  91.             iss >> n;
  92.             char slash;
  93.             iss >> slash >> d;
  94.  
  95.         } else {
  96.             iss >> w;
  97.         }
  98.  
  99.         return Fraction(w, n, d);
  100.     }
  101.  
  102.     Fraction operator+(const Fraction& other) const {
  103.         int lcm_denom = lcm(denominator, other.denominator);
  104.         cout << "lcm_denom: " << lcm_denom << endl;
  105.         int sum_num = (whole * denominator + numerator)  * (lcm_denom / denominator)
  106.                 + (other.whole * other.denominator + other.numerator) * (lcm_denom / other.denominator);
  107.         cout << "sum_num: " << sum_num << " numerator: " << whole * denominator + numerator << endl;
  108. //        cout << whole << " " << numerator << " " << denominator << endl;
  109.         return Fraction(0, sum_num, lcm_denom);
  110.     }
  111. //    10 1/3 + 2/3
  112.     Fraction operator-(const Fraction& other) const {
  113.         int lcm_denom = lcm(denominator, other.denominator);
  114.         cout << "lcm_denom: " << lcm_denom << endl;
  115.         int sum_num = (whole * denominator + numerator)  * (lcm_denom / denominator)
  116.                       - (other.whole * other.denominator + other.numerator) * (lcm_denom / other.denominator);
  117.         cout << "sum_num: " << sum_num << " numerator: " << whole * denominator + numerator << endl;
  118. //        cout << whole << " " << numerator << " " << denominator << endl;
  119.         return Fraction(0, sum_num, lcm_denom);
  120.     }
  121.  
  122.     Fraction operator*(const Fraction& other) const {
  123.         int new_num = (whole * denominator + numerator) * (other.whole * other.denominator + other.numerator);
  124.         int new_denom = denominator * other.denominator;
  125.         cout << "new_num: " << new_num << endl;
  126.         cout << "new_num1: " << whole * denominator + numerator << endl;
  127.         cout << "new_num2: " << other.whole * other.denominator + other.numerator << endl;
  128.  
  129.         return Fraction(0, new_num, new_denom);
  130.     }
  131.  // 10/6 = 5/3 = 1 2/3
  132.     Fraction operator/(const Fraction& other) const {
  133.         int new_num = (whole * denominator + numerator) * other.denominator;
  134.         int new_denom = denominator * (other.whole * other.denominator + other.numerator);
  135.         cout << "new_num: " << new_num << endl;
  136.         cout << "new_num1: " << whole * denominator + numerator << endl;
  137.         cout << "new_num2: " << other.whole * other.denominator + other.numerator << endl;
  138.  
  139.         return Fraction(0, new_num, new_denom);
  140.     }
  141. };
  142.  
  143. int main() {
  144.  
  145.     setlocale(LC_ALL, "Russian");
  146.     string frac1_str, frac2_str;
  147.     char operation;
  148.  
  149.     cout << "Введите первую дробь: ";
  150.     getline(cin, frac1_str);
  151.  
  152.     cout << "Введите знак операции (+, -, *, /): ";
  153.     cin >> operation;
  154.     cin.ignore();
  155.  
  156.     cout << "Введите вторую дробь: ";
  157.     getline(cin, frac2_str);
  158.  
  159.     Fraction frac1 = Fraction::fromString(frac1_str);
  160.     Fraction frac2 = Fraction::fromString(frac2_str);
  161.  
  162.     Fraction result(0, 0, 1);
  163.  
  164.     switch (operation) {
  165.         case '+':
  166.             result = frac1 + frac2;
  167.             break;
  168.         case '-':
  169.             result = frac1 - frac2;
  170.             break;
  171.         case '*':
  172.             result = frac1 * frac2;
  173.             break;
  174.         case '/':
  175.             result = frac1 / frac2;
  176.             break;
  177.         default:
  178.             cerr << "Неизвестная операция!" << endl;
  179.             return 1;
  180.     }
  181.  
  182.     cout << "Результат: " << result.toString() << endl;
  183.  
  184.     return 0;
  185. }
  186.  
Advertisement
Add Comment
Please, Sign In to add comment