Advertisement
Guest User

Untitled

a guest
Jan 4th, 2017
357
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.12 KB | None | 0 0
  1. #pragma once
  2. #include <string>
  3. #include <iostream>
  4. //todo init, add comments.
  5.  
  6. using namespace std;
  7. class poly
  8. {
  9. double coefficient[3];
  10. public:
  11. poly();
  12. poly(const poly& other);
  13. const poly& operator=(const poly&);
  14. bool operator==(const poly&);
  15. bool operator!=(const poly&);
  16. bool operator<(const poly&);
  17. bool operator>(const poly&); //surely there's a better way to implement those
  18. bool operator<=(const poly&);
  19. bool operator>=(const poly&);
  20.  
  21.  
  22. friend ostream& operator<<(ostream& os, const poly& vec)
  23. {
  24.  
  25. for(int i=0;i<3;i++)
  26. {
  27. os << "poly=" << round(vec.coefficient[i]) << "x^:" << i<<"*";
  28. return os;
  29. }
  30. }
  31. poly& operator+(const poly&);
  32. poly& operator-(const poly & p1);
  33. poly operator*(const double);
  34. poly operator/(const double);
  35.  
  36. ~poly();
  37. };
  38.  
  39.  
  40. poly::poly()
  41. {
  42. for (int i = 0; i < 3; i++)
  43. {
  44. coefficient[i] = 0;
  45. }
  46. }
  47.  
  48.  
  49. poly::~poly()
  50. {
  51. delete[] coefficient; //dont think it's neccesary as there are no pointers, might need to delete this before hand-in
  52. }
  53.  
  54. poly::poly(const poly& other)
  55. {
  56. for (int i = 0; i < 3; i++)
  57. {
  58. if (other.coefficient[i] != NULL)
  59. {
  60. this->coefficient[i] = other.coefficient[i];
  61. }
  62. if (other.coefficient[i] == NULL)
  63. {
  64. this->coefficient[i] = 0; //will come in handy in case we try to evaluate 1st and/or 2nd degree polynomials
  65. }
  66. }
  67. }
  68.  
  69. const poly& poly::operator=(const poly& other)
  70. {
  71. for (int i = 0; i < 3; i++)
  72. {
  73. if (other.coefficient[i] != NULL)
  74. {
  75. this->coefficient[i] = other.coefficient[i];
  76. }
  77. if (other.coefficient[i] == NULL)
  78. {
  79. this->coefficient[i] = 0;
  80. }
  81. }
  82. return *this;
  83. }
  84.  
  85. bool poly::operator==(const poly& other)
  86. {
  87. int trigger = 0; //if all 3 slots are equal then then polynomials are equal as well
  88. for (int i = 0; i < 3; i++)
  89. {
  90. if (this->coefficient[i] == other.coefficient[i])
  91. {
  92. trigger++;
  93. if (trigger <= 3 && i == 3)
  94. {
  95. return 1;
  96. }
  97. else
  98. {
  99. return 0;
  100.  
  101. }
  102. }
  103. }
  104. }
  105.  
  106.  
  107. bool poly::operator!=(const poly& other)
  108. {
  109. for (int i = 0; i < 3; i++)
  110. {
  111. if (this->coefficient[i] != other.coefficient[i])
  112. {
  113. return 1;
  114. }
  115. else { return 0; };
  116. }
  117. }
  118.  
  119.  
  120.  
  121. bool poly::operator<(const poly& other)
  122. {
  123.  
  124. for (int i = 0; i < 3; i++)
  125. {
  126. if (this->coefficient[i] == 0 && other.coefficient[i] != 0)
  127. {
  128. return 1;
  129. }
  130. if (this->coefficient[i] != 0 && other.coefficient[i] == 0)
  131. {
  132. return 0;
  133. }
  134. if (this->coefficient[i] != 0 && other.coefficient[i] != 0)
  135. {
  136. if (this->coefficient[i] > other.coefficient[i])
  137. {
  138. return 0;
  139.  
  140. }
  141. if (this->coefficient[i] < other.coefficient[i])
  142. {
  143. return 1;
  144. }
  145. }
  146. }
  147. }
  148.  
  149. bool poly::operator<=(const poly& other)
  150. {
  151.  
  152. for (int i = 0; i < 3; i++)
  153. {
  154. if (this->coefficient[i] == 0 && other.coefficient[i] != 0)
  155. {
  156. return 1;
  157. }
  158. if (this->coefficient[i] != 0 && other.coefficient[i] == 0)
  159. {
  160. return 0;
  161. }
  162. if (this->coefficient[i] != 0 && other.coefficient[i] != 0)
  163. {
  164. if (this->coefficient[i] >= other.coefficient[i])
  165. {
  166. return 0;
  167.  
  168. }
  169. if (this->coefficient[i] <= other.coefficient[i])
  170. {
  171. return 1;
  172. }
  173. }
  174. }
  175. }
  176.  
  177.  
  178. bool poly::operator>(const poly& other)
  179. {
  180.  
  181. for (int i = 0; i < 3; i++)
  182. {
  183. if (this->coefficient[i] == 0 && other.coefficient[i] != 0)
  184. {
  185. return 0;
  186. }
  187. if (this->coefficient[i] != 0 && other.coefficient[i] == 0)
  188. {
  189. return 1;
  190. }
  191. if (this->coefficient[i] != 0 && other.coefficient[i] != 0)
  192. {
  193. if (this->coefficient[i] > other.coefficient[i])
  194. {
  195. return 1;
  196.  
  197. }
  198. if (this->coefficient[i] < other.coefficient[i])
  199. {
  200. return 0;
  201. }
  202. }
  203. }
  204. }
  205.  
  206. bool poly::operator>=(const poly& other)
  207. {
  208.  
  209. for (int i = 0; i < 3; i++)
  210. {
  211. if (this->coefficient[i] == 0 && other.coefficient[i] != 0)
  212. {
  213. return 0;
  214. }
  215. if (this->coefficient[i] != 0 && other.coefficient[i] == 0)
  216. {
  217. return 1;
  218. }
  219. if (this->coefficient[i] != 0 && other.coefficient[i] != 0)
  220. {
  221. if (this->coefficient[i] >= other.coefficient[i])
  222. {
  223. return 1;
  224.  
  225. }
  226. if (this->coefficient[i] <= other.coefficient[i])
  227. {
  228. return 0;
  229. }
  230. }
  231. }
  232. }
  233.  
  234. //poly& poly::init(char s[50]) //a3*x ^ 3 + a2*x ^ 2 + a1*x ^ 1 + a0*x ^ 0
  235. //{
  236. // for (int i = 0; i < 50; i++)
  237. // {
  238. //
  239. // }
  240.  
  241. //}
  242.  
  243. poly& poly::operator+(const poly& p1)
  244. {
  245. poly temp;
  246. for (int i = 0; i < 3; i++)
  247. {
  248. temp.coefficient[i]=p1.coefficient[i] + this->coefficient[i];
  249. }
  250.  
  251. return temp;
  252. }
  253.  
  254. poly& poly::operator-(const poly& p1)
  255. {
  256. poly temp;
  257. for (int i = 0; i < 3; i++)
  258. {
  259. temp.coefficient[i]=p1.coefficient[i] - this->coefficient[i];
  260. }
  261. return temp;
  262. }
  263.  
  264. poly poly::operator*(const double d)
  265. {
  266. poly pp = *this;
  267. for (int i = 0; i < 3; i++)
  268. {
  269. pp.coefficient[i] *= d; //todo other way around something,see instructions
  270. }
  271. return pp;
  272. }
  273.  
  274. poly poly::operator/(const double d)
  275. {
  276. if (d != 0)
  277. {
  278. poly pp = *this;
  279. for (int i = 0; i < 3; i++)
  280. {
  281. pp.coefficient[i] /= d; //todo other way around something,see instructions
  282. }
  283. return pp;
  284. }
  285. else
  286. {
  287. cout << "are you trying to fuck up my computer?";
  288. }
  289. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement