+#include "expression.h"
+
+Expression::Expression(const std::string & expression)
+: expression_root{nullptr}
+{
+ std::string post_string{Postfix{expression}.to_string()};
+
+ std::stringstream iss{post_string};
+
+ Token token{};
+ std::stack<Node*> stack{};
+ Node * new_node{nullptr};
+
+ while (iss >> token)
+ {
+ std::cout << token << std::endl;
+
+ if (token.is_integer())
+ {
+ new_node = new Const_int{std::stoi(token)};
+ }
+ else if (token.is_decimal())
+ {
+ new_node = new Const_double{std::stod(token)};
+ }
+ else if ( isalpha(token.at(0)))
+ {
+ throw std::logic_error("Variables are not implemented");
+ }
+ else if (token.is_operator())
+ {
+ Node * lhs{stack.top()};
+ stack.pop();
+ Node * rhs{stack.top()};
+ stack.pop();
+
+ if (lhs == nullptr || rhs == nullptr)
+ {
+ throw std::logic_error("Missing operands");
+ }
+
+ switch (operator_string_map[token])
+ {
+ case ADDITION:
+ new_node = new Addition{*lhs, *rhs};
+ break;
+ case SUBTRACTION:
+ new_node = new Subtraction{*lhs, *rhs};
+ break;
+ case DIVISION:
+ new_node = new Division{*lhs, *rhs};
+ break;
+ case MULTIPLICATION:
+ new_node = new Multiplication{*lhs, *rhs};
+ break;
+ case EXPONENT:
+ new_node = new Exponent{*lhs, *rhs};
+ break;
+ default:
+ throw std::logic_error("Operator not implemented");
+ break;
+ }
+ }
+ else
+ {
+ throw std::logic_error("Undefined characters in expression");
+ }
+ stack.push(new_node);
+ }
+ if (stack.size() == 0)
+ {
+ throw std::logic_error("Empty expression");
+ }
+ if (stack.size() > 1)
+ {
+ throw std::logic_error("Missing operators");
+ }
+
+ expression_root = stack.top();
+}
+
+std::string Expression::to_string(StringFormat s_format) const
+{
+ switch(s_format)
+ {
+ case PREFIX:
+ return expression_root->get_prefix();
+ case POSTFIX:
+ return expression_root->get_postfix();
+ case INFIX:
+ default:
+ return expression_root->get_infix();
+ }
+}
+
+double Expression::evaluate() const
+{
+ return expression_root->get_value();
+}
\ No newline at end of file
#define EXPRESSION_H
#include <string>
-
-class Equation {
+#include <sstream>
+
+#include "postfix.h"
+#include "token.h"
+#include "operators.h"
+#include "operands.h"
+
+#include <iostream> // TA bort
+#include <cctype>
+#include <algorithm>
+#include <stdexcept>
+
+typedef enum
+{
+ PREFIX,
+ POSTFIX,
+ INFIX
+} StringFormat;
+
+typedef enum
+{
+ ADDITION,
+ SUBTRACTION,
+ MULTIPLICATION,
+ DIVISION,
+ EXPONENT
+} Operator_types;
+
+std::map<std::string, Operator_types> operator_string_map;
+
+class Expression
+{
public:
-protected:
-private:
-};
+ Expression(const std::string & expression);
-class Node {
-public:
- virtual ~Node() = default;
+ Expression(Expression const&) = delete;
+ Expression& operator=(Expression const&) = delete;
- virtual double get_value() const = 0;
- virtual std::string get_postfix() const = 0;
- virtual std::string get_infix() const = 0;
- virtual std::string get_prefix() const = 0;
+ std::string to_string(StringFormat s_format=INFIX) const;
+ double evaluate() const;
-private:
protected:
+private:
+ Node *expression_root;
};
-class Result {
+class Result
+{
};
-
-
#endif
\ No newline at end of file