Implemented expression tree
authorNilsForssen <forssennils@gmail.com>
Mon, 9 Oct 2023 09:33:46 +0000 (11:33 +0200)
committerNilsForssen <forssennils@gmail.com>
Mon, 9 Oct 2023 09:33:46 +0000 (11:33 +0200)
src/expression.cc
src/expression.h
src/node.h [new file with mode: 0644]
src/operands.h
src/operators.h

index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..36e3a252c58e150e094816ae03dca4dea4e55138 100644 (file)
@@ -0,0 +1,99 @@
+#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
index d1d1a999076e51ab0e316b369dafcfc553481486..0ddab7147ba75d7cec01545c48dacbb8066d1eba 100644 (file)
@@ -2,29 +2,54 @@
 #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
diff --git a/src/node.h b/src/node.h
new file mode 100644 (file)
index 0000000..d94663a
--- /dev/null
@@ -0,0 +1,20 @@
+#ifndef NODE_H
+#define NODE_H
+
+#include <string>
+
+class Node
+{
+public:
+    virtual ~Node() = default;
+
+    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;
+
+private:
+protected:
+};
+
+#endif
\ No newline at end of file
index fd43bd07678e7dd4383c27a10330b7dc3712b377..04769fe2a0bd1e19e90cc0cf2db1c396c999214e 100644 (file)
@@ -5,7 +5,7 @@
 #include <sstream>
 #include <iomanip>
 
-#include "expression.h"
+#include "node.h"
 
 class Operand : public Node
 {
index 2730c3f3dbaeac7f019912d90847f674436ecdd9..5023a397e4cd8608aa7c734aa5be195de4ae3773 100644 (file)
@@ -6,7 +6,7 @@
 #include <stdexcept>
 #include <cmath>
 
-#include "expression.h"
+#include "node.h"
 
 class Operator : public Node
 {
@@ -17,7 +17,7 @@ public:
     Operator & operator=(Operator const &) = delete;
 
     virtual ~Operator() = default;
-    
+
     std::string get_symbol() const;
     std::string get_postfix() const override;
     std::string get_infix() const override;