{
throw std::logic_error("Missing operands");
}
-
+
if (token == "+")
{
new_node = new Addition{*lhs, *rhs};
expression_root = stack.top();
}
+Expression::Expression(Expression && other)
+: expression_root{nullptr}
+{
+ *this = std::move(other);
+}
+
+Expression::~Expression()
+{
+ delete expression_root;
+}
+
std::string Expression::to_infix_string() const
{
return expression_root->get_infix();
double Expression::evaluate() const
{
return expression_root->get_value();
+}
+
+void Expression::unlink()
+{
+ expression_root = nullptr;
+}
+
+Expression & Expression::operator=(Expression && other)
+{
+ if (this == &other)
+ {
+ // Do nothing, return
+ return *this;
+ }
+
+ expression_root = other.expression_root;
+ other.unlink();
+ return *this;
}
\ No newline at end of file
#include <string>
#include <sstream>
+#include <cctype> //Behövs?
+#include <algorithm> //Behövs?
+#include <stdexcept>
#include "postfix.h"
#include "token.h"
#include "operators.h"
#include "operands.h"
-#include <iostream> // TA bort
-#include <cctype>
-#include <algorithm>
-#include <stdexcept>
-
class Expression
{
public:
Expression(const std::string & expression);
+ Expression(Expression && other); // Move constructor
+ ~Expression();
Expression(Expression const&) = delete;
Expression& operator=(Expression const&) = delete;
std::string to_infix_string() const;
std::string to_postfix_string() const;
std::string to_prefix_string() const;
-
+
+ Expression & operator=(Expression && other); // Move operator
+
double evaluate() const;
+ void unlink();
+
protected:
private:
Node *expression_root;
};
-class Result
-{
-};
-
#endif
\ No newline at end of file
: rhs{r}, lhs{l}, symbol{sym}
{}
+Operator::~Operator()
+{
+ delete rhs;
+ delete lhs;
+}
+
std::string Operator::get_symbol() const
{
return symbol;
Operator(Operator const &) = delete;
Operator & operator=(Operator const &) = delete;
- virtual ~Operator() = default;
-
+ ~Operator();
std::string get_symbol() const;
std::string get_postfix() const override;
std::string get_infix() const override;
}
else
{
- Expression e{line};
+ e = line;
}
}
return 0;