Advertisement
Guest User

Untitled

a guest
Nov 18th, 2019
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.06 KB | None | 0 0
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <string>
  4. #include <sstream>
  5. #include <exception>
  6. #include <vector>
  7.  
  8. using namespace std;
  9.  
  10. int GCD(int a, int b) {
  11.     if (b == 0) {
  12.         return a;
  13.     } else {
  14.         return GCD(b, a % b);
  15.     }
  16. }
  17.  
  18. class Rational {
  19. public:
  20.     Rational() {
  21.         numerator = 0;
  22.         denominator = 1;
  23.     }
  24.  
  25.     Rational(int new_numerator, int new_denominator) {
  26.         if (new_denominator == 0) {
  27.             throw invalid_argument("Invalid argument");
  28.         }
  29.         const int gcd = GCD(new_numerator, new_denominator);
  30.         numerator = new_numerator / gcd;
  31.         denominator = new_denominator / gcd;
  32.         if (denominator < 0) {
  33.             denominator = -denominator;
  34.             numerator = -numerator;
  35.         }
  36.     }
  37.  
  38.     int Numerator() const {
  39.         return numerator;
  40.     }
  41.  
  42.     int Denominator() const {
  43.         return denominator;
  44.     }
  45.  
  46. private:
  47.     int numerator;
  48.     int denominator;
  49. };
  50.  
  51. Rational operator+(const Rational &lhs, const Rational &rhs) {
  52.     return {lhs.Numerator() * rhs.Denominator() + rhs.Numerator() * lhs.Denominator(),
  53.             lhs.Denominator() * rhs.Denominator()};
  54. };
  55.  
  56. Rational operator-(const Rational &lhs, const Rational &rhs) {
  57.     return {lhs.Numerator() * rhs.Denominator() - rhs.Numerator() * lhs.Denominator(),
  58.             lhs.Denominator() * rhs.Denominator()};
  59. };
  60.  
  61. Rational operator*(const Rational &lhs, const Rational &rhs) {
  62.     return {
  63.             lhs.Numerator() * rhs.Numerator(),
  64.             lhs.Denominator() * rhs.Denominator()
  65.     };
  66. }
  67.  
  68. Rational operator/(const Rational &lhs, const Rational &rhs) {
  69.     if (rhs.Numerator() == 0) {
  70.         throw domain_error("Division by zero");
  71.     }
  72.     return lhs * Rational(rhs.Denominator(), rhs.Numerator());
  73. }
  74.  
  75. ostream &operator<<(ostream &stream, const Rational &rational) {
  76.     stream << rational.Numerator() << '/' << rational.Denominator();
  77. }
  78.  
  79. Rational CreateRationalFromString(string str) {
  80.     stringstream stream(str);
  81.     int num;
  82.     int denom;
  83.     stream >> num;
  84.     stream.ignore(1);
  85.     stream >> denom;
  86.     return {num, denom};
  87. }
  88.  
  89. string SliceRationalFromString(string str) {
  90.     int num, denom;
  91.     char op;
  92.     for (const auto &c : str) {
  93.         num = c - '0';
  94.     }
  95. }
  96.  
  97. bool IsOperator(char c) {
  98.     return c == '+' || c == '-' || c == '*' || c == '/';
  99. }
  100.  
  101. bool IsFullExpression(string str) {
  102.     int slash_count = count_if(begin(str), end(str), [](char c) {
  103.         return c == '/';
  104.     });
  105.     return slash_count == 2;
  106. }
  107.  
  108. int FindOperatorIndex(string str) {
  109.     vector<char> operators = {'+', '-', '*', '/'};
  110.     for (const char &op : operators) {
  111.         const int found = str.find(op);
  112.         if (found != string::npos) {
  113.             return found;
  114.         }
  115.     }
  116.  
  117.     return -1;
  118. }
  119.  
  120. int main() {
  121.     try {
  122.         /* Test cases */
  123.         // 1/2+2/3
  124.         // 1/2+ 2/3
  125.         // 1/2 +2/3
  126.         // 1/2 + 2/3
  127.  
  128.         string a, b;
  129.         char operation;
  130.         cin >> a;
  131.         if (IsFullExpression(a)) {
  132.             int operator_index = FindOperatorIndex(a);
  133.             operation = a[operator_index];
  134.             b = a.substr(operator_index + 1, a.size() - 1);
  135.             a = a.substr(0, operator_index);
  136.         } else {
  137.             char last_char = a[a.size() - 1];
  138.             // case 1/2+ 2/3
  139.             if (IsOperator(last_char)) {
  140.                 operation = last_char;
  141.                 a.pop_back();
  142.             } else {
  143.                 cin >> operation;
  144.             }
  145.             cin >> b;
  146.         }
  147.  
  148.         Rational r1 = CreateRationalFromString(a);
  149.         Rational r2 = CreateRationalFromString(b);
  150.         if (operation == '+') {
  151.             cout << (r1 + r2) << endl;
  152.         } else if (operation == '-') {
  153.             cout << (r1 - r2) << endl;
  154.         } else if (operation == '*') {
  155.             cout << (r1 * r2) << endl;
  156.         } else if (operation == '/') {
  157.             cout << (r1 / r2) << endl;
  158.         }
  159.     } catch (exception &ex) {
  160.         cout << ex.what() << endl;
  161.     }
  162.  
  163.     return 0;
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement