Advertisement
SIKER_98

woznica lab 5

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