#include <string>
class Equation {
+public:
+protected:
+private:
};
class Node {
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:
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
#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);
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);
};
-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;
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, "+"}
{}
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());
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;