diff options
author | HombreLaser <sebastian-440@live.com> | 2022-04-09 11:02:34 -0500 |
---|---|---|
committer | HombreLaser <sebastian-440@live.com> | 2022-04-09 11:02:34 -0500 |
commit | ae25e110ca9d65c4e2cfffef21abc16f26ac3739 (patch) | |
tree | 7cd18c66fec436952b138b21011848514f96eee1 | |
parent | 643e33711781fb5a2a3ffc1b8b4b0031bf9509a1 (diff) |
Corregidos errores de destructores
-rw-r--r-- | expression_base.cpp | 6 | ||||
-rw-r--r-- | expressions.cpp | 100 | ||||
-rw-r--r-- | include/expressions.hpp | 50 |
3 files changed, 121 insertions, 35 deletions
diff --git a/expression_base.cpp b/expression_base.cpp index 534e4f4..3202328 100644 --- a/expression_base.cpp +++ b/expression_base.cpp @@ -11,10 +11,6 @@ using std::string; Expression::Expression(Expression *left, Expression *right, token_type type) : left(left), right(right), type(type) {} -Expression::~Expression() { - delTree(); -} - const Expression *Expression::getLeft() { return left; } @@ -71,6 +67,4 @@ void Expression::delTree() { if(getRight() != NULL) delete right; - - delete this; } diff --git a/expressions.cpp b/expressions.cpp index e402020..722794f 100644 --- a/expressions.cpp +++ b/expressions.cpp @@ -1,5 +1,7 @@ #include <cstddef> +#include <math.h> #include <string> +#include <cmath> #include "include/lexer.hpp" #include "include/expressions.hpp" @@ -10,14 +12,16 @@ Literal::Literal(int value) : Expression(NULL, NULL, number) { this->value = value; } -Literal::~Literal() { - delTree(); -} +Literal::~Literal() {} void Literal::setRepr() { repr = to_string(value); } +float Literal::eval(int arg) { + return value; +} + // Function. Function::Function(Expression *arg, trig_functions name) : Expression(NULL, NULL, function) { this->arg = arg; @@ -28,36 +32,56 @@ Function::~Function() { delTree(); } -trig_functions Function::getFunctionName() { return function_name; } +trig_functions Function::getFunctionName() { + return function_name; +} void Function::delTree() { delete arg; - delete this; +} + +float Function::eval(int arg) { + float function_arg = this->arg->eval(arg); + + switch(function_name) { + case i_sin: + return std::sin(function_arg); + case i_cos: + return std::cos(function_arg); + case i_tan: + return std::tan(function_arg); + case i_csc: + return std::asin(function_arg); + case i_sec: + return std::acos(function_arg); + case i_ctg: + return std::atan(function_arg); + } } void Function::setRepr() { switch(function_name) { - case sin: + case i_sin: repr += "sin"; break; - case cos: + case i_cos: repr += "cos"; break; - case tan: + case i_tan: repr += "tan"; break; - case csc: + case i_csc: repr += "csc"; break; - case sec: + case i_sec: repr += "sec"; break; - case ctg: + case i_ctg: repr += "ctg"; break; @@ -74,14 +98,16 @@ Variable::Variable(char name) : Expression(NULL, NULL, variable) { setRepr(); } -Variable::~Variable() { - delTree(); -} +Variable::~Variable() {} void Variable::setRepr() { repr += name; } +float Variable::eval(int arg) { + return arg; +} + // Sum. AddExpression::AddExpression(Expression *left, Expression *right) : Expression(left, right, sum) {} @@ -90,6 +116,13 @@ AddExpression::~AddExpression() { delTree(); } +float AddExpression::eval(int arg) { + float right_val = right->eval(arg); + float left_val = left->eval(arg); + + return right_val + left_val; +} + // Substraction. SubExpression::SubExpression(Expression *left, Expression *right) : Expression(left, right, substraction) {} @@ -98,6 +131,13 @@ SubExpression::~SubExpression() { delTree(); } +float SubExpression::eval(int arg) { + float right_val = right->eval(arg); + float left_val = left->eval(arg); + + return right_val - left_val; +} + // Negation. NegationExpression::NegationExpression(Expression *right) : Expression(NULL, right, substraction) {} @@ -106,6 +146,17 @@ NegationExpression::~NegationExpression() { delTree(); } +void NegationExpression::setRepr() { + repr = "-("; + repr += right->getRepr() + ")"; +} + +float NegationExpression::eval(int arg) { + float expr_value = right->eval(arg); + + return -1 * expr_value; +} + // División. DivisionExpression::DivisionExpression(Expression *left, Expression *right) : Expression(left, right, division) {} @@ -114,6 +165,13 @@ DivisionExpression::~DivisionExpression() { delTree(); } +float DivisionExpression::eval(int arg) { + float right_val = right->eval(arg); + float left_val = left->eval(arg); + + return left_val / right_val; +} + // Multiplication. MultiplicationExpression::MultiplicationExpression(Expression *left, Expression *right) @@ -123,6 +181,13 @@ MultiplicationExpression::~MultiplicationExpression() { delTree(); } +float MultiplicationExpression::eval(int arg) { + float right_val = right->eval(arg); + float left_val = left->eval(arg); + + return left_val * right_val; +} + // Power. PowerExpression::PowerExpression(Expression *left, Expression *right) : Expression(left, right, power) {} @@ -130,3 +195,10 @@ PowerExpression::PowerExpression(Expression *left, Expression *right) PowerExpression::~PowerExpression() { delTree(); } + +float PowerExpression::eval(int arg) { + float right_val = right->eval(arg); + float left_val = left->eval(arg); + + return std::pow(left_val, right_val); +} diff --git a/include/expressions.hpp b/include/expressions.hpp index 908272f..974612d 100644 --- a/include/expressions.hpp +++ b/include/expressions.hpp @@ -7,27 +7,34 @@ #ifndef EXPRESSIONS_H #define EXPRESSIONS_H +/* Debido a que en eval() llamamos a la respectiva + función trigonométrica, hay un conflicto entre + nombres de las funciones definidas en cmath y + los nombres definidos aquí. Por esa razón decidí + renombrarlos con el prefijo i_ que significa + "identifier". Esto puede ser solucionado con un + namespace, pero es mucho pedo, así lo dejo.*/ enum trig_functions { - sin, - cos, - tan, - csc, - sec, - ctg + i_sin, + i_cos, + i_tan, + i_csc, + i_sec, + i_ctg }; class Expression { -private: - Expression *left; - Expression *right; - token_type type; public: Expression(Expression *left, Expression *right, token_type type); - virtual ~Expression() = 0; + virtual ~Expression() = default; std::string getRepr(); const Expression *getLeft(); const Expression *getRight(); + virtual float eval(int arg) = 0; protected: + Expression *left; + Expression *right; + token_type type; virtual void setRepr(); void delTree(); std::string repr{}; @@ -37,9 +44,11 @@ class Literal : public Expression { private: int value; public: - Literal(int value); + explicit Literal(int value); ~Literal(); int getValue(); + float eval(int arg) override; +protected: void setRepr() override; }; @@ -50,18 +59,21 @@ private: public: Function(Expression *arg, trig_functions name); ~Function(); + float eval(int arg) override; trig_functions getFunctionName(); - void setRepr() override; protected: void delTree(); + void setRepr() override; }; class Variable : public Expression { private: char name; public: - Variable(char name); + explicit Variable(char name); ~Variable(); + float eval(int arg) override; +protected: void setRepr() override; }; @@ -69,36 +81,44 @@ class AddExpression : public Expression { public: AddExpression(Expression *left, Expression *right); ~AddExpression(); + float eval(int arg) override; }; class SubExpression : public Expression { public: SubExpression(Expression *left, Expression *right); ~SubExpression(); + float eval(int arg) override; }; class NegationExpression : public Expression { public: - NegationExpression(Expression *right); + explicit NegationExpression(Expression *right); ~NegationExpression(); + float eval(int arg) override; +protected: + void setRepr() override; }; class DivisionExpression : public Expression { public: DivisionExpression(Expression *left, Expression *right); ~DivisionExpression(); + float eval(int arg) override; }; class MultiplicationExpression : public Expression { public: MultiplicationExpression(Expression *left, Expression *right); ~MultiplicationExpression(); + float eval(int arg) override; }; class PowerExpression : public Expression { public: PowerExpression(Expression *left, Expression *right); ~PowerExpression(); + float eval(int arg) override; }; #endif /* EXPRESSIONS_H */ |