Untitled
unknown
plain_text
a year ago
2.4 kB
3
Indexable
Never
#include <iostream> #include <stdlib.h> using namespace std; //Определить класс fraction, который содержит такие поля (члены класса): закрытые – числитель и знаменатель, публичные – // конструктор по умолчанию, конструктор, методы ввода данных (принимается числитель и знаменатель отдельно) // и отображение дроби на экран в обычном виде и десятичном // //* //Упрощение дробей (на этапе создания объекта) //Пример. //(25/100 –> 1/4) class Fraction{ private: int first, second; int nod(int a, int b){ while(a && b){ if (a > b){ a = a % b; } else{ b = b % a; } } return(max(a,b)); } public: Fraction():first(1), second(1){ } Fraction(int a, int b):first(a), second(b){ int del; do{ del = nod(first, second); first /= del; second /= del; } while (del != 1); } void set_data(int a, int b){ first = a; second = b; } void get_data_simple(){ cout << this->first << " / " << this->second << endl; } void get_data_dec(){ cout << first / (float)second << endl; } Fraction operator+(Fraction b){ this->first = this->first * b.second + b.first * this->second; this->second = this->second * b.second; return(*this); } Fraction operator-(Fraction b){ this->first = this->first * b.second - b.first * this->second; this->second = this->second * b.second; return(*this); } Fraction operator*(Fraction b){ this->first = this->first * b.first; this->second = this->second * b.second; return(*this); } Fraction operator/(Fraction b){ this->first = this->first * b.second; this->second = this->second * b.first; return(*this); } }; int main() { Fraction drob1(5, 25); Fraction drob2(100, 100); Fraction *pointer; drob1.get_data_simple(); drob1.get_data_dec(); pointer = &drob2; pointer->get_data_simple(); pointer->get_data_dec(); (drob1+drob2).get_data_simple(); }