Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- #include <string>
- #include <vector>
- using namespace std;
- class VectorFunc {
- protected:
- double a = 0;
- double b = 0;
- public:
- virtual double calculate(double x) = 0;
- virtual void print_function() = 0;
- void input_value() {
- cout << "Введите a и b";
- cin >> a >> b;
- }
- };
- class Func1 : public VectorFunc {
- public:
- double calculate(double x) {
- return a * sin(b * x);
- }
- void print_function() {
- cout << a << "*sin(" << b << "*x)";
- }
- };
- class Func2 : public VectorFunc {
- public:
- double calculate(double x) {
- return a * pow(x, b);
- }
- void print_function() {
- cout << a << "x^" << b;
- }
- };
- int input_function();
- int main() {
- setlocale(LC_ALL, "Russian");
- int N;
- cout << "Введите размерность массива: ";
- cin >> N;
- VectorFunc** functions = new VectorFunc * [N];
- int i = 0, is_exit_func_add;
- for (int i = 0; i < N; i++) {
- if (input_function() == 1)
- functions[i] = new Func1();
- else
- functions[i] = new Func2();
- cout << i + 1 << ". ";
- functions[i]->input_value();
- }
- cout << "Функции: " << endl;
- for (int i = 0; i < N; i++) {
- cout << i + 1 << ". ";
- functions[i]->print_function();
- cout << endl;
- }
- int is_exit;
- while (true) {
- double x;
- cout << "Введите x: ";
- cin >> x;
- cout << "Результат: " << endl;
- for (size_t i = 0; i < N; i++) {
- cout << i + 1 << ". " << functions[i]->calculate(x) << endl;
- }
- cout << "Выберите действие [продолжить(1)/выйти(0)]: ";
- cin >> is_exit;
- if (!is_exit) break;
- }
- for (size_t i = 0; i < N; i++)
- delete functions[i];
- return 0;
- }
- int input_function() {
- int id;
- while (true) {
- cout << "Выберите функцию: a * sin(b*x)(1) или a * x^b: ";
- cin >> id;
- if (id == 1 || id == 2)
- return id;
- cout << "Вы ввели неправильный номер." << endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement