#include "stdafx.h"
#include <string>
#include <iostream>
using namespace std;
class Expression //интерфейс для унарных операторов
{
public:
virtual Expression *diff() const = 0;
virtual ~Expression() {};
virtual string print() const = 0;
};
class Expression2 : public Expression //для бинарных операторов, т.к. у них разные сигнатуры с унарным
{
protected:
Expression *x1;
Expression *x2;
public:
virtual ~Expression2()
{
delete x1;
delete x2;
}
};
class Number : public Expression
{
private:
int val;
public:
Number() : val() {};
Number(const int n)
{
val = n;
}
Expression *diff() const override; //override пговорит об изменении родительского метода
string print() const override;
~Number();
};
Expression* Number::diff() const
{
return new Number(0); //дифференцируем число
}
Number::~Number() {}
string Number::print() const
{
return to_string(val);
}
class Variable : public Expression
{
private:
char name;
public:
Variable() : name() {};
Variable(const char obj)
{
name = obj;
}
Expression *diff() const override;
string print() const override;
~Variable();
};
Expression* Variable::diff() const
{
return new Number(1); //дифференцируем переменную
}
Variable::~Variable() {}
string Variable::print() const
{
return to_string(name);
}
class Sum : public Expression2
{
public:
Sum(Expression *a, Expression *b);
Expression *diff() const override;
string print() const override;
};
Expression *Sum::diff() const
{
return new Sum(x1->diff(), x2->diff());
}
string Sum::print() const
{
return "(" + x1->print() + " + " + x2->print() + ")";
}
Sum::Sum(Expression *a, Expression *b)
{
x1 = a;
x2 = b;
}
class Sub : public Expression2
{
public:
Sub(Expression *a, Expression *b);
Expression *diff() const override;
string print() const override;
};
Sub::Sub(Expression *a, Expression *b)
{
x1 = a;
x2 = b;
}
Expression *Sub::diff() const
{
return new Sub(x1->diff(), x2->diff());
}
string Sub::print() const
{
return "(" + x1->print() + "-" + x2->print() + ")";
}
class Mul : public Expression2
{
public:
Mul(Expression *a, Expression *b);
Expression *diff() const override;
string print() const override;
};
Mul::Mul(Expression *a, Expression *b)
{
x1 = a;
x2 = b;
}
Expression *Mul::diff() const
{
return new Sum(new Mul(x1->diff(), x2), new Mul(x1, x2->diff()));
}
string Mul::print() const
{
return "(" + x1->print() + "*" + x2->print() + ")";
}
class Div : public Expression2
{
public:
Div(Expression *a, Expression *b);
Expression *diff() const override;
string print() const override;
};
Div::Div(Expression *a, Expression *b)
{
x1 = a;
x2 = b;
}
Expression *Div::diff() const
{
return new Div(new Sub(new Mul(x1->diff(), x2), new Mul(x1, x2->diff())), new Mul(x2, x2));
}
string Div::print() const
{
return "(" + x1->print() + "/" + x2->print() + ")";
}
class Exp : public Expression
{
private: //нужна, тк аргумент тоже может быть функцией
Expression *x;
public:
Exp(Expression *val) : x(val) {};
Expression *diff() const override;
string print() const override;
~Exp() override; //?? зачем
};
Expression *Exp::diff() const
{
return new Mul(x->diff(), new Exp(x));
}
string Exp::print() const
{
return "exp(" + x->print() + ")";
}
Exp::~Exp() { delete x; }
class Ln : public Expression
{
private:
Expression *x;
public:
Ln(Expression *val) : x(val) {};
Expression *diff() const override;;
string print() const override;
~Ln() override;
};
Expression *Ln::diff() const
{
return new Mul(x->diff(), new Div(new Number(1), x));
}
string Ln::print() const
{
return "ln(" + x->print() + ")";
}
Ln::~Ln() { delete x; }
int main()
{
Expression * test = new Exp(new Variable('x')); //пример, как задавать функции
cout << test->print() << endl;
cout << test->diff()->print();
system("pause");
return 0;
}
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.