Calculator  Step 6
number.hpp
Go to the documentation of this file.
1 #ifndef NUMBER_HPP_
2 #define NUMBER_HPP_
3 
4 #include <memory>
5 #include <ostream>
6 #include <string>
7 
8 #include "rational.hpp"
9 
11 class number_impl;
12 
16 class number
17 {
18 public:
23  number();
30  number(int x);
35  number(long x);
40  number(long long x);
46  number(double x);
50  number(rational<long long> const& x);
51 
52  number(number const&) = default;
53  number(number&&) = default;
54  number& operator=(number const&) = default;
55  number& operator=(number&&) = default;
56  ~number() = default;
57 
59  number(std::istream& stream);
60 
64  void save(std::ostream& stream) const;
65 
71  bool equals(number b);
72 
78  bool less(number b);
79 
86  void coerce(number& rhs);
87 
90  std::string to_string() const;
91 
95  void print(std::ostream& stream);
96 
102  number operator+(number rhs);
108  number operator-(number rhs);
114  number operator*(number rhs);
121  number operator/(number rhs);
122 
123 private:
127  number(std::shared_ptr<number_impl> ptr);
128 
129  std::shared_ptr<number_impl> pimpl_;
130 };
131 
138 inline bool operator==(number a, number b)
139 {
140  return a.equals(b);
141 }
142 
149 inline bool operator!=(number a, number b)
150 {
151  return not (a == b);
152 }
153 
160 inline bool operator<(number a, number b)
161 {
162  return a.less(b);
163 }
164 
171 inline bool operator<=(number a, number b)
172 {
173  return not(b < a);
174 }
175 
182 inline bool operator>(number a, number b)
183 {
184  return b < a;
185 }
186 
193 inline bool operator>=(number a, number b)
194 {
195  return not (a < b);
196 }
197 
203 {
204  return number{0} - n;
205 }
206 
211 inline std::ostream& operator<<(std::ostream& stream, number n)
212 {
213  n.print(stream);
214  return stream;
215 }
216 
217 #endif
std::shared_ptr< number_impl > pimpl_
Definition: number.hpp:129
number operator-(number rhs)
Definition: number.cpp:82
bool operator!=(number a, number b)
Definition: number.hpp:149
std::string to_string() const
Definition: number.cpp:45
bool operator==(number a, number b)
Definition: number.hpp:138
number operator+(number rhs)
Definition: number.cpp:76
std::ostream & operator<<(std::ostream &stream, number n)
Definition: number.hpp:211
~number()=default
number & operator=(number const &)=default
bool operator<=(number a, number b)
Definition: number.hpp:171
number()
Definition: number.cpp:7
number operator-(number n)
Definition: number.hpp:202
void print(std::ostream &stream)
Definition: number.cpp:51
bool less(number b)
Definition: number.cpp:70
number operator*(number rhs)
Definition: number.cpp:88
void save(std::ostream &stream) const
Definition: number.cpp:39
void coerce(number &rhs)
Definition: number.cpp:56
bool equals(number b)
Definition: number.cpp:64
bool operator>(number a, number b)
Definition: number.hpp:182
number operator/(number rhs)
Definition: number.cpp:94
bool operator>=(number a, number b)
Definition: number.hpp:193
bool operator<(number a, number b)
Definition: number.hpp:160