Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <sstream>
- #include <string>
- //#include <numeric>
- using namespace std;
- int gcd(int a, int b)
- {
- // Everything divides 0
- if (a == 0)
- return b;
- if (b == 0)
- return a;
- // base case
- if (a == b)
- return a;
- // a is greater
- if (a > b)
- return gcd(a - b, b);
- return gcd(a, b - a);
- }
- int lcm(int a, int b){
- return a * b / gcd(a, b);
- }
- class Fraction {
- public:
- int whole;
- int numerator;
- int denominator;
- Fraction(int w, int n, int d) : whole(w), numerator(n), denominator(d) {
- normalize();
- }
- Fraction(int n, int d) : whole(0), numerator(n), denominator(d) {
- normalize();
- }
- void reduce(){
- numerator += denominator * whole;
- whole = 0;
- int d = gcd(abs(numerator), abs(denominator));
- numerator = numerator / d;
- denominator = denominator / d;
- }
- void normalize() {
- if (denominator < 0) {
- numerator = -numerator;
- denominator = -denominator;
- }
- reduce();
- whole += numerator / denominator;
- numerator = abs(numerator % denominator);
- if (numerator == 0) {
- denominator = 1;
- }
- if (whole == 0 && numerator < 0) {
- whole = -1;
- numerator = abs(numerator);
- }
- }
- string toString() const {
- stringstream ss;
- if (whole != 0 || numerator == 0) {
- ss << whole;
- }
- if (numerator != 0) {
- if (whole != 0) {
- ss << " ";
- }
- ss << numerator << "/" << denominator;
- }
- return ss.str();
- }
- static Fraction fromString(const string& str) {
- istringstream iss(str);
- int w = 0, n = 0, d = 1;
- if (str.find(' ') != string::npos) {
- iss >> w;
- char slash;
- iss >> n >> slash;
- iss >> d;
- } else if (str.find('/') != string::npos) {
- iss >> n;
- char slash;
- iss >> slash >> d;
- } else {
- iss >> w;
- }
- return Fraction(w, n, d);
- }
- Fraction operator+(const Fraction& other) const {
- int lcm_denom = lcm(denominator, other.denominator);
- cout << "lcm_denom: " << lcm_denom << endl;
- int sum_num = (whole * denominator + numerator) * (lcm_denom / denominator)
- + (other.whole * other.denominator + other.numerator) * (lcm_denom / other.denominator);
- cout << "sum_num: " << sum_num << " numerator: " << whole * denominator + numerator << endl;
- // cout << whole << " " << numerator << " " << denominator << endl;
- return Fraction(0, sum_num, lcm_denom);
- }
- // 10 1/3 + 2/3
- Fraction operator-(const Fraction& other) const {
- int lcm_denom = lcm(denominator, other.denominator);
- cout << "lcm_denom: " << lcm_denom << endl;
- int sum_num = (whole * denominator + numerator) * (lcm_denom / denominator)
- - (other.whole * other.denominator + other.numerator) * (lcm_denom / other.denominator);
- cout << "sum_num: " << sum_num << " numerator: " << whole * denominator + numerator << endl;
- // cout << whole << " " << numerator << " " << denominator << endl;
- return Fraction(0, sum_num, lcm_denom);
- }
- Fraction operator*(const Fraction& other) const {
- int new_num = (whole * denominator + numerator) * (other.whole * other.denominator + other.numerator);
- int new_denom = denominator * other.denominator;
- cout << "new_num: " << new_num << endl;
- cout << "new_num1: " << whole * denominator + numerator << endl;
- cout << "new_num2: " << other.whole * other.denominator + other.numerator << endl;
- return Fraction(0, new_num, new_denom);
- }
- // 10/6 = 5/3 = 1 2/3
- Fraction operator/(const Fraction& other) const {
- int new_num = (whole * denominator + numerator) * other.denominator;
- int new_denom = denominator * (other.whole * other.denominator + other.numerator);
- cout << "new_num: " << new_num << endl;
- cout << "new_num1: " << whole * denominator + numerator << endl;
- cout << "new_num2: " << other.whole * other.denominator + other.numerator << endl;
- return Fraction(0, new_num, new_denom);
- }
- };
- int main() {
- setlocale(LC_ALL, "Russian");
- string frac1_str, frac2_str;
- char operation;
- cout << "Введите первую дробь: ";
- getline(cin, frac1_str);
- cout << "Введите знак операции (+, -, *, /): ";
- cin >> operation;
- cin.ignore();
- cout << "Введите вторую дробь: ";
- getline(cin, frac2_str);
- Fraction frac1 = Fraction::fromString(frac1_str);
- Fraction frac2 = Fraction::fromString(frac2_str);
- Fraction result(0, 0, 1);
- switch (operation) {
- case '+':
- result = frac1 + frac2;
- break;
- case '-':
- result = frac1 - frac2;
- break;
- case '*':
- result = frac1 * frac2;
- break;
- case '/':
- result = frac1 / frac2;
- break;
- default:
- cerr << "Неизвестная операция!" << endl;
- return 1;
- }
- cout << "Результат: " << result.toString() << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment