Advertisement
xladomaz

3laba_sanya

Oct 28th, 2020
2,335
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.98 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <string>
  4. #include <vector>
  5.  
  6. using namespace std;
  7.  
  8. class VectorFunc {
  9. protected:
  10.     double a = 0;
  11.     double b = 0;
  12.  
  13. public:
  14.     virtual double calculate(double x) = 0;
  15.     virtual void print_function() = 0;
  16.  
  17.     void input_value() {
  18.         cout << "Введите a и b";
  19.         cin >> a >> b;
  20.     }
  21. };
  22.  
  23. class Func1 : public VectorFunc {
  24. public:
  25.     double calculate(double x) {
  26.         return a * sin(b * x);
  27.     }
  28.  
  29.     void print_function() {
  30.         cout << a << "*sin(" << b << "*x)";
  31.     }
  32. };
  33.  
  34. class Func2 : public VectorFunc {
  35. public:
  36.     double calculate(double x) {
  37.         return a * pow(x, b);
  38.     }
  39.  
  40.     void print_function() {
  41.         cout << a << "x^" << b;
  42.     }
  43. };
  44.  
  45. int input_function();
  46.  
  47. int main() {
  48.     setlocale(LC_ALL, "Russian");
  49.  
  50.     int N;
  51.     cout << "Введите размерность массива: ";
  52.     cin >> N;
  53.  
  54.     VectorFunc** functions = new VectorFunc * [N];
  55.  
  56.     int i = 0, is_exit_func_add;
  57.     for (int i = 0; i < N; i++) {
  58.         if (input_function() == 1)
  59.             functions[i] = new Func1();
  60.         else
  61.             functions[i] = new Func2();
  62.  
  63.         cout << i + 1 << ". ";
  64.         functions[i]->input_value();
  65.     }
  66.  
  67.     cout << "Функции: " << endl;
  68.     for (int i = 0; i < N; i++) {
  69.         cout << i + 1 << ". ";
  70.         functions[i]->print_function();
  71.         cout << endl;
  72.     }
  73.  
  74.     int is_exit;
  75.     while (true) {
  76.         double x;
  77.         cout << "Введите x: ";
  78.         cin >> x;
  79.  
  80.         cout << "Результат: " << endl;
  81.         for (size_t i = 0; i < N; i++) {
  82.             cout << i + 1 << ". " << functions[i]->calculate(x) << endl;
  83.         }
  84.  
  85.         cout << "Выберите действие [продолжить(1)/выйти(0)]: ";
  86.         cin >> is_exit;
  87.         if (!is_exit) break;
  88.     }
  89.  
  90.     for (size_t i = 0; i < N; i++)
  91.         delete functions[i];
  92.  
  93.     return 0;
  94. }
  95.  
  96. int input_function() {
  97.     int id;
  98.     while (true) {
  99.         cout << "Выберите функцию: a * sin(b*x)(1) или a * x^b: ";
  100.         cin >> id;
  101.  
  102.         if (id == 1 || id == 2)
  103.             return id;
  104.  
  105.         cout << "Вы ввели неправильный номер." << endl;
  106.     }
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement