Добавил:
vvrstcnho
Рад, если кому-то помог
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз:
Предмет:
Файл:Лабораторные работы С++ (для ИВТ) / Готовые лабы С++ / Лаба2 / Laba 2 (2)
.cpp#include <iostream>
#include <windows.h>
using namespace std;
int gcd(int a, int b) {
return b == 0 ? a : gcd(b, a % b);
}
class Fraction {
private:
int numerator;
int denominator;
void reduce() {
int common = gcd(numerator, denominator);
numerator /= common;
denominator /= common;
if (denominator < 0) {
numerator = -numerator;
denominator = -denominator;
}
}
public:
Fraction(int num, int den = 1) : numerator(num), denominator(den) {
reduce();
}
void print() {
cout << "Дробь: " << numerator;
if (denominator != 1) cout << "/" << denominator;
cout << endl;
}
};
int main() {
SetConsoleOutputCP(65001);
Fraction f1(1, 2);
Fraction f2(6, 12);
Fraction f3(5);
f1.print();
f2.print();
f3.print();
cout << "Дроби 1/2 и 6/12 после сокращения стали одинаковыми" << endl;
return 0;
}
Соседние файлы в папке Лаба2
