Skip to content

Commit

Permalink
Операторни функции за въвеждане и извеждане
Browse files Browse the repository at this point in the history
  • Loading branch information
triffon committed Mar 30, 2022
1 parent 7661a82 commit d56777e
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 7 deletions.
14 changes: 8 additions & 6 deletions lectures/1/rational/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,16 @@ void testRational() {
Rational q = Rational(5, 6);
q.printnl();
std::cout << q.toDouble() << std::endl;
q.read();
//q.read();
std::cin >> q;

q.printnl();
r.print();
std::cout << " * ";
q.print();
std::cout << " = ";
multiply(r, q).printnl();
// r.print();

std::cout << r << " * " << q << " = " << multiply(r, q) << std::endl;
// q.print();
// std::cout << " = ";
// multiply(r, q).printnl();

r.print();
std::cout << " + ";
Expand Down
13 changes: 12 additions & 1 deletion lectures/1/rational/rational.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,15 @@ bool lessThan(Rational const& p, Rational const& q) {

inline bool Rational::equals(Rational const& p) const {
return numer == p.numer && denom == p.denom;
}
}

std::ostream& operator<<(std::ostream& os, Rational const& r) {
return os << r.numer << '/' << r.denom;
}

std::istream& operator>>(std::istream& is, Rational& r) {
char c;
is >> r.numer >> c >> r.denom;
r.normalize();
return is;
}
9 changes: 9 additions & 0 deletions lectures/1/rational/rational.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#ifndef __RATIONAL_HPP
#define __RATIONAL_HPP

#include <iostream>

class Rational {
private:
// ниво 0: представяне
Expand Down Expand Up @@ -35,6 +37,13 @@ class Rational {

// ниво 1: селектор за сравнение
bool equals(Rational const& p) const;

// ниво 1: въвеждане и извеждане
// !!! std::ostream& operator<<(std::ostream&) const;

friend std::ostream& operator<<(std::ostream&, Rational const&);
// { return ... }
friend std::istream& operator>>(std::istream&, Rational&);
};

// ниво 2: аритметични операции за умножение и събиране
Expand Down

0 comments on commit d56777e

Please sign in to comment.