Advertisement
vguk

Untitled

May 19th, 2020
13
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.49 KB | None | 0 0
  1. #include "variable.h"
  2.  
  3. variable::variable()
  4. {
  5. type = var_types::none;
  6. }
  7.  
  8. variable::variable(int value)
  9. {
  10. type = var_types::int_type;
  11. this->value.int_value = value;
  12. }
  13.  
  14. variable::variable(float value)
  15. {
  16. type = var_types::float_type;
  17. this->value.float_value = value;
  18. }
  19.  
  20. variable::variable(const variable& other)
  21. :type{ other.type }, value{other.value}
  22. {
  23. }
  24.  
  25. variable& variable::operator=(const variable& other)
  26. {
  27. switch (type)
  28. {
  29. case var_types::int_type:
  30. switch (other.type)
  31. {
  32. case var_types::int_type:
  33. value.int_value = other.value.int_value;
  34. break;
  35. case var_types::float_type:
  36. value.int_value = other.value.float_value;
  37. break;
  38. case var_types::none:
  39. value.int_value = 0;
  40. break;
  41. }
  42. break;
  43. case var_types::float_type:
  44. switch (other.type)
  45. {
  46. case var_types::int_type:
  47. value.float_value = other.value.int_value;
  48. break;
  49. case var_types::float_type:
  50. value.float_value = other.value.float_value;
  51. break;
  52. case var_types::none:
  53. value.float_value = 0;
  54. break;
  55. }
  56. break;
  57. case var_types::none:
  58. break;
  59. }
  60. return *this;
  61. }
  62.  
  63. variable variable::operator*(const variable& other)
  64. {
  65. switch (type)
  66. {
  67. case var_types::int_type:
  68. switch (other.type)
  69. {
  70. case var_types::int_type:
  71. return value.int_value * other.value.int_value;
  72. case var_types::float_type:
  73. return value.int_value * other.value.float_value;
  74. case var_types::none:
  75. return variable{};
  76. }
  77. case var_types::float_type:
  78. switch (other.type)
  79. {
  80. case var_types::int_type:
  81. return value.float_value * other.value.int_value;
  82. case var_types::float_type:
  83. return value.float_value * other.value.float_value;
  84. case var_types::none:
  85. return variable{};
  86. }
  87. case var_types::none:
  88. return variable{};
  89. }
  90. }
  91.  
  92. variable variable::operator+(const variable& other)
  93. {
  94. switch (type)
  95. {
  96. case var_types::int_type:
  97. switch (other.type)
  98. {
  99. case var_types::int_type:
  100. return value.int_value + other.value.int_value;
  101. case var_types::float_type:
  102. return value.int_value + other.value.float_value;
  103. case var_types::none:
  104. return variable{};
  105. }
  106. case var_types::float_type:
  107. switch (other.type)
  108. {
  109. case var_types::int_type:
  110. return value.float_value + other.value.int_value;
  111. case var_types::float_type:
  112. return value.float_value + other.value.float_value;
  113. case var_types::none:
  114. return variable{};
  115. }
  116. case var_types::none:
  117. return variable{};
  118. }
  119. }
  120.  
  121. variable variable::operator-(const variable& other)
  122. {
  123. switch (type)
  124. {
  125. case var_types::int_type:
  126. switch (other.type)
  127. {
  128. case var_types::int_type:
  129. return value.int_value - other.value.int_value;
  130. case var_types::float_type:
  131. return value.int_value - other.value.float_value;
  132. case var_types::none:
  133. return variable{};
  134. }
  135. case var_types::float_type:
  136. switch (other.type)
  137. {
  138. case var_types::int_type:
  139. return value.float_value - other.value.int_value;
  140. case var_types::float_type:
  141. return value.float_value - other.value.float_value;
  142. case var_types::none:
  143. return variable{};
  144. }
  145. case var_types::none:
  146. return variable{};
  147. }
  148. }
  149.  
  150. variable variable::operator/(const variable& other)
  151. {
  152. switch (type)
  153. {
  154. case var_types::int_type:
  155. switch (other.type)
  156. {
  157. case var_types::int_type:
  158. return value.int_value / other.value.int_value;
  159. case var_types::float_type:
  160. return value.int_value / other.value.float_value;
  161. case var_types::none:
  162. return variable{};
  163. }
  164. case var_types::float_type:
  165. switch (other.type)
  166. {
  167. case var_types::int_type:
  168. return value.float_value / other.value.int_value;
  169. case var_types::float_type:
  170. return value.float_value / other.value.float_value;
  171. case var_types::none:
  172. return variable{};
  173. }
  174. case var_types::none:
  175. return variable{};
  176. }
  177. }
  178.  
  179. variable variable::operator%(const variable& other)
  180. {
  181. if (type != var_types::int_type || other.type != var_types::int_type)
  182. {
  183. throw std::exception{};
  184. }
  185. return variable{value.int_value % other.value.int_value};
  186. }
  187.  
  188. bool variable::operator==(const variable& other)
  189. {
  190. switch (type)
  191. {
  192. case var_types::int_type:
  193. switch (other.type)
  194. {
  195. case var_types::int_type:
  196. return value.int_value == other.value.int_value;
  197. break;
  198. case var_types::float_type:
  199. return value.int_value == other.value.float_value;
  200. case var_types::none:
  201. return false;
  202. }
  203. case var_types::float_type:
  204. switch (other.type)
  205. {
  206. case var_types::int_type:
  207. return value.float_value == other.value.int_value;
  208. case var_types::float_type:
  209. return value.float_value == other.value.float_value;
  210. case var_types::none:
  211. return false;
  212. }
  213. case var_types::none:
  214. return false;
  215. }
  216. }
  217.  
  218. bool variable::operator>(const variable& other)
  219. {
  220. switch (type)
  221. {
  222. case var_types::int_type:
  223. switch (other.type)
  224. {
  225. case var_types::int_type:
  226. return value.int_value > other.value.int_value;
  227. break;
  228. case var_types::float_type:
  229. return value.int_value > other.value.float_value;
  230. case var_types::none:
  231. return false;
  232. }
  233. case var_types::float_type:
  234. switch (other.type)
  235. {
  236. case var_types::int_type:
  237. return value.float_value > other.value.int_value;
  238. case var_types::float_type:
  239. return value.float_value > other.value.float_value;
  240. case var_types::none:
  241. return false;
  242. }
  243. case var_types::none:
  244. return false;
  245. }
  246. }
  247.  
  248. bool variable::operator!=(const variable& other)
  249. {
  250. return !(*this == other);
  251. }
  252.  
  253. bool variable::operator<(const variable& other)
  254. {
  255. return !(*this >= other);
  256. }
  257.  
  258. bool variable::operator>=(const variable& other)
  259. {
  260. return (*this > other) || (*this == other);
  261. }
  262.  
  263. bool variable::operator<=(const variable& other)
  264. {
  265. return (*this < other) || (*this == other);
  266. }
  267.  
  268. variable::operator int()
  269. {
  270. switch (type)
  271. {
  272. case var_types::int_type:
  273. return value.int_value;
  274. case var_types::float_type:
  275. return value.float_value;
  276. case var_types::none:
  277. return 0;
  278. }
  279. }
  280.  
  281. void variable::reinit(int val)
  282. {
  283. type = var_types::int_type;
  284. value.int_value = val;
  285. }
  286.  
  287. void variable::reinit(float val)
  288. {
  289. type = var_types::float_type;
  290. value.float_value = val;
  291. }
  292.  
  293. void variable::reinit(const variable& other)
  294. {
  295. type = other.type;
  296. value = other.value;
  297. }
  298.  
  299. std::ostream& operator<<(std::ostream& out, const variable& val)
  300. {
  301. switch (val.type)
  302. {
  303. case var_types::int_type:
  304. return out << val.value.int_value;
  305. case var_types::float_type:
  306. return out << val.value.float_value;
  307. case var_types::none:
  308. return out << "none";
  309. }
  310. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement