Advertisement
Guest User

Untitled

a guest
Feb 20th, 2020
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.13 KB | None | 0 0
  1. // Calem zadania jest przećwiczenie operatora() i jego
  2. // wykorzystania w połączeniu z std::function.
  3. //
  4. // Proszę napisać klasy reprezentujące funkcje matematyczne:
  5. // - Sin -- sin(a*x+b)
  6. // - Sqrt -- pierwiastek 2go stopnia
  7. // - Linear -- funkcja liniowa a*x+b
  8. //
  9. // Mają one operator(), który zwraca odpowiednią wartość funkcji.
  10. // (mogą być we wspólnych plikach Functions.h i Functions.cpp)
  11. //
  12. // Proszę także napisać klasę FunctionSeries, która przyjmuje
  13. // i przechowuje obiekty funkcyjne i pozwala na obliczenie wyniku
  14. // kolejnych wywołań, np. dla dwóch funkcji w serii:
  15. //
  16. // FunctionSeries f
  17. // ...
  18. // f.calc(x) := f1( f2(x) )
  19. //
  20. // oraz zwraca wyniki pośrednich obliczeń przez operator[].
  21. // UWAGA: zalecane, by użyć std::vector
  22. //
  23. // Pliku lab12.cpp nie modyfikujemy (poza odkomentowaniem "błędnych" linii).
  24. // Program proszę kompilować przynajmniej z flaga -Wall.
  25. //
  26.  
  27. #include "Functions.h"
  28. #include "FunctionSeries.h"
  29. #include <iostream>
  30.  
  31. double rec(double x){
  32.     return x>0 ? x : 0;
  33. }
  34.  
  35. int main ()
  36. {
  37.   Sin sinus(M_PI, M_PI/3.);
  38.   const Sqrt squareroot;
  39.   Linear lin(-0.25, 1.0);
  40.  
  41.   double x = 10;
  42.  
  43.   std::cout << sinus(x) << std::endl;
  44.   std::cout << squareroot(x) << std::endl;
  45.   std::cout << lin(x) << std::endl;
  46.  
  47.   FunctionSeries fs;
  48.   fs.insert(sinus);
  49.   fs.insert(squareroot);
  50.   fs.insert(lin);
  51.   fs.insert(rec);
  52.  
  53.   double result1 = fs.calc(x);
  54.   double result2 = rec(lin(squareroot(sinus(x))));
  55.  
  56.   std::cout << "-- dla x=10:\t" << result1 << ", " << result2 << std::endl;
  57.  
  58.   x=0.5;
  59.   std::cout << "-- dla x=0.5:\t" << fs.calc(x) << std::endl;
  60.  
  61.   // operator[] zwracający wyniki pośrenich obliczeń
  62.   std::cout << "posrednie wyniki: " << std::endl;
  63.   double sinx = sinus(x);
  64.   std::cout << sinx << ", " << fs[0] << std::endl;
  65.   std::cout << squareroot(sinx) << ", " << fs[1] << std::endl;
  66.   std::cout << lin(squareroot(sinx)) << ", " << fs[2] << std::endl;
  67. }
  68. /* wynik dzialania programu:
  69.  
  70. 0.866025
  71. 3.16228
  72. -1.5
  73. -- dla x=10:    0.767349, 0.767349
  74. -- dla x=0.5:   0.823223
  75. posrednie wyniki:
  76. 0.5, 0.5
  77. 0.707107, 0.707107
  78. 0.823223, 0.823223
  79.  
  80. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement