144 lines
2.3 KiB
C++
144 lines
2.3 KiB
C++
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
#include <string>
|
|
#include <map>
|
|
|
|
namespace N_SST
|
|
{
|
|
|
|
|
|
class IntegerCalculator
|
|
{
|
|
public:
|
|
|
|
|
|
|
|
enum E_Error
|
|
{
|
|
|
|
|
|
e_Ok,
|
|
|
|
|
|
e_DivisionByZero,
|
|
|
|
|
|
e_UnknownOperationOrVariable,
|
|
|
|
|
|
e_NoOperandBetweenOperators,
|
|
|
|
|
|
e_InvalidOperand,
|
|
|
|
|
|
e_MissingLeftParanthesis,
|
|
|
|
|
|
e_MissingRightParanthesis,
|
|
|
|
|
|
e_UnknownError
|
|
};
|
|
|
|
|
|
IntegerCalculator();
|
|
virtual ~IntegerCalculator() = default;
|
|
|
|
|
|
|
|
int64_t calc(const std::string& a_calcText, E_Error& a_error, std::string* a_addErrorText = nullptr);
|
|
|
|
|
|
|
|
void addVariable(const std::string& a_name, const int64_t a_value);
|
|
|
|
|
|
|
|
void removeVariable(const std::string& a_name);
|
|
|
|
protected:
|
|
|
|
|
|
|
|
const std::map<std::string, int> m_OperationList;
|
|
|
|
|
|
std::map<std::string, int64_t> m_VariableList;
|
|
|
|
|
|
std::string m_AdditionalErrorText;
|
|
|
|
|
|
int32_t m_ParanthesisCount;
|
|
E_Error m_Error;
|
|
|
|
|
|
|
|
|
|
int64_t calc(const std::string& a_calcText, size_t& a_pos, bool& a_IsEndParanthesis,
|
|
const std::string& a_prevOp = std::string(), const int64_t a_prevValue = 0,
|
|
const std::string& a_initOp = std::string());
|
|
|
|
|
|
|
|
bool calcWithPriority(const std::string& a_calcText, size_t& a_pos, int64_t& a_value1,
|
|
std::string& a_operation, int64_t a_value2, const std::string& a_prevOp);
|
|
|
|
|
|
|
|
static int64_t calcSign(const int64_t a_value, bool& a_isNegative);
|
|
|
|
|
|
|
|
int64_t convertValue(const std::string& a_valueText, size_t& a_pos);
|
|
|
|
|
|
|
|
static std::string getToken(const std::string& a_calcText, size_t& a_pos);
|
|
|
|
|
|
|
|
bool isOperation(const std::string& a_operation) const;
|
|
|
|
|
|
|
|
int64_t doOperation(const int64_t a_value1, const std::string& a_operation, const int64_t a_value2);
|
|
|
|
|
|
|
|
bool isOperationPriorityHigher(const std::string& a_operation1, const std::string& a_operation2) const;
|
|
|
|
|
|
|
|
virtual bool getVariableValue(const std::string& a_variableName, int64_t& a_value) const;
|
|
|
|
|
|
|
|
static bool isDigit(const char a_char, const bool a_isHex = false, const bool a_isBin = false);
|
|
|
|
|
|
|
|
int16_t getPriority(const std::string& a_operation) const;
|
|
|
|
|
|
|
|
static void replace(std::string& a_haystack, const std::string& a_searchText, const std::string& a_replaceText);
|
|
|
|
};
|
|
|
|
}
|