/
AirLexa
/
origin
Обзор
Документация
Войти
/
AirLexa
/
origin
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
main
Lesson_09/Task_2/Task_2.cpp
257 строк
7 KB
AirLexa
Исправление Lesson_09/Task_2/Task_2.cpp
24 ноя 2025, 18:43
24 ноя 2025, 18:43
fcf710a
Код
Авторство
О чём код?
#include <iostream> #include <string> #include <windows.h> class Fraction { private: int num; int den; Fraction trim() // сокращение дроби { int a = num < 0 ? -num : num; // обеспечение положительных int b = den < 0 ? -den : den; // значений для расчета НОД while (b != 0) // метод Евклида для НОД { int tmp = b; b = a % b; a = tmp; } num /= a; den /= a; return *this; } public: Fraction(int numerator = 0, int denominator = 1) // 0 - по умолчанию { set_fraction(numerator, denominator); } void set_fraction(int n, int d) // сеттеры { num = d == 0 ? d : n; // если знаменатель = 0 den = d == 0 ? 1 : d; // то дробь = 0 trim(); } std::string to_str() const { return den == 1 ? std::to_string(num) : std::to_string(num).append("/").append(std::to_string(den)) ; } Fraction operator + (Fraction other) { return Fraction(num * other.den + other.num * den, den * other.den).trim(); } Fraction operator - (Fraction other) // вычитание это сложение с отрицательным знаком { return *this + Fraction(-other.num, other.den); } Fraction operator * (Fraction other) { return Fraction(num * other.num, den * other.den).trim(); } Fraction operator / (Fraction other) // деление это умножение на перевернутую дробь { return *this * Fraction(other.den, other.num); } Fraction& operator ++ () // префиксная { num += den; trim(); return *this; } Fraction operator ++ (int) // постфиксная { Fraction copy = { *this }; ++(*this); return copy.trim(); } Fraction& operator -- () // префиксная { num -= den; trim(); return *this; } Fraction operator -- (int) // постфиксная { Fraction copy = { *this }; --(*this); return copy.trim(); } }; std::ostream& operator << (std::ostream& os, const Fraction& f) { return os << f.to_str(); } std::istream& operator >> (std::istream& in, Fraction& f) { int x = 0, y = 0; in >> x >> y; f.set_fraction(x, y); return in; } int main() { SetConsoleCP(1251); SetConsoleOutputCP(1251); Fraction f1, f2; std::cout << "Дробь 1. Введите числитель знаменатель через пробел: "; std::cin >> f1; std::cout << "Дробь 2. Введите числитель знаменатель через пробел: "; std::cin >> f2; std::cout << f1 << " + " << f2 << " = " << (f1 + f2) << std::endl; std::cout << f1 << " - " << f2 << " = " << (f1 - f2) << std::endl; std::cout << f1 << " * " << f2 << " = " << (f1 * f2) << std::endl; std::cout << f1 << " / " << f2 << " = " << (f1 / f2) << std::endl; std::string s = "++" + f1.to_str(); // значение 1 дроби перед операциями std::cout << s << " * " << f2 << " = " << (++f1 * f2) << std::endl; std::cout << "Значение дроби 1 = " << f1 << std::endl; s = f1.to_str().append("--"); // значение 1 дроби перед операциями std::cout << s << " * " << f2 << " = " << (f1-- * f2) << std::endl; std::cout << "Значение дроби 1 = " << f1 << std::endl; return 0; }#include <iostream> #include <string> #include <windows.h> class Fraction { private: int num; int den; Fraction trim() // сокращение дроби { int a = num < 0 ? -num : num; // обеспечение положительных int b = den < 0 ? -den : den; // значений для расчета НОД while (b != 0) // метод Евклида для НОД { int tmp = b; b = a % b; a = tmp; } num /= a; den /= a; return *this; } public: Fraction(int numerator = 0, int denominator = 1) // 0 - по умолчанию { set_fraction(numerator, denominator); } void set_fraction(int n, int d) // сеттер { num = d == 0 ? d : n; // если знаменатель = 0 den = d == 0 ? 1 : d; // то дробь = 0 trim(); } std::string to_str() const { return den == 1 ? std::to_string(num) : std::to_string(num).append("/").append(std::to_string(den)) ; } Fraction operator + (Fraction other) { return Fraction(num * other.den + other.num * den, den * other.den).trim(); } Fraction operator - (Fraction other) // вычитание это сложение с отрицательным знаком { return *this + Fraction(-other.num, other.den); } Fraction operator * (Fraction other) { return Fraction(num * other.num, den * other.den).trim(); } Fraction operator / (Fraction other) // деление это умножение на перевернутую дробь { return *this * Fraction(other.den, other.num); } Fraction& operator ++ () // префиксная { num += den; trim(); return *this; } Fraction operator ++ (int) // постфиксная { Fraction copy = { *this }; ++(*this); return copy.trim(); } Fraction& operator -- () // префиксная { num -= den; trim(); return *this; } Fraction operator -- (int) // постфиксная { Fraction copy = { *this }; --(*this); return copy.trim(); } }; std::ostream& operator << (std::ostream& os, const Fraction& f) { return os << f.to_str(); } std::istream& operator >> (std::istream& in, Fraction& f) { int x = 0, y = 0; in >> x >> y; f.set_fraction(x, y); return in; } int main() { SetConsoleCP(1251); SetConsoleOutputCP(1251); Fraction f1, f2; std::cout << "Дробь 1. Введите числитель знаменатель через пробел: "; std::cin >> f1; std::cout << "Дробь 2. Введите числитель знаменатель через пробел: "; std::cin >> f2; std::cout << f1 << " + " << f2 << " = " << (f1 + f2) << std::endl; std::cout << f1 << " - " << f2 << " = " << (f1 - f2) << std::endl; std::cout << f1 << " * " << f2 << " = " << (f1 * f2) << std::endl; std::cout << f1 << " / " << f2 << " = " << (f1 / f2) << std::endl; std::string s = "++" + f1.to_str(); // значение 1 дроби перед операциями std::cout << s << " * " << f2 << " = " << (++f1 * f2) << std::endl; std::cout << "Значение дроби 1 = " << f1 << std::endl; s = f1.to_str().append("--"); // значение 1 дроби перед операциями std::cout << s << " * " << f2 << " = " << (f1-- * f2) << std::endl; std::cout << "Значение дроби 1 = " << f1 << std::endl; return 0; }