From d9c7ca9b19eb029e43c791930488e3f5b9b6974a Mon Sep 17 00:00:00 2001 From: NilsForssen Date: Fri, 6 Oct 2023 16:28:34 +0200 Subject: [PATCH] Added infix and prefix and some tests --- src/expression.h | 5 +++++ src/operands.cc | 10 ++++++++++ src/operands.h | 17 ++++++++++++++--- src/operators.cc | 20 +++++++++++++++++--- src/operators.h | 2 ++ 5 files changed, 48 insertions(+), 6 deletions(-) diff --git a/src/expression.h b/src/expression.h index fab63e7..d1d1a99 100644 --- a/src/expression.h +++ b/src/expression.h @@ -4,6 +4,9 @@ #include class Equation { +public: +protected: +private: }; class Node { @@ -12,6 +15,8 @@ public: 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: diff --git a/src/operands.cc b/src/operands.cc index 4d2092d..6b8487f 100644 --- a/src/operands.cc +++ b/src/operands.cc @@ -44,4 +44,14 @@ double Variable::get_value() const std::string Variable::get_postfix() const { return name; +} + +std::string Operand::get_infix() const +{ + return get_postfix(); +} + +std::string Operand::get_prefix() const +{ + return get_postfix(); } \ No newline at end of file diff --git a/src/operands.h b/src/operands.h index f658aaf..fd43bd0 100644 --- a/src/operands.h +++ b/src/operands.h @@ -7,8 +7,17 @@ #include "expression.h" +class Operand : public Node +{ +public: + virtual ~Operand() = default; + std::string get_infix() const override; + std::string get_prefix() const override; +protected: +private: +}; -class Const_int : public Node +class Const_int : public Operand { public: Const_int(int v); @@ -16,13 +25,14 @@ public: double get_value() const override; std::string get_postfix() const override; + protected: private: int value; }; -class Const_double : public Node +class Const_double : public Operand { public: Const_double(double v); @@ -36,13 +46,14 @@ private: }; -class Variable : public Node +class Variable : public Operand { public: Variable(std::string & n, double v); double get_value() const override; std::string get_postfix() const override; + protected: private: std::string name; diff --git a/src/operators.cc b/src/operators.cc index b700daa..f4e43a3 100644 --- a/src/operators.cc +++ b/src/operators.cc @@ -16,6 +16,20 @@ std::string Operator::get_postfix() const return concat.str(); } +std::string Operator::get_infix() const +{ + std::stringstream concat{}; + concat << "(" << lhs->get_infix() << symbol << rhs->get_infix() << ")"; + return concat.str(); +} + +std::string Operator::get_prefix() const +{ + std::stringstream concat{}; + concat << symbol << " " << lhs->get_postfix() << " " << rhs->get_postfix(); + return concat.str(); +} + Addition::Addition(Node & l, Node & r) : Operator{&l, &r, "+"} {} @@ -61,13 +75,13 @@ Exponent::Exponent(Node & l, Node & r) double Exponent::get_value() const { - if (lhs->get_value() < 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"); } - if (lhs->get_value() == 0 && rhs->get_value() < 0) + if (lhs->get_value() == 0.0 && rhs->get_value() < 0.0) { - std::logic_error("Cannot have base of 0 and negative exponent"); + throw std::logic_error("Cannot have base of 0 and negative exponent"); } return pow(lhs->get_value(), rhs->get_value()); diff --git a/src/operators.h b/src/operators.h index ddba79b..2730c3f 100644 --- a/src/operators.h +++ b/src/operators.h @@ -20,6 +20,8 @@ public: std::string get_symbol() const; std::string get_postfix() const override; + std::string get_infix() const override; + std::string get_prefix() const override; protected: Node * rhs; -- 2.30.2