Calculator  Step 6
Public Member Functions | Private Member Functions | Private Attributes | List of all members
number_rational Class Reference

#include <number_impl.hpp>

Inheritance diagram for number_rational:
number_impl

Public Member Functions

 number_rational (rational< long long > value)
 
 number_rational (long long num)
 
 number_rational (long long num, long long den)
 
rational< long long > value () const
 
virtual std::shared_ptr
< number_impl
promote_to_void ()
 
virtual std::shared_ptr
< number_impl
promote_to_double ()
 
- Public Member Functions inherited from number_impl
virtual ~number_impl ()
 
void print (std::ostream &stream) const
 
std::string to_string () const
 
void save (std::ostream &stream) const
 
bool equals (number_impl const &rhs) const
 
bool less (number_impl const &rhs) const
 
std::shared_ptr< number_impladd (number_impl const &rhs)
 
std::shared_ptr< number_implsubtract (number_impl const &rhs)
 
std::shared_ptr< number_implmultiply (number_impl const &rhs)
 
std::shared_ptr< number_impldivide (number_impl const &rhs)
 
void add_ref ()
 
void del_ref ()
 
std::shared_ptr< number_implpromote (number_impl &rhs) const
 
virtual std::shared_ptr
< number_impl
promote_to_long ()
 
virtual std::shared_ptr
< number_impl
promote_to_rational ()
 

Private Member Functions

virtual std::string do_to_string () const
 
virtual void do_save (std::ostream &stream) const
 
virtual bool do_equals (number_impl const &rhs) const
 
virtual bool do_less (number_impl const &rhs) const
 
virtual std::shared_ptr
< number_impl
do_add (number_impl const &rhs)
 
virtual std::shared_ptr
< number_impl
do_subtract (number_impl const &rhs)
 
virtual std::shared_ptr
< number_impl
do_multiply (number_impl const &rhs)
 
virtual std::shared_ptr
< number_impl
do_divide (number_impl const &rhs)
 
virtual std::shared_ptr
< number_impl
do_promote (number_impl &rhs) const
 

Private Attributes

rational< long long > value_
 

Additional Inherited Members

- Static Public Member Functions inherited from number_impl
static std::shared_ptr
< number_impl
read_library (std::istream &stream)
 
- Protected Member Functions inherited from number_impl
 number_impl ()
 

Detailed Description

Represent a rational number. For the greatest range, always use long to represent the numerator and denominator.

Definition at line 122 of file number_impl.hpp.

Constructor & Destructor Documentation

number_rational::number_rational ( rational< long long >  value)
inline

Definition at line 124 of file number_impl.hpp.

124 : number_impl(), value_(value) {}
rational< long long > value_
number_rational::number_rational ( long long  num)
inline

Definition at line 125 of file number_impl.hpp.

125 : number_impl(), value_(num) {}
rational< long long > value_
number_rational::number_rational ( long long  num,
long long  den 
)
inline

Definition at line 126 of file number_impl.hpp.

126 : number_impl(), value_(num, den) {}
rational< long long > value_

Member Function Documentation

std::shared_ptr< number_impl > number_rational::do_add ( number_impl const &  rhs)
privatevirtual

Implements number_impl.

Definition at line 271 of file number_impl.cpp.

References value(), and value_.

272 {
273  rational<long long> result{ value_ + dynamic_cast<number_rational const&>(rhs).value() };
274  if (result.denominator() == 1)
275  return std::make_shared<number_long>(result.numerator());
276  else
277  return std::make_shared<number_rational>(result);
278 }
rational< long long > value() const
rational< long long > value_
std::shared_ptr< number_impl > number_rational::do_divide ( number_impl const &  rhs)
privatevirtual

Implements number_impl.

Definition at line 298 of file number_impl.cpp.

References rational< T >::denominator(), rational< T >::numerator(), value(), and value_.

299 {
300  rational<long long> right{ dynamic_cast<number_rational const&>(rhs).value() };
301  if (right.numerator() == 0)
302  throw calc_error{"division by zero"};
303  rational<long long> result = value_ / right;
304  if (result.denominator() == 1)
305  return std::make_shared<number_long>(result.numerator());
306  else
307  return std::make_shared<number_rational>(result);
308 }
value_type denominator() const
Return the denominator.
Definition: rational.hpp:52
value_type numerator() const
Return the numerator.
Definition: rational.hpp:50
rational< long long > value() const
rational< long long > value_
bool number_rational::do_equals ( number_impl const &  rhs) const
privatevirtual

Implements number_impl.

Definition at line 259 of file number_impl.cpp.

References value().

261 {
262  return value() == dynamic_cast<number_rational const&>(rhs).value();
263 }
rational< long long > value() const
bool number_rational::do_less ( number_impl const &  rhs) const
privatevirtual

Implements number_impl.

Definition at line 265 of file number_impl.cpp.

References value().

267 {
268  return value() < dynamic_cast<number_rational const&>(rhs).value();
269 }
rational< long long > value() const
std::shared_ptr< number_impl > number_rational::do_multiply ( number_impl const &  rhs)
privatevirtual

Implements number_impl.

Definition at line 289 of file number_impl.cpp.

References value(), and value_.

290 {
291  rational<long long> result{ value_ * dynamic_cast<number_rational const&>(rhs).value() };
292  if (result.denominator() == 1)
293  return std::make_shared<number_long>(result.numerator());
294  else
295  return std::make_shared<number_rational>(result);
296 }
rational< long long > value() const
rational< long long > value_
std::shared_ptr< number_impl > number_rational::do_promote ( number_impl rhs) const
privatevirtual

Implements number_impl.

Definition at line 310 of file number_impl.cpp.

References number_impl::promote_to_rational().

312 {
313  return rhs.promote_to_rational();
314 }
virtual std::shared_ptr< number_impl > promote_to_rational()
void number_rational::do_save ( std::ostream &  stream) const
privatevirtual

Implements number_impl.

Definition at line 253 of file number_impl.cpp.

References value().

255 {
256  stream << "rational " << value() << ' ';
257 }
rational< long long > value() const
std::shared_ptr< number_impl > number_rational::do_subtract ( number_impl const &  rhs)
privatevirtual

Implements number_impl.

Definition at line 280 of file number_impl.cpp.

References value(), and value_.

281 {
282  rational<long long> result{ value_ - dynamic_cast<number_rational const&>(rhs).value() };
283  if (result.denominator() == 1)
284  return std::make_shared<number_long>(result.numerator());
285  else
286  return std::make_shared<number_rational>(result);
287 }
rational< long long > value() const
rational< long long > value_
std::string number_rational::do_to_string ( ) const
privatevirtual

Implements number_impl.

Definition at line 245 of file number_impl.cpp.

References value().

247 {
248  std::ostringstream stream{};
249  stream << value();
250  return stream.str();
251 }
rational< long long > value() const
std::shared_ptr< number_impl > number_rational::promote_to_double ( )
virtual

Reimplemented from number_impl.

Definition at line 321 of file number_impl.cpp.

References value().

322 {
323  return std::make_shared<number_double>(static_cast<double>(value()));
324 }
rational< long long > value() const
std::shared_ptr< number_impl > number_rational::promote_to_void ( )
virtual

Reimplemented from number_impl.

Definition at line 316 of file number_impl.cpp.

317 {
318  return std::make_shared<number_void>();
319 }
rational<long long> number_rational::value ( ) const
inline

Definition at line 127 of file number_impl.hpp.

References value_.

Referenced by do_add(), do_divide(), do_equals(), do_less(), do_multiply(), do_save(), do_subtract(), do_to_string(), and promote_to_double().

127 { return value_; }
rational< long long > value_

Member Data Documentation

rational<long long> number_rational::value_
private

Definition at line 141 of file number_impl.hpp.

Referenced by do_add(), do_divide(), do_multiply(), do_subtract(), and value().


The documentation for this class was generated from the following files: