Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <algorithm>
- #include <iostream>
- #include <string>
- #include <sstream>
- #include <exception>
- #include <vector>
- using namespace std;
- int GCD(int a, int b) {
- if (b == 0) {
- return a;
- } else {
- return GCD(b, a % b);
- }
- }
- class Rational {
- public:
- Rational() {
- numerator = 0;
- denominator = 1;
- }
- Rational(int new_numerator, int new_denominator) {
- if (new_denominator == 0) {
- throw invalid_argument("Invalid argument");
- }
- const int gcd = GCD(new_numerator, new_denominator);
- numerator = new_numerator / gcd;
- denominator = new_denominator / gcd;
- if (denominator < 0) {
- denominator = -denominator;
- numerator = -numerator;
- }
- }
- int Numerator() const {
- return numerator;
- }
- int Denominator() const {
- return denominator;
- }
- private:
- int numerator;
- int denominator;
- };
- Rational operator+(const Rational &lhs, const Rational &rhs) {
- return {lhs.Numerator() * rhs.Denominator() + rhs.Numerator() * lhs.Denominator(),
- lhs.Denominator() * rhs.Denominator()};
- };
- Rational operator-(const Rational &lhs, const Rational &rhs) {
- return {lhs.Numerator() * rhs.Denominator() - rhs.Numerator() * lhs.Denominator(),
- lhs.Denominator() * rhs.Denominator()};
- };
- Rational operator*(const Rational &lhs, const Rational &rhs) {
- return {
- lhs.Numerator() * rhs.Numerator(),
- lhs.Denominator() * rhs.Denominator()
- };
- }
- Rational operator/(const Rational &lhs, const Rational &rhs) {
- if (rhs.Numerator() == 0) {
- throw domain_error("Division by zero");
- }
- return lhs * Rational(rhs.Denominator(), rhs.Numerator());
- }
- ostream &operator<<(ostream &stream, const Rational &rational) {
- stream << rational.Numerator() << '/' << rational.Denominator();
- }
- Rational CreateRationalFromString(string str) {
- stringstream stream(str);
- int num;
- int denom;
- stream >> num;
- stream.ignore(1);
- stream >> denom;
- return {num, denom};
- }
- string SliceRationalFromString(string str) {
- int num, denom;
- char op;
- for (const auto &c : str) {
- num = c - '0';
- }
- }
- bool IsOperator(char c) {
- return c == '+' || c == '-' || c == '*' || c == '/';
- }
- bool IsFullExpression(string str) {
- int slash_count = count_if(begin(str), end(str), [](char c) {
- return c == '/';
- });
- return slash_count == 2;
- }
- int FindOperatorIndex(string str) {
- vector<char> operators = {'+', '-', '*', '/'};
- for (const char &op : operators) {
- const int found = str.find(op);
- if (found != string::npos) {
- return found;
- }
- }
- return -1;
- }
- int main() {
- try {
- /* Test cases */
- // 1/2+2/3
- // 1/2+ 2/3
- // 1/2 +2/3
- // 1/2 + 2/3
- string a, b;
- char operation;
- cin >> a;
- if (IsFullExpression(a)) {
- int operator_index = FindOperatorIndex(a);
- operation = a[operator_index];
- b = a.substr(operator_index + 1, a.size() - 1);
- a = a.substr(0, operator_index);
- } else {
- char last_char = a[a.size() - 1];
- // case 1/2+ 2/3
- if (IsOperator(last_char)) {
- operation = last_char;
- a.pop_back();
- } else {
- cin >> operation;
- }
- cin >> b;
- }
- Rational r1 = CreateRationalFromString(a);
- Rational r2 = CreateRationalFromString(b);
- if (operation == '+') {
- cout << (r1 + r2) << endl;
- } else if (operation == '-') {
- cout << (r1 - r2) << endl;
- } else if (operation == '*') {
- cout << (r1 * r2) << endl;
- } else if (operation == '/') {
- cout << (r1 / r2) << endl;
- }
- } catch (exception &ex) {
- cout << ex.what() << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement