Advertisement
SIKER_98

stadej

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