Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- // Класс для выполнения операций с дробями
- class Fraction {
- private:
- int first, second;
- // Наибольший общий делитель (алгоритм Евклида)
- int gcd(int x, int y) {
- while (x != y) {
- if (x > y) {
- x -= y;
- }
- else {
- y -= x;
- }
- }
- return x;
- }
- public:
- // Ввод с клавиатуры
- void read() {
- do {
- cout << "Числитель: ";
- cin >> first;
- } while (first < 1);
- cout << "\n";
- do {
- cout << "Знаменатель: ";
- cin >> second;
- } while (second < 1);
- cout << "\n";
- }
- // Сокращение дроби на НОД числителя и знаменателя
- void reduce() {
- int divisor = gcd(first, second);
- first /= divisor;
- second /= divisor;
- }
- // Вывод на экран
- void display() {
- cout << "Дробь: " << first << "/" << second;
- }
- };
- int main() {
- // Пример работы с объектами класса
- Fraction frac;
- frac.read();
- frac.reduce();
- frac.display();
- return 0;
- }
Add Comment
Please, Sign In to add comment