Piethon  1.0
A Python-like interpreter using flex and bison.
Public Member Functions | Friends | List of all members
Number Class Reference

#include <number.h>

Collaboration diagram for Number:
Collaboration graph

Public Member Functions

 Number ()
 
 Number (const int val)
 
 Number (const double val)
 
Numberoperator= (const Number &)
 
Numberoperator+= (const Number &)
 
Numberoperator-= (const Number &)
 
Numberoperator*= (const Number &)
 
Numberoperator/= (const Number &)
 
Numberoperator%= (const Number &)
 
Numberoperator^= (const Number &)
 
Number operator+ (const Number &) const
 
Number operator- (const Number &) const
 
Number operator* (const Number &) const
 
Number operator/ (const Number &) const
 
Number operator% (const Number &) const
 
Number operator^ (const Number &) const
 
Number operator- () const
 

Friends

ostream & operator<< (ostream &, const Number &)
 
bool operator== (const Number &, const Number &)
 
bool operator!= (const Number &, const Number &)
 
bool operator< (const Number &, const Number &)
 
bool operator> (const Number &, const Number &)
 
bool operator<= (const Number &, const Number &)
 
bool operator>= (const Number &, const Number &)
 

Detailed Description

Definition at line 21 of file number.h.

Constructor & Destructor Documentation

◆ Number() [1/3]

Number::Number ( )
inline

Definition at line 23 of file number.h.

23 : type(INT), value(0) { }
Definition: number.h:9

◆ Number() [2/3]

Number::Number ( const int  val)
inline

Definition at line 24 of file number.h.

24 : type(INT), value(val) { }
Definition: number.h:9

◆ Number() [3/3]

Number::Number ( const double  val)
inline

Definition at line 25 of file number.h.

25 : type(FLOAT), value(val) { }
Definition: number.h:10

Member Function Documentation

◆ operator%()

Number Number::operator% ( const Number rhs) const

Definition at line 169 of file number.cpp.

169  {
170  if (type == INT && rhs.type == INT) {
171  if (rhs.value.i == 0) {
172  throw ModByZero();
173  }
174  return Number(value.i % rhs.value.i);
175  } else {
176  throw FloatingPointMod();
177  }
178 }
Definition: number.h:9
int i
Definition: number.h:17
Number()
Definition: number.h:23

◆ operator%=()

Number & Number::operator%= ( const Number rhs)

Definition at line 100 of file number.cpp.

100  {
101  *this = *this % rhs;
102  return *this;
103 }

◆ operator*()

Number Number::operator* ( const Number rhs) const

Definition at line 138 of file number.cpp.

138  {
139  if (type == INT && rhs.type == INT) {
140  return Number(value.i * rhs.value.i);
141  } else if (type == FLOAT && rhs.type == FLOAT) {
142  return Number(value.f * rhs.value.f);
143  } else if (type == INT && rhs.type == FLOAT) {
144  return Number(value.i * rhs.value.f);
145  } else if (type == FLOAT && rhs.type == INT) {
146  return Number(value.f * rhs.value.i);
147  } else {
148  throw UnknownType();
149  }
150 }
Definition: number.h:10
double f
Definition: number.h:18
Definition: number.h:9
int i
Definition: number.h:17
Number()
Definition: number.h:23

◆ operator*=()

Number & Number::operator*= ( const Number rhs)

Definition at line 90 of file number.cpp.

90  {
91  *this = *this * rhs;
92  return *this;
93 }

◆ operator+()

Number Number::operator+ ( const Number rhs) const

Definition at line 110 of file number.cpp.

110  {
111  if (type == INT && rhs.type == INT) {
112  return Number(value.i + rhs.value.i);
113  } else if (type == FLOAT && rhs.type == FLOAT) {
114  return Number(value.f + rhs.value.f);
115  } else if (type == INT && rhs.type == FLOAT) {
116  return Number(value.i + rhs.value.f);
117  } else if (type == FLOAT && rhs.type == INT) {
118  return Number(value.f + rhs.value.i);
119  } else {
120  throw UnknownType();
121  }
122 }
Definition: number.h:10
double f
Definition: number.h:18
Definition: number.h:9
int i
Definition: number.h:17
Number()
Definition: number.h:23

◆ operator+=()

Number & Number::operator+= ( const Number rhs)

Definition at line 80 of file number.cpp.

80  {
81  *this = *this + rhs;
82  return *this;
83 }

◆ operator-() [1/2]

Number Number::operator- ( const Number rhs) const

Definition at line 124 of file number.cpp.

124  {
125  if (type == INT && rhs.type == INT) {
126  return Number(value.i - rhs.value.i);
127  } else if (type == FLOAT && rhs.type == FLOAT) {
128  return Number(value.f - rhs.value.f);
129  } else if (type == INT && rhs.type == FLOAT) {
130  return Number(value.i - rhs.value.f);
131  } else if (type == FLOAT && rhs.type == INT) {
132  return Number(value.f - rhs.value.i);
133  } else {
134  throw UnknownType();
135  }
136 }
Definition: number.h:10
double f
Definition: number.h:18
Definition: number.h:9
int i
Definition: number.h:17
Number()
Definition: number.h:23

◆ operator-() [2/2]

Number Number::operator- ( ) const

Definition at line 194 of file number.cpp.

194  {
195  if (type == INT) {
196  return Number(-value.i);
197  } else if (type == FLOAT) {
198  return Number(-value.f);
199  } else {
200  throw UnknownType();
201  }
202 }
Definition: number.h:10
double f
Definition: number.h:18
Definition: number.h:9
int i
Definition: number.h:17
Number()
Definition: number.h:23

◆ operator-=()

Number & Number::operator-= ( const Number rhs)

Definition at line 85 of file number.cpp.

85  {
86  *this = *this - rhs;
87  return *this;
88 }

◆ operator/()

Number Number::operator/ ( const Number rhs) const

Definition at line 152 of file number.cpp.

152  {
153  if (type == INT && rhs.type == INT) {
154  if (rhs.value.i == 0) {
155  throw DivideByZero();
156  }
157  return Number(value.i / rhs.value.i);
158  } else if (type == FLOAT && rhs.type == FLOAT) {
159  return Number(value.f / rhs.value.f);
160  } else if (type == INT && rhs.type == FLOAT) {
161  return Number(value.i / rhs.value.f);
162  } else if (type == FLOAT && rhs.type == INT) {
163  return Number(value.f / rhs.value.i);
164  } else {
165  throw UnknownType();
166  }
167 }
Definition: number.h:10
double f
Definition: number.h:18
Definition: number.h:9
int i
Definition: number.h:17
Number()
Definition: number.h:23

◆ operator/=()

Number & Number::operator/= ( const Number rhs)

Definition at line 95 of file number.cpp.

95  {
96  *this = *this / rhs;
97  return *this;
98 }

◆ operator=()

Number & Number::operator= ( const Number rhs)

Definition at line 74 of file number.cpp.

74  {
75  type = rhs.type;
76  value = rhs.value;
77  return *this;
78 }

◆ operator^()

Number Number::operator^ ( const Number rhs) const

Definition at line 180 of file number.cpp.

180  {
181  if (type == INT && rhs.type == INT) {
182  return Number((int) pow(value.i, rhs.value.i));
183  } else if (type == FLOAT && rhs.type == FLOAT) {
184  return Number(pow(value.f, rhs.value.f));
185  } else if (type == INT && rhs.type == FLOAT) {
186  return Number(pow(value.i, rhs.value.f));
187  } else if (type == FLOAT && rhs.type == INT) {
188  return Number(pow(value.f, rhs.value.i));
189  } else {
190  throw UnknownType();
191  }
192 }
Definition: number.h:10
double f
Definition: number.h:18
Definition: number.h:9
int i
Definition: number.h:17
Number()
Definition: number.h:23

◆ operator^=()

Number & Number::operator^= ( const Number rhs)

Definition at line 105 of file number.cpp.

105  {
106  *this = *this ^ rhs;
107  return *this;
108 }

Friends And Related Function Documentation

◆ operator!=

bool operator!= ( const Number lhs,
const Number rhs 
)
friend

Definition at line 34 of file number.cpp.

34  {
35  return !(lhs == rhs);
36 }

◆ operator<

bool operator< ( const Number lhs,
const Number rhs 
)
friend

Definition at line 38 of file number.cpp.

38  {
39  if (lhs.type == INT && rhs.type == INT) {
40  return lhs.value.i < rhs.value.i;
41  } else if (lhs.type == FLOAT && rhs.type == FLOAT) {
42  return lhs.value.f < rhs.value.f;
43  } else if (lhs.type == INT && rhs.type == FLOAT) {
44  return lhs.value.i < rhs.value.f;
45  } else if (lhs.type == FLOAT && rhs.type == INT) {
46  return lhs.value.f < rhs.value.i;
47  } else {
48  throw UnknownType();
49  }
50 }
Definition: number.h:10
double f
Definition: number.h:18
Definition: number.h:9
int i
Definition: number.h:17

◆ operator<<

ostream& operator<< ( ostream &  out,
const Number num 
)
friend

Definition at line 10 of file number.cpp.

10  {
11  if (num.type == INT) {
12  return out << num.value.i;
13  } else if (num.type == FLOAT) {
14  return out << fixed << num.value.f;
15  } else {
16  throw UnknownType();
17  }
18 }
Definition: number.h:10
double f
Definition: number.h:18
Definition: number.h:9
int i
Definition: number.h:17

◆ operator<=

bool operator<= ( const Number lhs,
const Number rhs 
)
friend

Definition at line 66 of file number.cpp.

66  {
67  return !(lhs > rhs);
68 }

◆ operator==

bool operator== ( const Number lhs,
const Number rhs 
)
friend

Definition at line 20 of file number.cpp.

20  {
21  if (lhs.type == INT && rhs.type == INT) {
22  return lhs.value.i == rhs.value.i;
23  } else if (lhs.type == FLOAT && rhs.type == FLOAT) {
24  return lhs.value.f == rhs.value.f;
25  } else if (lhs.type == INT && rhs.type == FLOAT) {
26  return lhs.value.i == rhs.value.f;
27  } else if (lhs.type == FLOAT && rhs.type == INT) {
28  return lhs.value.f == rhs.value.i;
29  } else {
30  throw UnknownType();
31  }
32 }
Definition: number.h:10
double f
Definition: number.h:18
Definition: number.h:9
int i
Definition: number.h:17

◆ operator>

bool operator> ( const Number lhs,
const Number rhs 
)
friend

Definition at line 52 of file number.cpp.

52  {
53  if (lhs.type == INT && rhs.type == INT) {
54  return lhs.value.i > rhs.value.i;
55  } else if (lhs.type == FLOAT && rhs.type == FLOAT) {
56  return lhs.value.f > rhs.value.f;
57  } else if (lhs.type == INT && rhs.type == FLOAT) {
58  return lhs.value.i > rhs.value.f;
59  } else if (lhs.type == FLOAT && rhs.type == INT) {
60  return lhs.value.f > rhs.value.i;
61  } else {
62  throw UnknownType();
63  }
64 }
Definition: number.h:10
double f
Definition: number.h:18
Definition: number.h:9
int i
Definition: number.h:17

◆ operator>=

bool operator>= ( const Number lhs,
const Number rhs 
)
friend

Definition at line 70 of file number.cpp.

70  {
71  return !(lhs < rhs);
72 }

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