Advertisement
SIKER_98

Untitled

Jan 14th, 2020
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.78 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <cmath>
  4.  
  5. using namespace std;
  6.  
  7. class Range {
  8. private:
  9. double start, end;
  10. public:
  11. double getStart() {
  12. return start;
  13. }
  14.  
  15. double getEnd() {
  16. return end;
  17. }
  18.  
  19. Range(double start, double end) {
  20. this->start = start;
  21. this->end = end;
  22. }
  23.  
  24. bool contains(double val) {
  25. return val >= start && val <= end;
  26. }
  27. };
  28.  
  29. class Function {
  30. private:
  31. //std::vector<Range *> domain;
  32. public:
  33. std::vector<Range *> domain;
  34.  
  35. virtual void setDomain(std::vector<Range *> ranges) { domain = ranges; }
  36.  
  37. virtual bool isWithinDomain(double x) {
  38. if (domain.size() == 0) {
  39. return true;
  40. }
  41. for (Range *domainPart : domain) {
  42. if (domainPart->contains(x)) {
  43. return true;
  44. }
  45. }
  46. return false;
  47. }
  48.  
  49. virtual bool isWithinRange(double y) = 0;
  50.  
  51. virtual double getY(double x) = 0;
  52.  
  53. virtual std::vector<double> getRoots() = 0;
  54.  
  55. virtual int getRootCount() {
  56. return getRoots().size();
  57. };
  58. };
  59.  
  60. class LinearFunction : public Function {
  61. private:
  62. double a, b;
  63. public:
  64. LinearFunction(double x, double y) {
  65. this->a = x;
  66. this->b = y;
  67. }
  68.  
  69. double getY(double x) override {
  70. return this->a * x + this->b;
  71. };
  72.  
  73. bool isWithinRange(double y) override {
  74. if (this->a == 0) {
  75. return y == this->b;
  76. } else {
  77. double x = (y - this->b) / this->a;
  78. return isWithinDomain(x);
  79. }
  80. };
  81.  
  82. vector<double> getRoots() override {
  83. vector<double> roots;
  84. if (a != 0) {
  85. double x = -(this->b) / this->a;
  86. if (isWithinDomain(x) == 1)roots.push_back(x);
  87. }
  88. return roots;
  89. };
  90.  
  91. /*int getRootCount() override {
  92. return this->a == 0 ? 0 : 1;
  93. };*/
  94. };
  95.  
  96. //-------------------------------
  97.  
  98. class SquareFunction : public Function {
  99. private:
  100. double a, b, c;
  101. public:
  102.  
  103. SquareFunction(double x, double y, double z) {
  104. this->a = x;
  105. this->b = y;
  106. this->c = z;
  107. }
  108.  
  109. double getY(double x) override {
  110. double a = this->a;
  111. double b = this->b;
  112. double c = this->c;
  113. return a * x * x + b * x + c;
  114. }
  115.  
  116. bool isWithinRange(double y) override {
  117. if (a == 0) {//liniowa
  118. if (b == 0) {
  119. return y == this->c;
  120. } else {
  121. double x = (y - this->c) / this->b;
  122. return isWithinDomain(x);
  123. }
  124. } else { //kwadratowa
  125. double p = -b / (2 * a);
  126. double delta = b * b - 4 * a * c;
  127. double q = -delta / (4 * a);
  128. double x = sqrt((y - q) / a) + p;
  129. if (isWithinDomain(x) == 1) {
  130. return (a * (x - p) * (x - p) + q) == (a * x * x + b * x + c);
  131. }
  132. }
  133. };
  134.  
  135. vector<double> getRoots() override {
  136. vector<double> roots;
  137. double delta;
  138. double zero;
  139. /*
  140. if (a != 0) {
  141. double x = -(this->b) / this->a;
  142. if (isWithinDomain(x) == 1)roots.push_back(x);
  143. }
  144. */
  145. if (a == 0) { // liniowa
  146. if (b != 0) {
  147. double x = -(this->c) / this->b;
  148. if (isWithinDomain(x) == 1)roots.push_back(x);
  149. }
  150. } else { // kwadratowa
  151. delta = b * b - 4 * a * c;
  152. delta = sqrt(delta);
  153. if (delta > 0) {
  154. double x = (-b + delta) / (2 * a);
  155. roots.push_back(x);
  156. x = (-b - delta) / (2 * a);
  157. roots.push_back(x);
  158.  
  159. } else if (delta == 0) {
  160. double x = -b / (2 * a);
  161. roots.push_back(x);
  162. }
  163. }
  164. return roots;
  165. };
  166. };
  167.  
  168.  
  169. ///------------------
  170.  
  171. class HomographicFunction : public Function {
  172. private:
  173. double a, b, c, d;
  174. public:
  175. HomographicFunction(double a, double b, double c, double d) {
  176. this->a = a;
  177. this->b = b;
  178. this->c = c;
  179. this->d = d;
  180. };
  181.  
  182. double getY(double x) {
  183. double a = this->a;
  184. double b = this->b;
  185. double c = this->c;
  186. double d = this->d;
  187. return (a * x + b) / (c * x + d);
  188. };
  189.  
  190.  
  191. bool isWithinRange(double y) override {
  192.  
  193. if (y == a / c) return 0;
  194.  
  195. double x = ((d * y - b) / (a * c - y * c));
  196. if (isWithinDomain(x) == 1);
  197. return y == (a * x + b) / (c * x + d);
  198. };
  199.  
  200. bool isWithinDomain(double x) {
  201. if (domain.size() == 0 && x != -d / c) {
  202. return true;
  203. }
  204. for (Range *domainPart : domain) {
  205. if (domainPart->contains(x)) {
  206. return true;
  207. }
  208. }
  209. return false;
  210. }
  211.  
  212. vector<double> getRoots() override {
  213. vector<double> roots;
  214. double x;
  215. if (-d / c != 0) {
  216. x = -b / a;
  217. roots.push_back(x);
  218. }
  219. if (-d / c == 0 && -b / a == 0) {
  220. x = -b / a;
  221. roots.push_back(x);
  222. }
  223.  
  224. return roots;
  225. };
  226. };
  227.  
  228.  
  229. ///---------------
  230.  
  231. int main() {
  232. HomographicFunction *funkcja = new HomographicFunction(2, 3, 1, 1);
  233. cout << "wartosc: " << funkcja->getY(2) << endl;
  234. cout << "dziedzina: " << funkcja->isWithinDomain(-1) << endl;
  235. cout << "przeciwdziedzina: " << funkcja->isWithinRange(2) << endl;
  236. cout << "ilosc pierwiastkow: " << funkcja->getRootCount() << endl;
  237. for (int i = 0; i < funkcja->getRoots().size(); i++) {
  238. cout << "pierwiastek " << funkcja->getRoots()[i] << endl;
  239. }
  240. return 0;
  241. }
  242.  
  243. //-----------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement