Changed operator parsing and added more string functionality
authorNilsForssen <forssennils@gmail.com>
Mon, 9 Oct 2023 10:26:31 +0000 (12:26 +0200)
committerNilsForssen <forssennils@gmail.com>
Mon, 9 Oct 2023 10:26:31 +0000 (12:26 +0200)
src/expression.cc
src/expression.h

index 36e3a252c58e150e094816ae03dca4dea4e55138..ed2cd5fe942344d2bad6fff4dca51674f47fff50 100644 (file)
@@ -13,8 +13,6 @@ Expression::Expression(const std::string & expression)
 
     while (iss >> token)
     {
-        std::cout << token << std::endl;
-
         if (token.is_integer())
         {
             new_node = new Const_int{std::stoi(token)};
@@ -29,36 +27,39 @@ Expression::Expression(const std::string & expression)
         }
         else if (token.is_operator())
         {
-            Node * lhs{stack.top()};
-            stack.pop();
             Node * rhs{stack.top()};
             stack.pop();
+            Node * lhs{stack.top()};
+            stack.pop();
 
             if (lhs == nullptr || rhs == nullptr) 
             {
                 throw std::logic_error("Missing operands");
             }
 
-            switch (operator_string_map[token])
+            if (token == "+")
             {
-            case ADDITION:
                 new_node = new Addition{*lhs, *rhs};
-                break;
-            case SUBTRACTION:
+            }
+            else if (token == "-")
+            {
                 new_node = new Subtraction{*lhs, *rhs};
-                break;
-            case DIVISION:
-                new_node = new Division{*lhs, *rhs};
-                break;
-            case MULTIPLICATION:
+            }
+            else if (token == "/")
+            {
+                new_node = new Division{*lhs, *rhs};        
+            }
+            else if (token == "*")
+            {
                 new_node = new Multiplication{*lhs, *rhs};
-                break;
-            case EXPONENT:
+            }
+            else if (token == "^")
+            {
                 new_node = new Exponent{*lhs, *rhs};
-                break;
-            default:
+            }
+            else
+            {
                 throw std::logic_error("Operator not implemented");
-                break;
             }
         }
         else
@@ -79,18 +80,19 @@ Expression::Expression(const std::string & expression)
     expression_root = stack.top();
 }
 
-std::string Expression::to_string(StringFormat s_format) const
+std::string Expression::to_infix_string() 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();
-    }
+    return expression_root->get_infix();
+}
+
+std::string Expression::to_postfix_string() const
+{
+    return expression_root->get_postfix();
+}
+
+std::string Expression::to_prefix_string() const
+{
+    return expression_root->get_prefix();
 }
 
 double Expression::evaluate() const
index 0ddab7147ba75d7cec01545c48dacbb8066d1eba..7ae5a9735bddf8506ee778478ffbd13aad7b6ee4 100644 (file)
 #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:
@@ -40,7 +22,10 @@ public:
     Expression(Expression const&) = delete;
     Expression& operator=(Expression const&) = delete;
 
-    std::string to_string(StringFormat s_format=INFIX) const;
+    std::string to_infix_string() const;
+    std::string to_postfix_string() const;
+    std::string to_prefix_string() const;
+    
     double evaluate() const;
 
 protected: