exercise 3
This commit is contained in:
parent
6a05ccbf97
commit
aeddf4cc21
|
@ -47,7 +47,7 @@ class Minivan : public PassengerCar, public Bus
|
|||
{
|
||||
public:
|
||||
Minivan(std::string company, std::string model) :
|
||||
PassengerCar(company, model), Bus(company, model), Car(company, model)
|
||||
Car(company, model), PassengerCar(company, model), Bus(company, model)
|
||||
{
|
||||
std::cout << "Минивэн компании " + getCompany() + ", модель " + getModel() << std::endl;
|
||||
}
|
||||
|
|
|
@ -1,8 +1,225 @@
|
|||
#ifndef EXERCISE_3_HPP_
|
||||
#define EXERCISE_3_HPP_
|
||||
|
||||
#include <iostream>
|
||||
|
||||
class Fraction
|
||||
{
|
||||
private:
|
||||
bool is_zero;
|
||||
bool is_integer;
|
||||
int numerator;
|
||||
int denominator;
|
||||
|
||||
/*
|
||||
* Поиск наибольшего общего делителя
|
||||
* для числителя и знаменателя
|
||||
*/
|
||||
int NOD(int n1, int n2)
|
||||
{
|
||||
int div;
|
||||
if (n1 == n2) return n1;
|
||||
int d = n1 - n2;
|
||||
if (d < 0)
|
||||
{
|
||||
d = -d;
|
||||
div = NOD(n1, d);
|
||||
}
|
||||
else
|
||||
div = NOD(n2, d);
|
||||
|
||||
return div;
|
||||
}
|
||||
|
||||
/*
|
||||
* Поиск наименьшего общего кратного
|
||||
* для знаменателей
|
||||
*/
|
||||
int NOK(int n1, int n2)
|
||||
{
|
||||
return n1 * n2 / NOD(n1, n2);
|
||||
}
|
||||
|
||||
/*
|
||||
* Функция сокращения дроби
|
||||
*/
|
||||
void reduceFraction(int& a, int& b)
|
||||
{
|
||||
int divisor = NOD(abs(a), abs(b));
|
||||
a = a / divisor;
|
||||
b = b / divisor;
|
||||
}
|
||||
public:
|
||||
Fraction () : numerator(0), denominator(0)
|
||||
{
|
||||
is_zero = true;
|
||||
is_integer = true;
|
||||
}
|
||||
|
||||
Fraction(int num, int dnum) : numerator(num), denominator(dnum)
|
||||
{
|
||||
if (numerator == 0 || denominator == 0)
|
||||
{
|
||||
std::cout << "Числитель или знаменатель не может быть нулевым!\n"
|
||||
<< "Число будет инициализировано нулём!" << std::endl;
|
||||
numerator = 0;
|
||||
denominator = 0;
|
||||
is_zero = true;
|
||||
is_integer = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
is_zero = false;
|
||||
is_integer = (abs(numerator) == abs(denominator) || denominator == 1);
|
||||
}
|
||||
}
|
||||
|
||||
friend std::ostream& operator<<(std::ostream&, const Fraction&);
|
||||
|
||||
Fraction operator+(const Fraction&);
|
||||
Fraction operator-(const Fraction&);
|
||||
Fraction operator*(const Fraction&);
|
||||
Fraction operator/(const Fraction&);
|
||||
|
||||
Fraction operator-() const;
|
||||
|
||||
bool operator==(const Fraction&);
|
||||
bool operator!=(const Fraction&);
|
||||
bool operator<(const Fraction&);
|
||||
bool operator>=(const Fraction&);
|
||||
bool operator>(const Fraction&);
|
||||
bool operator<=(const Fraction&);
|
||||
};
|
||||
|
||||
std::ostream& operator<<(std::ostream& s, const Fraction& f)
|
||||
{
|
||||
if (f.is_integer)
|
||||
{
|
||||
if (f.is_zero)
|
||||
s << f.numerator;
|
||||
else
|
||||
s << f.numerator / f.denominator;
|
||||
}
|
||||
else
|
||||
s << f.numerator << '/' << f.denominator;
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
Fraction Fraction::operator+(const Fraction& f)
|
||||
{
|
||||
if (f.is_zero)
|
||||
return Fraction(numerator, denominator);
|
||||
|
||||
if (is_zero)
|
||||
return Fraction(f.numerator, f.denominator);
|
||||
|
||||
int new_denominator = NOK(denominator, f.denominator);
|
||||
int new_numerator = numerator * (new_denominator / denominator) + f.numerator * (new_denominator / f.denominator);
|
||||
|
||||
reduceFraction(new_numerator, new_denominator);
|
||||
|
||||
return Fraction(new_numerator, new_denominator);
|
||||
}
|
||||
|
||||
Fraction Fraction::operator-(const Fraction& f)
|
||||
{
|
||||
if (f.is_zero)
|
||||
return Fraction(numerator, denominator);
|
||||
|
||||
if (is_zero)
|
||||
return Fraction(-f.numerator, f.denominator);
|
||||
|
||||
int new_denominator = NOK(denominator, f.denominator);
|
||||
int new_numerator = numerator * (new_denominator / denominator) - f.numerator * (new_denominator / f.denominator);
|
||||
|
||||
if (new_numerator == 0)
|
||||
return Fraction();
|
||||
|
||||
reduceFraction(new_numerator, new_denominator);
|
||||
|
||||
return Fraction(new_numerator, new_denominator);
|
||||
}
|
||||
|
||||
Fraction Fraction::operator*(const Fraction& f)
|
||||
{
|
||||
if (is_zero || f.is_zero)
|
||||
return Fraction();
|
||||
|
||||
int new_numerator = numerator * f.numerator;
|
||||
int new_denominator = denominator * f.denominator;
|
||||
|
||||
reduceFraction(new_numerator, new_denominator);
|
||||
|
||||
return Fraction(new_numerator, new_denominator);
|
||||
}
|
||||
|
||||
Fraction Fraction::operator/(const Fraction& f)
|
||||
{
|
||||
if (f.is_zero)
|
||||
{
|
||||
std::cout << " [На 0 делить нельзя!] ";
|
||||
return Fraction();
|
||||
}
|
||||
|
||||
if (is_zero)
|
||||
return Fraction();
|
||||
|
||||
return *this * Fraction(f.denominator, f.numerator);
|
||||
}
|
||||
|
||||
Fraction Fraction::operator-() const
|
||||
{
|
||||
return Fraction(-numerator, denominator);
|
||||
}
|
||||
|
||||
bool Fraction::operator==(const Fraction& f)
|
||||
{
|
||||
if (is_zero || f.is_zero)
|
||||
return numerator == f.numerator;
|
||||
|
||||
int a_n = numerator;
|
||||
int a_d = denominator;
|
||||
|
||||
int b_n = f.numerator;
|
||||
int b_d = f.denominator;
|
||||
|
||||
reduceFraction(a_n, a_d);
|
||||
reduceFraction(b_n, b_d);
|
||||
|
||||
return a_n == b_n && a_d == b_d;
|
||||
}
|
||||
|
||||
bool Fraction::operator!=(const Fraction& f)
|
||||
{
|
||||
return !(*this == f);
|
||||
}
|
||||
|
||||
bool Fraction::operator<(const Fraction& f)
|
||||
{
|
||||
if (is_zero || f.is_zero)
|
||||
return numerator < f.numerator;
|
||||
|
||||
int new_denominator = NOK(denominator, f.denominator);
|
||||
int a_n = numerator * (new_denominator / denominator);
|
||||
int b_n = f.numerator * (new_denominator / f.denominator);
|
||||
|
||||
return a_n < b_n;
|
||||
}
|
||||
|
||||
bool Fraction::operator>=(const Fraction& f)
|
||||
{
|
||||
return (*this == f || !(*this < f));
|
||||
}
|
||||
|
||||
bool Fraction::operator>(const Fraction& f)
|
||||
{
|
||||
return (*this != f && !(*this < f));
|
||||
}
|
||||
|
||||
bool Fraction::operator<=(const Fraction& f)
|
||||
{
|
||||
return (*this == f || *this < f);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
|
|
@ -12,15 +12,13 @@ void ex_1();
|
|||
void ex_2();
|
||||
void ex_3();
|
||||
void ex_4();
|
||||
void ex_5();
|
||||
|
||||
int main()
|
||||
{
|
||||
// ex_1();
|
||||
ex_2();
|
||||
// ex_2();
|
||||
ex_3();
|
||||
ex_4();
|
||||
ex_5();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@ -56,19 +54,83 @@ void ex_1()
|
|||
void ex_2()
|
||||
{
|
||||
Minivan m("Макдоналдс", "ABC");
|
||||
|
||||
/*
|
||||
* Транспортное средство компании Макдоналдс, модель ABC
|
||||
* Легковой автомобиль компании Макдоналдс, модель ABC
|
||||
* Автобус компании Макдоналдс, модель ABC
|
||||
* Минивэн компании Макдоналдс, модель ABC
|
||||
*/
|
||||
}
|
||||
|
||||
void ex_3()
|
||||
{
|
||||
Fraction a(3, 9);
|
||||
Fraction b(2, 3);
|
||||
Fraction c; // 0
|
||||
|
||||
cout << "a = " << a << endl;
|
||||
cout << "b = " << b << endl;
|
||||
cout << "c = " << c << endl;
|
||||
cout << "[a + b] " << a << " + " << b << " = " << a + b << endl;
|
||||
cout << "[a - b] " << a << " - " << b << " = " << a - b << endl;
|
||||
cout << "[a * b] " << a << " * " << b << " = " << a * b << endl;
|
||||
cout << "[a / b] " << a << " / " << b << " = " << a / b << endl;
|
||||
cout << "[c + b] " << c << " + " << b << " = " << c + b << endl;
|
||||
cout << "[b + c] " << b << " + " << c << " = " << b + c << endl;
|
||||
cout << "[c - b] " << c << " - " << b << " = " << c - b << endl;
|
||||
cout << "[b - c] " << b << " - " << c << " = " << b - c << endl;
|
||||
cout << "[c * b] " << c << " * " << b << " = " << c * b << endl;
|
||||
cout << "[b * c] " << b << " * " << c << " = " << b * c << endl;
|
||||
cout << "[c / b] " << c << " / " << b << " = " << c / b << endl;
|
||||
cout << "[b / c] " << b << " / " << c << " = " << b / c << endl;
|
||||
cout << "-a = " << -a << endl;
|
||||
cout << "[a == b] " << a << " == " << b << " is " << ((a == b) ? "true" : "false") << endl;
|
||||
cout << "[a != b] " << a << " != " << b << " is " << ((a != b) ? "true" : "false") << endl;
|
||||
cout << "[a < b] " << a << " < " << b << " is " << ((a < b) ? "true" : "false") << endl;
|
||||
cout << "[a <= b] " << a << " <= " << b << " is " << ((a <= b) ? "true" : "false") << endl;
|
||||
cout << "[a > b] " << a << " > " << b << " is " << ((a > b) ? "true" : "false") << endl;
|
||||
cout << "[a >= b] " << a << " >= " << b << " is " << ((a >= b) ? "true" : "false") << endl;
|
||||
cout << "[c == a] " << c << " == " << a << " is " << ((c == a) ? "true" : "false") << endl;
|
||||
cout << "[c != a] " << c << " != " << a << " is " << ((c != a) ? "true" : "false") << endl;
|
||||
cout << "[c < a] " << c << " < " << a << " is " << ((c < a) ? "true" : "false") << endl;
|
||||
cout << "[c <= a] " << c << " <= " << a << " is " << ((c <= a) ? "true" : "false") << endl;
|
||||
cout << "[c > a] " << c << " > " << a << " is " << ((c > a) ? "true" : "false") << endl;
|
||||
cout << "[c >= a] " << c << " >= " << a << " is " << ((c >= a) ? "true" : "false") << endl;
|
||||
|
||||
/*
|
||||
* a = 3/9
|
||||
* b = 2/3
|
||||
* c = 0
|
||||
* [a + b] 3/9 + 2/3 = 1
|
||||
* [a - b] 3/9 - 2/3 = -1/3
|
||||
* [a * b] 3/9 * 2/3 = 2/9
|
||||
* [a / b] 3/9 / 2/3 = 1/2
|
||||
* [c + b] 0 + 2/3 = 2/3
|
||||
* [b + c] 2/3 + 0 = 2/3
|
||||
* [c - b] 0 - 2/3 = -2/3
|
||||
* [b - c] 2/3 - 0 = 2/3
|
||||
* [c * b] 0 * 2/3 = 0
|
||||
* [b * c] 2/3 * 0 = 0
|
||||
* [c / b] 0 / 2/3 = 0
|
||||
* [b / c] 2/3 / 0 = [На 0 делить нельзя!] 0
|
||||
* -a = -3/9
|
||||
* [a == b] 3/9 == 2/3 is false
|
||||
* [a != b] 3/9 != 2/3 is true
|
||||
* [a < b] 3/9 < 2/3 is true
|
||||
* [a <= b] 3/9 <= 2/3 is true
|
||||
* [a > b] 3/9 > 2/3 is false
|
||||
* [a >= b] 3/9 >= 2/3 is false
|
||||
* [c == a] 0 == 3/9 is false
|
||||
* [c != a] 0 != 3/9 is true
|
||||
* [c < a] 0 < 3/9 is true
|
||||
* [c <= a] 0 <= 3/9 is true
|
||||
* [c > a] 0 > 3/9 is false
|
||||
* [c >= a] 0 >= 3/9 is false
|
||||
*/
|
||||
}
|
||||
|
||||
void ex_4()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void ex_5()
|
||||
{
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue