Untitled
unknown
plain_text
3 years ago
1.9 kB
11
Indexable
#include <iostream>
using namespace std;
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 = b.second * b.first;
this->second = this->second * this->first;
return(*this);
}
Fraction operator/(Fraction b) {
this->first = this->first * this->second;
this->second = b.first * b.second;
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();
return(0);
}
Editor is loading...