From: Alma Dejus Date: Wed, 11 Oct 2023 15:06:24 +0000 (+0200) Subject: Changed from reference to pointer X-Git-Url: https://gitweb.forssennils.se/?a=commitdiff_plain;h=b165568065010a103206a8415dc3c55d95a38c43;p=TDDC76.git Changed from reference to pointer --- diff --git a/src/expression.cc b/src/expression.cc index ff1a8f0..92e3a2c 100644 --- a/src/expression.cc +++ b/src/expression.cc @@ -22,7 +22,12 @@ Expression::Expression(const std::string & expression) new_node = new Const_double{std::stod(token)}; } else if ( isalpha(token.at(0))) - { + { + while(!stack.empty()) + { + delete stack.top(); + stack.pop(); + } throw std::logic_error("Variables are not implemented"); } else if (token.is_operator()) @@ -34,28 +39,33 @@ Expression::Expression(const std::string & expression) if (lhs == nullptr || rhs == nullptr) { + while(!stack.empty()) + { + delete stack.top(); + stack.pop(); + } throw std::logic_error("Missing operands"); } if (token == "+") { - new_node = new Addition{*lhs, *rhs}; + new_node = new Addition{lhs, rhs}; } else if (token == "-") { - new_node = new Subtraction{*lhs, *rhs}; + new_node = new Subtraction{lhs, rhs}; } else if (token == "/") { - new_node = new Division{*lhs, *rhs}; + new_node = new Division{lhs, rhs}; } else if (token == "*") { - new_node = new Multiplication{*lhs, *rhs}; + new_node = new Multiplication{lhs, rhs}; } else if (token == "^") { - new_node = new Exponent{*lhs, *rhs}; + new_node = new Exponent{lhs, rhs}; } else { @@ -64,6 +74,11 @@ Expression::Expression(const std::string & expression) } else { + while(!stack.empty()) + { + delete stack.top(); + stack.pop(); + } throw std::logic_error("Undefined characters in expression"); } stack.push(new_node); @@ -72,12 +87,18 @@ Expression::Expression(const std::string & expression) { throw std::logic_error("Empty expression"); } + + expression_root = stack.top(); + if (stack.size() > 1) { + while(!stack.empty()) + { + delete stack.top(); + stack.pop(); + } throw std::logic_error("Missing operators"); } - - expression_root = stack.top(); } Expression::Expression(Expression && other) @@ -124,6 +145,7 @@ Expression & Expression::operator=(Expression && other) return *this; } + delete expression_root; expression_root = other.expression_root; other.unlink(); return *this; diff --git a/src/operands.h b/src/operands.h index 04769fe..135a47f 100644 --- a/src/operands.h +++ b/src/operands.h @@ -45,7 +45,6 @@ private: double value; }; - class Variable : public Operand { public: @@ -60,7 +59,4 @@ private: double value; }; - - - #endif \ No newline at end of file diff --git a/src/operators.cc b/src/operators.cc index f81a23e..cd16ad7 100644 --- a/src/operators.cc +++ b/src/operators.cc @@ -36,8 +36,8 @@ std::string Operator::get_prefix() const return concat.str(); } -Addition::Addition(Node & l, Node & r) -: Operator{&l, &r, "+"} +Addition::Addition(Node * l, Node * r) +: Operator{l, r, "+"} {} double Addition::get_value() const @@ -45,8 +45,8 @@ double Addition::get_value() const return lhs->get_value() + rhs->get_value(); } -Subtraction::Subtraction(Node & l, Node & r) -: Operator{&l, &r, "-"} +Subtraction::Subtraction(Node * l, Node * r) +: Operator{l, r, "-"} {} double Subtraction::get_value() const @@ -54,8 +54,8 @@ double Subtraction::get_value() const return lhs->get_value() - rhs->get_value(); } -Multiplication::Multiplication(Node & l, Node & r) -: Operator{&l, &r, "*"} +Multiplication::Multiplication(Node * l, Node * r) +: Operator{l, r, "*"} {} double Multiplication::get_value() const @@ -63,8 +63,8 @@ double Multiplication::get_value() const return lhs->get_value() * rhs->get_value(); } -Division::Division(Node & l, Node & r) -: Operator{&l, &r, "/"} +Division::Division(Node * l, Node * r) +: Operator{l, r, "/"} {} double Division::get_value() const @@ -75,13 +75,13 @@ double Division::get_value() const return lhs->get_value() / rhs->get_value(); } -Exponent::Exponent(Node & l, Node & r) -: Operator{&l, &r, "^"} +Exponent::Exponent(Node * l, Node * r) +: Operator{l, r, "^"} {} double Exponent::get_value() const { - if (lhs->get_value() < 0.0 && std::fmod(rhs->get_value(), 1) == 0.0) + if (lhs->get_value() < 0.0 && std::fmod(rhs->get_value(), 1) != 0.0) { throw std::logic_error("Cannot have negative base with decimal exponent"); } diff --git a/src/operators.h b/src/operators.h index d067fe9..bfe5bb6 100644 --- a/src/operators.h +++ b/src/operators.h @@ -15,8 +15,9 @@ public: Operator(Operator const &) = delete; Operator & operator=(Operator const &) = delete; - + ~Operator(); + std::string get_symbol() const; std::string get_postfix() const override; std::string get_infix() const override; @@ -32,8 +33,7 @@ private: class Addition : public Operator { public: - Addition(Node & l, Node & r); - + Addition(Node * l, Node * r); double get_value() const override; protected: private: @@ -42,8 +42,7 @@ private: class Subtraction : public Operator { public: - Subtraction(Node & l, Node & r); - + Subtraction(Node * l, Node * r); double get_value() const override; protected: private: @@ -52,8 +51,7 @@ private: class Multiplication : public Operator { public: - Multiplication(Node & l, Node & r); - + Multiplication(Node * l, Node * r); double get_value() const override; protected: private: @@ -62,8 +60,7 @@ private: class Division : public Operator { public: - Division(Node & l, Node & r); - + Division(Node * l, Node * r); double get_value() const override; protected: private: @@ -72,7 +69,7 @@ private: class Exponent : public Operator { public: - Exponent(Node & l, Node & r); + Exponent(Node * l, Node * r); double get_value() const override; protected: