From 2d345153a761187fcb047845688875ed68f1d7f4 Mon Sep 17 00:00:00 2001 From: kuinji Date: Fri, 17 Dec 2021 14:34:45 +0300 Subject: [PATCH] init commit --- .gitignore | 10 +- CMakeLists.txt | 18 +++ build/.keep | 0 include/Enums.h | 38 +++++ include/Expr.h | 33 +++++ include/Lexeme.h | 137 +++++++++++++++++ src/Expr.cpp | 304 +++++++++++++++++++++++++++++++++++++ src/Lexeme.cpp | 379 +++++++++++++++++++++++++++++++++++++++++++++++ src/main.cpp | 47 ++++++ 9 files changed, 962 insertions(+), 4 deletions(-) create mode 100644 CMakeLists.txt create mode 100644 build/.keep create mode 100644 include/Enums.h create mode 100644 include/Expr.h create mode 100644 include/Lexeme.h create mode 100644 src/Expr.cpp create mode 100644 src/Lexeme.cpp create mode 100644 src/main.cpp diff --git a/.gitignore b/.gitignore index 259148f..34b6411 100644 --- a/.gitignore +++ b/.gitignore @@ -1,11 +1,11 @@ -# Prerequisites -*.d - # Compiled Object files *.slo *.lo *.o *.obj +*.dep +*.res +*.resources # Precompiled Headers *.gch @@ -18,7 +18,6 @@ # Fortran module files *.mod -*.smod # Compiled Static libraries *.lai @@ -30,3 +29,6 @@ *.exe *.out *.app + +# Folders +build/ \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..b570c79 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,18 @@ +cmake_minimum_required(VERSION 3.5) + +project(ArithmeticExpression LANGUAGES CXX) + +if(NOT CMAKE_CXX_EXTENSIONS) + set(CMAKE_CXX_EXTENSIONS OFF) +endif() + +include_directories("include/") + +FILE(GLOB SRCS + "*.h" + "*.cpp" + "src/*.cpp" + "include/*.h" +) + +add_executable(${PROJECT_NAME} "src/main.cpp" ${SRCS}) diff --git a/build/.keep b/build/.keep new file mode 100644 index 0000000..e69de29 diff --git a/include/Enums.h b/include/Enums.h new file mode 100644 index 0000000..e0db3de --- /dev/null +++ b/include/Enums.h @@ -0,0 +1,38 @@ +#pragma once +#include +#include +#include + +enum class TypeLexeme +{ + number, + variable, + un_op, + bin_op, + left_scope, + right_scope, + end, + error +}; + +enum class Priority +{ + none, + number, + scope, + low, + mid, + hight +}; + +enum class LexemeState +{ + state_start, + state_variable, + state_un_op, + state_bin_op, + state_left_scope, + state_right_scope, + state_end, + state_error +}; diff --git a/include/Expr.h b/include/Expr.h new file mode 100644 index 0000000..81c9d0a --- /dev/null +++ b/include/Expr.h @@ -0,0 +1,33 @@ +#pragma once +#include +#include "enums.h" +#include "Lexeme.h" + +class Expr +{ +private: + std::string s; + std::vector exprInf; + std::vector reversedP; + std::vector stack; + double res; + + AllFunc allFunc; + + LexemeState state; + int indx; + +public: + Expr(const std::string& _str); + Expr() = default; + + double getRes(); + +private: + Lexeme getNextLexeme(); + void getNextState(const Lexeme lexeme); + void convertStrToInfix(); + void convertInfixToRPN(); + void calculate(); + void doOper(Lexeme lex); +}; diff --git a/include/Lexeme.h b/include/Lexeme.h new file mode 100644 index 0000000..8e33357 --- /dev/null +++ b/include/Lexeme.h @@ -0,0 +1,137 @@ +#pragma once +#include +#include +#include "Enums.h" + +class Lexeme +{ +public: + TypeLexeme type; + std::string value; + Priority prior; +}; + +class Func : public Lexeme +{ +protected: + std::vector allNames; + std::vector possibleState; + int amountOfArgs; + +public: + std::vector getAllNames(); + int getAmountOfArgs(); + std::vector getPossibleState(); + virtual double doOperation(std::vector) = 0; +}; + +class Func0arg : public Func +{ +public: + Func0arg(); +}; + +class Func1arg : public Func +{ +public: + Func1arg(); +}; + +class Func2arg : public Func +{ +public: + Func2arg(); +}; + +class Error : public Func +{ +public: + Error(); + double doOperation(std::vector = {}); +}; + +class AllFunc +{ +public: + std::vector allFunc; + AllFunc(); + + ~AllFunc(); +}; + +class Number : public Func0arg +{ +public: + Number(double val = 0); + double doOperation(std::vector = {}); + +}; + +class LeftScope : public Func0arg { public: LeftScope(); double doOperation(std::vector = {}); }; +class RightScope : public Func0arg { public: RightScope(); double doOperation(std::vector = {}); }; + +class Pi : public Func0arg { public: Pi(); double doOperation(std::vector = {}); }; + +class Exp : public Func0arg { public: Exp(); double doOperation(std::vector = {}); }; + +class Plus : public Func2arg { public: Plus(); double doOperation(std::vector); }; + +class BinMinus : public Func2arg { public: BinMinus(); double doOperation(std::vector); }; + +class Mul : public Func2arg { public: Mul(); double doOperation(std::vector); }; + +class Div : public Func2arg { public: Div(); double doOperation(std::vector); }; + +class Pow : public Func2arg { public: Pow(); double doOperation(std::vector); }; + +class UnMinus : public Func1arg { public: UnMinus(); double doOperation(std::vector); }; + +class Log : public Func1arg { public: Log(); double doOperation(std::vector); }; + +class Lg : public Func1arg { public: Lg(); double doOperation(std::vector); }; + +class Sqrt : public Func1arg { public: Sqrt(); double doOperation(std::vector); }; + +class Cbrt : public Func1arg { public: Cbrt(); double doOperation(std::vector); }; + +class Abs : public Func1arg { public: Abs(); double doOperation(std::vector); }; + +class Sin : public Func1arg { public: Sin(); double doOperation(std::vector); }; + +class Cos : public Func1arg { public: Cos(); double doOperation(std::vector); }; + +class Tan : public Func1arg { public: Tan(); double doOperation(std::vector); }; + +class Cot : public Func1arg { public: Cot(); double doOperation(std::vector); }; + +class Sec : public Func1arg { public: Sec(); double doOperation(std::vector); }; + +class Csc : public Func1arg { public: Csc(); double doOperation(std::vector); }; + +class Asin : public Func1arg { public: Asin(); double doOperation(std::vector); }; + +class Acos : public Func1arg { public: Acos(); double doOperation(std::vector); }; + +class Atan : public Func1arg { public: Atan(); double doOperation(std::vector); }; + +class Acot : public Func1arg { public: Acot(); double doOperation(std::vector); }; + +class Asec : public Func1arg { public: Asec(); double doOperation(std::vector); }; + +class Acsc : public Func1arg { public: Acsc(); double doOperation(std::vector); }; + +class Sinh : public Func1arg { public: Sinh(); double doOperation(std::vector); }; + +class Cosh : public Func1arg { public: Cosh(); double doOperation(std::vector); }; + +class Tanh : public Func1arg { public: Tanh(); double doOperation(std::vector); }; + +class Coth : public Func1arg { public: Coth(); double doOperation(std::vector); }; + +class Asinh : public Func1arg { public: Asinh(); double doOperation(std::vector); }; + +class Acosh : public Func1arg { public: Acosh(); double doOperation(std::vector); }; + +class Atanh : public Func1arg { public: Atanh(); double doOperation(std::vector); }; + +class Acoth : public Func1arg { public: Acoth(); double doOperation(std::vector); }; \ No newline at end of file diff --git a/src/Expr.cpp b/src/Expr.cpp new file mode 100644 index 0000000..bef6352 --- /dev/null +++ b/src/Expr.cpp @@ -0,0 +1,304 @@ +#include "Expr.h" + +double Expr::getRes() { return res; } + +Expr::Expr(const std::string& _str) : s(_str), state(LexemeState::state_start), indx(0) +{ + + try + { + convertStrToInfix(); + convertInfixToRPN(); + calculate(); + } + catch (const char* exception) + { + throw exception; + } +} + +void Expr::convertStrToInfix() +{ + for (; indx < s.size() + 1; indx++) + { + Lexeme lexeme = getNextLexeme(); + getNextState(lexeme); + exprInf.push_back(lexeme); + } + + exprInf.pop_back(); +} + +Lexeme Expr::getNextLexeme() +{ + int maxNameSize = 1; + Lexeme _lex = Error(); + + for (; indx < s.size(); indx++) + { + if (s[indx] == ' ') + continue; + + else + { + int maxNameSize = 0; + + for (auto func : allFunc.allFunc) + { + for (auto name : func->getAllNames()) + { + if (s.find(name, indx) == indx) + { + if (func->type == TypeLexeme::number) + { + _lex = { func->type, std::string(1, s[indx]), func->prior }; + maxNameSize++; + + for (auto digit : func->getAllNames()) + { + if (std::string(1, s[indx + 1]) == digit && indx + 1 < s.size()) + { + indx++; + std::string P = std::string(1, s[indx]); + _lex.value += getNextLexeme().value; + } + } + + return _lex; + } + + + for (int i = 0; i < func->getPossibleState().size(); i++) + { + if ((func->getPossibleState()[i] == state) && (name.size() > maxNameSize)) + { + maxNameSize = name.size(); + _lex = { func->type, func->value, func->prior }; + } + } + } + } + } + indx += maxNameSize - 1; + return _lex; + } + } + + _lex.type = TypeLexeme::end; + return _lex; +} + + +void Expr::getNextState(const Lexeme lexeme) +{ + if (state == LexemeState::state_start) + { + if (lexeme.type == TypeLexeme::un_op) { + state = LexemeState::state_un_op; + } + + else if (lexeme.type == TypeLexeme::variable || lexeme.type == TypeLexeme::number) { + state = LexemeState::state_variable; + } + + else if (lexeme.type == TypeLexeme::left_scope) { + state = LexemeState::state_left_scope; + } + + else if (lexeme.type == TypeLexeme::end) { + state = LexemeState::state_end; + } + + else throw "Error start lexeme"; + } + + else if (state == LexemeState::state_un_op) + { + if (lexeme.type == TypeLexeme::variable || lexeme.type == TypeLexeme::number) { + state = LexemeState::state_variable; + } + + else if (lexeme.type == TypeLexeme::left_scope) { + state = LexemeState::state_left_scope; + } + + else if (lexeme.type == TypeLexeme::un_op) { + state = LexemeState::state_un_op; + } + + else if (lexeme.type == TypeLexeme::left_scope) { + state = LexemeState::state_left_scope; + } + + else throw "Error unary operator lexeme"; + } + + else if (state == LexemeState::state_variable) + { + if (lexeme.type == TypeLexeme::bin_op) { + state = LexemeState::state_bin_op; + } + + else if (lexeme.type == TypeLexeme::right_scope) { + state = LexemeState::state_right_scope; + } + + else if (lexeme.type == TypeLexeme::end) { + state = LexemeState::state_end; + } + + else throw "Error variable lexeme"; + } + + else if (state == LexemeState::state_bin_op) + { + if (lexeme.type == TypeLexeme::variable || lexeme.type == TypeLexeme::number) { + state = LexemeState::state_variable; + } + + else if (lexeme.type == TypeLexeme::left_scope) { + state = LexemeState::state_left_scope; + } + + else if (lexeme.type == TypeLexeme::un_op) { + state = LexemeState::state_un_op; + } + + else throw "Error binary operator lexeme"; + } + + else if (state == LexemeState::state_left_scope) + { + if (lexeme.type == TypeLexeme::left_scope) { + state = LexemeState::state_left_scope; + } + + else if (lexeme.type == TypeLexeme::un_op) { + state = LexemeState::state_un_op; + } + + else if (lexeme.type == TypeLexeme::variable || lexeme.type == TypeLexeme::number) { + state = LexemeState::state_variable; + } + + else throw "Error left scope lexeme"; + } + + else if (state == LexemeState::state_right_scope) + { + if (lexeme.type == TypeLexeme::right_scope) { + state = LexemeState::state_right_scope; + } + + else if (lexeme.type == TypeLexeme::bin_op) { + state = LexemeState::state_bin_op; + } + + else if (lexeme.type == TypeLexeme::end) { + state = LexemeState::state_end; + } + + else throw "Error right scope lexeme"; + } + + else throw "error"; +} + +void Expr::convertInfixToRPN() +{ + for (auto lex : exprInf) + { + if (lex.type == TypeLexeme::number) + reversedP.push_back(lex); + + else if (lex.type == TypeLexeme::variable) + stack.push_back(lex); + + else if (lex.type == TypeLexeme::left_scope) + stack.push_back(lex); + + else if (lex.type == TypeLexeme::right_scope) + { + while (stack.size() > 0 && stack.back().type != TypeLexeme::left_scope) + { + reversedP.push_back(stack.back()); + stack.pop_back(); + } + + if (stack.size() > 0 && stack.back().type == TypeLexeme::left_scope) + { + stack.pop_back(); + } + + else throw "The brackets are not consistent"; + } + + else if (lex.type == TypeLexeme::un_op) + stack.push_back(lex); + + else if (lex.type == TypeLexeme::bin_op) + { + while (!stack.empty() && stack.back().prior >= lex.prior) + { + reversedP.push_back(stack.back()); + stack.pop_back(); + } + stack.push_back(lex); + } + } + + while (stack.size() > 0) + { + Lexeme back = stack.back(); + reversedP.push_back(back); + stack.pop_back(); + if (back.type == TypeLexeme::number || back.type == TypeLexeme::left_scope || back.type == TypeLexeme::right_scope) + throw "The brackets are not consistent"; + } +} + +void Expr::calculate() +{ + for (int i = 0; i < reversedP.size(); i++) + doOper(reversedP[i]); + + std::string srtr = stack.back().value; + res = std::stod(stack.back().value); +} + +void Expr::doOper(Lexeme lex) +{ + for (auto func : allFunc.allFunc) + { + if (lex.type == func->type) + { + for (auto name : func->getAllNames()) + { + if (lex.value.find(name) == 0) + { + if (lex.type == TypeLexeme::number || lex.type == TypeLexeme::variable) + { + stack.push_back(lex); + } + + else + { + std::vector arg = { std::stod(stack.back().value) }; + stack.pop_back(); + + for (int i = func->getAmountOfArgs() - 1; i > 0; i--) + { + arg.push_back(std::stod(stack.back().value)); + stack.pop_back(); + } + + std::reverse(arg.begin(), arg.end()); + stack.push_back(Number(func->doOperation(arg))); + + } + return; + } + } + } + } +} diff --git a/src/Lexeme.cpp b/src/Lexeme.cpp new file mode 100644 index 0000000..e3c82aa --- /dev/null +++ b/src/Lexeme.cpp @@ -0,0 +1,379 @@ +#include +#include "Lexeme.h" + + +const double pi = 3.14159265; +const double e = 2.71828182; + +AllFunc::AllFunc() +{ + std::vector buffAllFunc = + { + new Number, new RightScope, new LeftScope, new Pi, new Exp, new Plus, new BinMinus, new Mul, new Div, new Pow, new UnMinus, + new Log, new Lg, new Sqrt, new Cbrt, new Abs, new Sin, new Cos, new Tan, new Cot, new Cot, new Sec, new Csc, new Asin, new Acos, + new Atan, new Acot, new Asec, new Acsc, new Sinh, new Cosh, new Tanh, new Coth, new Asinh, new Acosh, new Atanh, new Acoth + }; + + allFunc = buffAllFunc; +} +AllFunc::~AllFunc() +{ + for (auto pLex : allFunc) + { + delete pLex; + } +} + +std::vector Func::getAllNames() { return allNames; } +int Func::getAmountOfArgs() { return amountOfArgs; } +std::vector Func::getPossibleState() { return possibleState; } + +Func0arg::Func0arg() +{ + amountOfArgs = 0; + type = TypeLexeme::number; + prior = Priority::hight; + std::vector buffState = {LexemeState::state_start, LexemeState::state_bin_op, LexemeState::state_left_scope, LexemeState::state_un_op }; + possibleState = buffState; +} + + +Func1arg::Func1arg() +{ + amountOfArgs = 1; + type = TypeLexeme::un_op; + prior = Priority::hight; + std::vector buffState = { LexemeState::state_start, LexemeState::state_bin_op, LexemeState::state_left_scope, LexemeState::state_un_op }; + possibleState = buffState; +} + +Func2arg::Func2arg() +{ + amountOfArgs = 2; + type = TypeLexeme::bin_op; + prior = Priority::low; + std::vector buffState = { LexemeState::state_variable, LexemeState::state_right_scope, LexemeState::state_un_op}; + possibleState = buffState; +} + +Error::Error() +{ + amountOfArgs = -1; + type = TypeLexeme::error; + prior = Priority::none; +} +double Error::doOperation(std::vector) { return -1; } + +Number::Number(double val) : Func0arg() +{ + possibleState.push_back(LexemeState::state_variable); + std::vector buffName = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "0", ".", "," }; + allNames = buffName; + value = std::to_string(val); +} +double Number::doOperation(std::vector arg) { return std::stod(value); } + +LeftScope::LeftScope() : Func0arg() +{ + possibleState.push_back(LexemeState::state_right_scope); + type = TypeLexeme::left_scope; + prior = Priority::scope; + allNames.push_back("("); + value = allNames[0]; +} +double LeftScope::doOperation(std::vector) { return -1; } + +RightScope::RightScope() : Func0arg() +{ + possibleState.push_back(LexemeState::state_right_scope); + possibleState.push_back(LexemeState::state_variable); + type = TypeLexeme::right_scope; + prior = Priority::scope; + allNames.push_back(")"); + value = allNames[0]; +} +double RightScope::doOperation(std::vector) { return -1; } + +Pi::Pi(): Func0arg() +{ + type = TypeLexeme::variable; + allNames.push_back(std::to_string(pi)); + allNames.push_back("pi"); + allNames.push_back("Pi"); + value = allNames[0]; +} +double Pi::doOperation(std::vector arg) { return std::stod(value); } + +Exp::Exp() : Func0arg() +{ + type = TypeLexeme::variable; + allNames.push_back(std::to_string(e)); + allNames.push_back("exp"); + allNames.push_back("e"); + value = allNames[0]; +} +double Exp::doOperation(std::vector arg) { return std::stod(value); } + +Plus::Plus() : Func2arg() +{ + possibleState.push_back(LexemeState::state_right_scope); + allNames.push_back("+"); + value = allNames[0]; +} +double Plus::doOperation(std::vector arg){ return arg[0] + arg[1]; } + +BinMinus::BinMinus() : Func2arg() +{ + std::vector buffAllNames = { "-" , "–" , "—" }; + allNames.push_back("-"); + allNames.push_back("–"); + allNames.push_back("—"); + value = allNames[0]; +} +double BinMinus::doOperation(std::vector arg) { return arg[0] - arg[1]; } + +Mul::Mul() : Func2arg() +{ + prior = Priority::mid; + allNames.push_back("*"); + value = allNames[0]; +} +double Mul::doOperation(std::vector arg) { return arg[0] * arg[1]; } + +Div::Div() : Func2arg() +{ + prior = Priority::mid; + allNames.push_back("/"); + value = allNames[0]; +} +double Div::doOperation(std::vector arg) { return arg[0] / arg[1]; } + +Pow::Pow() : Func2arg() +{ + prior = Priority::mid; + allNames.push_back("^"); + allNames.push_back("**"); + value = allNames[0]; +} +double Pow::doOperation(std::vector arg) { return std::pow(arg[0], arg[1]); } + +UnMinus::UnMinus() : Func1arg() +{ + allNames.push_back("-"); + allNames.push_back("–"); + allNames.push_back("—"); + value = allNames[0]; +} +double UnMinus::doOperation(std::vector arg) { return -arg[0]; } + + +Log::Log() : Func1arg() +{ + allNames.push_back("log"); + allNames.push_back("ln"); + value = allNames[0]; +} +double Log::doOperation(std::vector arg) { return std::log(arg[0]); } + +Lg::Lg() : Func1arg() +{ + allNames.push_back("lg"); + value = allNames[0]; +} +double Lg::doOperation(std::vector arg) { return std::log10(arg[0]); } + +Sqrt::Sqrt() : Func1arg() +{ + allNames.push_back("sqrt"); + value = allNames[0]; +} +double Sqrt::doOperation(std::vector arg) { return std::sqrt(arg[0]); } + +Cbrt::Cbrt() : Func1arg() +{ + allNames.push_back("cbrt"); + value = allNames[0]; +} +double Cbrt::doOperation(std::vector arg) { return std::cbrt(arg[0]); } + +Sin::Sin() : Func1arg() +{ + allNames.push_back("sin"); + value = allNames[0]; +} +double Sin::doOperation(std::vector arg) { return std::sin(arg[0]); } + + +Cos::Cos() : Func1arg() +{ + allNames.push_back("cos"); + value = allNames[0]; +} +double Cos::doOperation(std::vector arg) { return std::cos(arg[0]); } + + +Abs::Abs() : Func1arg() +{ + allNames.push_back("abs"); + value = allNames[0]; +} +double Abs::doOperation(std::vector arg) { return std::abs(arg[0]); } + + +Tan::Tan() : Func1arg() +{ + allNames.push_back("tan"); + allNames.push_back("tg"); + value = allNames[0]; +} +double Tan::doOperation(std::vector arg) { return std::tan(arg[0]); } + +Cot::Cot() : Func1arg() +{ + allNames.push_back("cot"); + allNames.push_back("ctg"); + value = allNames[0]; +} +double Cot::doOperation(std::vector arg) { return 1 / std::tan(arg[0]); } + +Sec::Sec() : Func1arg() +{ + allNames.push_back("sec"); + value = allNames[0]; +} +double Sec::doOperation(std::vector arg) { return 1 / std::cos(arg[0]); } + +Csc::Csc() : Func1arg() +{ + allNames.push_back("csc"); + value = allNames[0]; +} +double Csc::doOperation(std::vector arg) { return 1 / std::sin(arg[0]); } + +Asin::Asin() : Func1arg() +{ + allNames.push_back("asin"); + allNames.push_back("arcsin"); + value = allNames[0]; +} +double Asin::doOperation(std::vector arg) { return std::asin(arg[0]); } + +Acos::Acos() : Func1arg() +{; + allNames.push_back("acos"); + allNames.push_back("arccos"); + value = allNames[0]; +} +double Acos::doOperation(std::vector arg) { return std::acos(arg[0]); } + +Atan::Atan() : Func1arg() +{ + allNames.push_back("atan"); + allNames.push_back("arctan"); + allNames.push_back("atg"); + allNames.push_back("arctg"); + value = allNames[0]; +} +double Atan::doOperation(std::vector arg) { return std::atan(arg[0]); } + +Acot::Acot() : Func1arg() +{ + allNames.push_back("acot"); + allNames.push_back("arccot"); + allNames.push_back("actg"); + allNames.push_back("arcctg"); + value = allNames[0]; +} +double Acot::doOperation(std::vector arg) { return std::atan(1 / arg[0]); } + +Asec::Asec() : Func1arg() +{ + allNames.push_back("asec"); + allNames.push_back("arsec"); + allNames.push_back("arcsec"); + value = allNames[0]; +} +double Asec::doOperation(std::vector arg) { return std::acos(1 / arg[0]); } + +Acsc::Acsc() : Func1arg() +{ + allNames.push_back("acsc"); + allNames.push_back("arcsc"); + allNames.push_back("arccsc"); + value = allNames[0]; +} +double Acsc::doOperation(std::vector arg) { return std::asin(1 / arg[0]); } + +Sinh::Sinh() : Func1arg() +{ + allNames.push_back("sinh"); + allNames.push_back("sh"); + value = allNames[0]; +} +double Sinh::doOperation(std::vector arg) { return std::sinh(arg[0]); } + +Cosh::Cosh() : Func1arg() +{ + allNames.push_back("cosh"); + allNames.push_back("ch"); + value = allNames[0]; +} +double Cosh::doOperation(std::vector arg) { return std::cosh(arg[0]); } + + +Tanh::Tanh() : Func1arg() +{ + allNames.push_back("tanh"); + allNames.push_back("th"); + value = allNames[0]; +} +double Tanh::doOperation(std::vector arg) { return std::tanh(arg[0]); } + +Coth::Coth() : Func1arg() +{ + allNames.push_back("coth"); + allNames.push_back("cth"); + value = allNames[0]; +} +double Coth::doOperation(std::vector arg) { return 1 / std::tanh(arg[0]); } + +Asinh::Asinh() : Func1arg() +{ + allNames.push_back("asinh"); + allNames.push_back("arcsinh"); + allNames.push_back("arsinh"); + allNames.push_back("arsh"); + allNames.push_back("arcsh"); + value = allNames[0]; +} +double Asinh::doOperation(std::vector arg) { return std::asinh(1 / arg[0]); } + +Acosh::Acosh() : Func1arg() +{ + allNames.push_back("acosh"); + allNames.push_back("arccosh"); + allNames.push_back("arcch"); + allNames.push_back("arch"); + allNames.push_back("arcch"); + value = allNames[0]; +} +double Acosh::doOperation(std::vector arg) { return std::acosh(1 / arg[0]); } + +Atanh::Atanh() : Func1arg() +{ + allNames.push_back("atanh"); + allNames.push_back("arth"); + allNames.push_back("arcth"); + value = allNames[0]; +} +double Atanh::doOperation(std::vector arg) { return std::atanh(1 / arg[0]); } + +Acoth::Acoth() : Func1arg() +{ + allNames.push_back("acoth"); + allNames.push_back("arcth"); + allNames.push_back("arccth"); + value = allNames[0]; +} +double Acoth::doOperation(std::vector arg) { return std::atanh(1 / arg[0]); } + diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..0b11ae7 --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,47 @@ +#include +#include +#include +#include + +#include "Enums.h" +#include "Expr.h" +#include "Lexeme.h" + +bool checkExpr(const std::string& expr) +{ + try + { + Expr exprL(expr); + } + catch (const char* exception) + { + return false; + } + + return true; +} + +int main() +{ + std::string exprStr; + + std::cout << "Enter the expression:" << std::endl << std::endl; + std::cin >> exprStr; + + Expr expr; + + //if (checkExpr(exprStr)) std::cout << "OK"; + //else std::cout << "NOT OK"; + + try + { + Expr expr(exprStr); + std::cout << " =" << std::endl << expr.getRes() << std::endl; + } + catch (const char* exception) + { + std::cerr << exception; + } + + return 0; +} \ No newline at end of file