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())
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
{
}
else
{
+ while(!stack.empty())
+ {
+ delete stack.top();
+ stack.pop();
+ }
throw std::logic_error("Undefined characters in expression");
}
stack.push(new_node);
{
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)
return *this;
}
+ delete expression_root;
expression_root = other.expression_root;
other.unlink();
return *this;
double value;
};
-
class Variable : public Operand
{
public:
double value;
};
-
-
-
#endif
\ No newline at end of file
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
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
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
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
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");
}
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;
class Addition : public Operator
{
public:
- Addition(Node & l, Node & r);
-
+ Addition(Node * l, Node * r);
double get_value() const override;
protected:
private:
class Subtraction : public Operator
{
public:
- Subtraction(Node & l, Node & r);
-
+ Subtraction(Node * l, Node * r);
double get_value() const override;
protected:
private:
class Multiplication : public Operator
{
public:
- Multiplication(Node & l, Node & r);
-
+ Multiplication(Node * l, Node * r);
double get_value() const override;
protected:
private:
class Division : public Operator
{
public:
- Division(Node & l, Node & r);
-
+ Division(Node * l, Node * r);
double get_value() const override;
protected:
private:
class Exponent : public Operator
{
public:
- Exponent(Node & l, Node & r);
+ Exponent(Node * l, Node * r);
double get_value() const override;
protected: