From 8c1fd7bba01f372def9e1d3023058acbff4d804d Mon Sep 17 00:00:00 2001 From: superroket169 Date: Thu, 22 Jan 2026 19:24:55 +0300 Subject: [PATCH 1/2] feat : added math parser for plott mathemathical funcs --- src/mathfunc.cpp | 130 +++++++++++++++++++++++++++++++++++++++++++++++ src/mathfunc.h | 56 ++++++++++++++++++++ 2 files changed, 186 insertions(+) create mode 100644 src/mathfunc.cpp create mode 100644 src/mathfunc.h diff --git a/src/mathfunc.cpp b/src/mathfunc.cpp new file mode 100644 index 0000000..bdb53ba --- /dev/null +++ b/src/mathfunc.cpp @@ -0,0 +1,130 @@ +#include "mathfunc.h" +#include +#include + +#include + +#define inf 1e9 + +// helpers: +bool isValidString(const std::string& str) +{ + return true; + std::string validChars = "()0123456789+-/*x"; + std::string opChars = "()+-/*"; + int balance = 0; + bool isBeforOp = false; + + //if (opChars.find(str[0]) == std::string::npos || opChars.find(str[str.size() - 1]) == std::string::npos) { std::cout << "try2:1"; return false;} + for (char c : str) + { + if (validChars.find(c) == std::string::npos) { std::cout << "try2:2"; return false;} + if (c == '(') balance++; + if (c == ')') balance--; + if (balance < 0) { std::cout << "try2:3"; return false;}; + + if (opChars.find(c) != std::string::npos) + { + if(isBeforOp) { std::cout << "try2:4"; return false;} + isBeforOp = true; + } + } + return balance == 0; +} + +std::vector getPriVec(std::string str) +{ + std::vector vec(str.size(), inf); + int parVal = 0; + for(int i = 0; i < vec.size(); ++i) + { + if(str[i] == '(') { parVal++; continue; } + if(str[i] == ')') { parVal--; continue; } + + if(str[i] == '+' || str[i] == '-') vec[i] = parVal + 1; + else if(str[i] == '*' || str[i] == '/') vec[i] = parVal + 2; + } + + return vec; +} + +bool isOp(char c) +{ + std::string ops = "/*-+"; + return ops.find(c) != std::string::npos; +} + +Operation toOp(char c) +{ + switch (c) + { + case '+' : return Operation::Add; + case '-' : return Operation::Subtraction; + case '*' : return Operation::Multiple; + case '/' : return Operation::Divide; + default : return Operation::None; + } +} + +size_t getMinInd(std::vector vec) +{ + size_t index = 0; + for(int i = 0; i < vec.size(); ++i) + if(vec[index] >= vec[i]) index = i; + + return index; +} + +// build tree : +void Node::buildTree(std::string str) +{ + static int a = 0;a++; + int b = 0; + std::cout << "not ex1 " << a < priVec = getPriVec(str); + size_t minInd = getMinInd(priVec); + + if(isOp(str[minInd])) + { + std::cout << "not ex2 "< 1 && str.front() == '(' && str.back() == ')') + str = str.substr(1, str.size() - 2); + + *value = std::stod(str); + } + } +} + +void MathFunction::translateMath() +{ + if (isValidString(funcStr) == false) exit(1); + node.buildTree(funcStr); + return; +} + +double Node::foundSearch(double x) +{ + if(isValueX) return x; + if(value != nullptr) return *value; + + switch (*op) + { + case Operation::Add : return left->foundSearch(x) + right->foundSearch(x); + case Operation::Subtraction : return left->foundSearch(x) - right->foundSearch(x); + case Operation::Divide : return left->foundSearch(x) / right->foundSearch(x); + case Operation::Multiple : return left->foundSearch(x) * right->foundSearch(x); + default : return 0; + } +} \ No newline at end of file diff --git a/src/mathfunc.h b/src/mathfunc.h new file mode 100644 index 0000000..ccc8170 --- /dev/null +++ b/src/mathfunc.h @@ -0,0 +1,56 @@ +#ifndef MATHFUNC_H +#define MATHFUNC_H + +#include + +enum class Operation { Multiple, Divide, Add, Subtraction, None }; + +class Node +{ +private: + Node* left = nullptr; + Node* right = nullptr; + + double *value = nullptr; + Operation *op = nullptr; + + bool isValueX = false; + +public: + Node() = default; + + Node(std::string str) { buildTree(str); } + ~Node() + { + delete left; + delete right; + delete op; + delete value; + } + + void buildTree(std::string str); + + double foundSearch(double x); +}; // Class Node + +class MathFunction +{ +private: + std::string funcStr; + Node node; + +public: + MathFunction(std::string str) + : funcStr(str) + { + translateMath(); + } + + // string to math helpers: + void translateMath(); + + // cpp function + double func(double x) { return node.foundSearch(x); } +}; + +#endif \ No newline at end of file From c40d9f9ca3278f1b50b8fe6b9ac7273233bc22d7 Mon Sep 17 00:00:00 2001 From: superroket169 Date: Thu, 22 Jan 2026 19:26:44 +0300 Subject: [PATCH 2/2] docs : added cmake --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 4e1bea2..dda2713 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,7 +12,7 @@ FetchContent_Declare(SFML SYSTEM) FetchContent_MakeAvailable(SFML) -add_executable(main src/main.cpp) +add_executable(main src/main.cpp src/mathfunc.cpp) target_compile_features(main PRIVATE cxx_std_17) target_link_libraries(main PRIVATE SFML::Graphics)