From a9d63d8cd314fa88d69faceeafa442e9c5ba4f8d Mon Sep 17 00:00:00 2001 From: Jarek Jeda Date: Tue, 12 Aug 2025 19:18:15 +0200 Subject: [PATCH 001/104] advancedCalculator.cpp --- homework/advanced-calculator/advancedCalculator.cpp | 0 homework/advanced-calculator/advancedCalculator.hpp | 0 2 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 homework/advanced-calculator/advancedCalculator.cpp create mode 100644 homework/advanced-calculator/advancedCalculator.hpp diff --git a/homework/advanced-calculator/advancedCalculator.cpp b/homework/advanced-calculator/advancedCalculator.cpp new file mode 100644 index 000000000..e69de29bb diff --git a/homework/advanced-calculator/advancedCalculator.hpp b/homework/advanced-calculator/advancedCalculator.hpp new file mode 100644 index 000000000..e69de29bb From 3fb0d9caf57e077490a1fff7e92ac09937c9211b Mon Sep 17 00:00:00 2001 From: Jarek Jeda Date: Wed, 13 Aug 2025 13:30:01 +0200 Subject: [PATCH 002/104] enums, classes --- .../advancedCalculator.cpp | 46 +++++++++++++++++++ .../advancedCalculator.hpp | 27 +++++++++++ homework/advanced-calculator/main.cpp | 6 +++ homework/advanced-calculator/test.cpp | 4 +- 4 files changed, 81 insertions(+), 2 deletions(-) create mode 100644 homework/advanced-calculator/main.cpp diff --git a/homework/advanced-calculator/advancedCalculator.cpp b/homework/advanced-calculator/advancedCalculator.cpp index e69de29bb..76d3f829b 100644 --- a/homework/advanced-calculator/advancedCalculator.cpp +++ b/homework/advanced-calculator/advancedCalculator.cpp @@ -0,0 +1,46 @@ +#include "advancedCalculator.hpp" + +#include + +std::string errorCodeToString(ErrorCode code) { + switch (code) { + case ErrorCode::Ok: + return "Brak błędu."; + case ErrorCode::BadCharacter: + return "Nieprawidłowy znak — dozwolone są tylko liczby."; + case ErrorCode::BadFormat: + return "Zły format komendy."; + case ErrorCode::DividedBy0: + return "Błąd: dzielenie przez zero."; + case ErrorCode::SqrtOfNegativeNumber: + return "Błąd: pierwiastek z liczby ujemnej."; + case ErrorCode::ModuleOfNonIntegerValue: + return "Błąd: operacja % wymaga liczb całkowitych."; + default: + return "Nieznany błąd."; + } +} + +AdvancedCalculator::AdvancedCalculator() { + operations['+'] = [](double a, double b) { return Result{ a + b }; }; + operations['-'] = [](double a, double b) { return Result{ a - b }; }; + operations['*'] = [](double a, double b) { return Result{ a * b }; }; + operations['/'] = [](double a, double b) { + if (b == 0.0) { + return Result{ErrorCode::DividedBy0}; + } + return Result{a / b}; + }; + operations['%'] = [](double a, double b) { + if (static_cast(a) != a || static_cast(b) != b) { + return Result{ErrorCode::ModuleOfNonIntegerValue}; + } else if (b == 0.0) { + return Result{ErrorCode::DividedBy0}; + } + return Result{ + static_cast(a) % static_cast(b)}; + }; + + + + }; \ No newline at end of file diff --git a/homework/advanced-calculator/advancedCalculator.hpp b/homework/advanced-calculator/advancedCalculator.hpp index e69de29bb..a83a0d416 100644 --- a/homework/advanced-calculator/advancedCalculator.hpp +++ b/homework/advanced-calculator/advancedCalculator.hpp @@ -0,0 +1,27 @@ +#include +#include +#include +#include + +enum class ErrorCode { + Ok, + BadCharacter, + BadFormat, + DividedBy0, + SqrtOfNegativeNumber, + ModuleOfNonIntegerValue +}; + +std::string errorCodeToString(ErrorCode code); + +class AdvancedCalculator { +public: + using Result = std::variant; + + AdvancedCalculator(); + Result calculate(char op, double a, double b = 0) const; + +private: + std::map> operations; + +}; \ No newline at end of file diff --git a/homework/advanced-calculator/main.cpp b/homework/advanced-calculator/main.cpp new file mode 100644 index 000000000..1cb17f621 --- /dev/null +++ b/homework/advanced-calculator/main.cpp @@ -0,0 +1,6 @@ +#include + +int main() { + + return 0; +} \ No newline at end of file diff --git a/homework/advanced-calculator/test.cpp b/homework/advanced-calculator/test.cpp index a3e0d862a..00714a005 100644 --- a/homework/advanced-calculator/test.cpp +++ b/homework/advanced-calculator/test.cpp @@ -12,13 +12,13 @@ bool cmp(double first, double second, double epsilon = 0.5) { TEST(advancedCalculatorTest, ShouldAdd) { double result = 0; - ASSERT_EQ(process("5+ 11", &result), ErrorCode::OK); + ASSERT_EQ(process("5+11", &result), ErrorCode::OK); EXPECT_TRUE(cmp(result, 16)); ASSERT_EQ(process("43.21+11.54", &result), ErrorCode::OK); EXPECT_TRUE(cmp(result, 54.75)); ASSERT_EQ(process("-54.31 + 11", &result), ErrorCode::OK); EXPECT_TRUE(cmp(result, -43.31)); - ASSERT_EQ(process("28.43 +-810.43", &result), ErrorCode::OK); + ASSERT_EQ(process("28.43+ -810.43", &result), ErrorCode::OK); EXPECT_TRUE(cmp(result, -782)); ASSERT_EQ(process("-11.230 + -77.321", &result), ErrorCode::OK); EXPECT_TRUE(cmp(result, -88.551)); From 1e02cdca5eb1e21b4fe04e09c52771e393a5ffa9 Mon Sep 17 00:00:00 2001 From: Jarek Jeda Date: Wed, 13 Aug 2025 15:27:46 +0200 Subject: [PATCH 003/104] operations_implementations --- .../advancedCalculator.cpp | 33 +++++++++++++++---- .../advancedCalculator.hpp | 2 +- 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/homework/advanced-calculator/advancedCalculator.cpp b/homework/advanced-calculator/advancedCalculator.cpp index 76d3f829b..47e08639f 100644 --- a/homework/advanced-calculator/advancedCalculator.cpp +++ b/homework/advanced-calculator/advancedCalculator.cpp @@ -10,7 +10,7 @@ std::string errorCodeToString(ErrorCode code) { return "Nieprawidłowy znak — dozwolone są tylko liczby."; case ErrorCode::BadFormat: return "Zły format komendy."; - case ErrorCode::DividedBy0: + case ErrorCode::DivideBy0: return "Błąd: dzielenie przez zero."; case ErrorCode::SqrtOfNegativeNumber: return "Błąd: pierwiastek z liczby ujemnej."; @@ -27,20 +27,41 @@ AdvancedCalculator::AdvancedCalculator() { operations['*'] = [](double a, double b) { return Result{ a * b }; }; operations['/'] = [](double a, double b) { if (b == 0.0) { - return Result{ErrorCode::DividedBy0}; + return Result{ErrorCode::DivideBy0}; } return Result{a / b}; }; operations['%'] = [](double a, double b) { - if (static_cast(a) != a || static_cast(b) != b) { + if (b == 0.0) { + return Result{ErrorCode::DivideBy0}; + } else if (static_cast(a) != a || static_cast(b) != b) { return Result{ErrorCode::ModuleOfNonIntegerValue}; - } else if (b == 0.0) { - return Result{ErrorCode::DividedBy0}; } return Result{ static_cast(a) % static_cast(b)}; }; + operations['^'] = [](double a, double b) { return Result{std::pow(a, b)}; }; + operations['$'] = [](double a, double b) { + if (b == 0.0) { + return Result{ErrorCode::DivideBy0}; + } else if (a < 0 && static_cast(b) % 2 == 0) { + return Result{ErrorCode::SqrtOfNegativeNumber}; + } + return Result{ + std::pow(a, 1.0 / b)}; + }; + operations['!'] = [](double a, double) { + return Result{ + std::tgamma(a + 1)}; + }; +} +AdvancedCalculator::Result AdvancedCalculator::calculate(char op, double a, double b) const { + auto it = operations.find(op); + if (it != operations.end()) { + return it->second(a, b); + } + return Result{ErrorCode::BadCharacter}; +} - }; \ No newline at end of file diff --git a/homework/advanced-calculator/advancedCalculator.hpp b/homework/advanced-calculator/advancedCalculator.hpp index a83a0d416..be5ce69a6 100644 --- a/homework/advanced-calculator/advancedCalculator.hpp +++ b/homework/advanced-calculator/advancedCalculator.hpp @@ -7,7 +7,7 @@ enum class ErrorCode { Ok, BadCharacter, BadFormat, - DividedBy0, + DivideBy0, SqrtOfNegativeNumber, ModuleOfNonIntegerValue }; From c4f018f9c4172dc7895630baf854b448a00dd57b Mon Sep 17 00:00:00 2001 From: Jarek Jeda Date: Wed, 13 Aug 2025 16:51:33 +0200 Subject: [PATCH 004/104] operations fixes --- .../advancedCalculator.cpp | 59 +++++++++++-------- .../advancedCalculator.hpp | 6 +- homework/advanced-calculator/main.cpp | 8 +++ homework/advanced-calculator/test.cpp | 2 +- 4 files changed, 47 insertions(+), 28 deletions(-) diff --git a/homework/advanced-calculator/advancedCalculator.cpp b/homework/advanced-calculator/advancedCalculator.cpp index 47e08639f..60d3f13f6 100644 --- a/homework/advanced-calculator/advancedCalculator.cpp +++ b/homework/advanced-calculator/advancedCalculator.cpp @@ -1,6 +1,6 @@ -#include "advancedCalculator.hpp" +#include -#include +#include "advancedCalculator.hpp" std::string errorCodeToString(ErrorCode code) { switch (code) { @@ -22,41 +22,54 @@ std::string errorCodeToString(ErrorCode code) { } AdvancedCalculator::AdvancedCalculator() { - operations['+'] = [](double a, double b) { return Result{ a + b }; }; - operations['-'] = [](double a, double b) { return Result{ a - b }; }; - operations['*'] = [](double a, double b) { return Result{ a * b }; }; - operations['/'] = [](double a, double b) { + operations['+'] = [](double a, double b, double* out) { + *out = a + b; + return ErrorCode::Ok; + }; + operations['-'] = [](double a, double b, double *out) { + *out = a - b; + return ErrorCode::Ok; + }; + operations['*'] = [](double a, double b, double *out) { + *out = a * b; + return ErrorCode::Ok; + }; + operations['/'] = [](double a, double b, double* out) { if (b == 0.0) { - return Result{ErrorCode::DivideBy0}; + return ErrorCode::DivideBy0; } - return Result{a / b}; + *out = a / b; + return ErrorCode::Ok; }; - operations['%'] = [](double a, double b) { + operations['%'] = [](double a, double b, double* out) { if (b == 0.0) { - return Result{ErrorCode::DivideBy0}; + return ErrorCode::DivideBy0; } else if (static_cast(a) != a || static_cast(b) != b) { - return Result{ErrorCode::ModuleOfNonIntegerValue}; + return ErrorCode::ModuleOfNonIntegerValue; } - return Result{ - static_cast(a) % static_cast(b)}; + *out = static_cast(a) % static_cast(b); + return ErrorCode::Ok; }; - operations['^'] = [](double a, double b) { return Result{std::pow(a, b)}; }; - operations['$'] = [](double a, double b) { + operations['^'] = [](double a, double b, double* out) { + *out = std::pow(a, b); + return ErrorCode::Ok; + }; + operations['$'] = [](double a, double b, double* out) { if (b == 0.0) { - return Result{ErrorCode::DivideBy0}; + return ErrorCode::DivideBy0; } else if (a < 0 && static_cast(b) % 2 == 0) { - return Result{ErrorCode::SqrtOfNegativeNumber}; + return ErrorCode::SqrtOfNegativeNumber; } - return Result{ - std::pow(a, 1.0 / b)}; + *out = std::pow(a, 1.0 / b); + return ErrorCode::Ok; }; - operations['!'] = [](double a, double) { - return Result{ - std::tgamma(a + 1)}; + operations['!'] = [](double a, double, double* out) { + *out = std::tgamma(a + 1); + return ErrorCode::Ok; }; } -AdvancedCalculator::Result AdvancedCalculator::calculate(char op, double a, double b) const { +ErrorCode AdvancedCalculator::process(const std::string& input, double* out) const { auto it = operations.find(op); if (it != operations.end()) { return it->second(a, b); diff --git a/homework/advanced-calculator/advancedCalculator.hpp b/homework/advanced-calculator/advancedCalculator.hpp index be5ce69a6..932fd6d81 100644 --- a/homework/advanced-calculator/advancedCalculator.hpp +++ b/homework/advanced-calculator/advancedCalculator.hpp @@ -16,12 +16,10 @@ std::string errorCodeToString(ErrorCode code); class AdvancedCalculator { public: - using Result = std::variant; - AdvancedCalculator(); - Result calculate(char op, double a, double b = 0) const; + ErrorCode process(const std::string& input, double* out) const; private: - std::map> operations; + std::map> operations; }; \ No newline at end of file diff --git a/homework/advanced-calculator/main.cpp b/homework/advanced-calculator/main.cpp index 1cb17f621..1ec559ee9 100644 --- a/homework/advanced-calculator/main.cpp +++ b/homework/advanced-calculator/main.cpp @@ -1,6 +1,14 @@ #include +#include "advancedCalculator.hpp" + int main() { + AdvancedCalculator calc; + double result = 0; + + if (calc.process("123 / 0", &result) == ErrorCode::DivideBy0) { + + } return 0; } \ No newline at end of file diff --git a/homework/advanced-calculator/test.cpp b/homework/advanced-calculator/test.cpp index 00714a005..66c11b188 100644 --- a/homework/advanced-calculator/test.cpp +++ b/homework/advanced-calculator/test.cpp @@ -170,7 +170,7 @@ TEST(advancedCalculatorTest, ShouldReturnDivideBy0) { ASSERT_EQ(process("123 / 0", &result), ErrorCode::DivideBy0); ASSERT_EQ(process("123 / 0.0", &result), ErrorCode::DivideBy0); - ASSERT_EQ(process("123 / -0", &result), ErrorCode::DivideBy0); + ASSERT_EQ(process("123 / -0", &result), ErrorCode::DivideBy0); ASSERT_EQ(process("123 / -0.0", &result), ErrorCode::DivideBy0); ASSERT_EQ(process("-123 / -0.0", &result), ErrorCode::DivideBy0); ASSERT_EQ(process("0.0 / 0", &result), ErrorCode::DivideBy0); From cd01bb15da9cad08bf3ef4cfa601ef12000a7b3b Mon Sep 17 00:00:00 2001 From: Jarek Jeda Date: Wed, 13 Aug 2025 17:30:20 +0200 Subject: [PATCH 005/104] main.cpp --- homework/advanced-calculator/README.md | 4 +- .../advancedCalculator.cpp | 67 ++++++++++++------- .../advancedCalculator.hpp | 2 +- homework/advanced-calculator/main.cpp | 18 ++++- 4 files changed, 59 insertions(+), 32 deletions(-) diff --git a/homework/advanced-calculator/README.md b/homework/advanced-calculator/README.md index e04976fd2..a7d448f5e 100644 --- a/homework/advanced-calculator/README.md +++ b/homework/advanced-calculator/README.md @@ -24,7 +24,7 @@ ___ ### Error code -* `Ok` +* `OK` * `BadCharacter` - znak inny niż liczba * `BadFormat` - zły format komendy np. + 5 4, powinno być 4 + 5 * `DivideBy0` - dzielenie przez 0 @@ -40,7 +40,7 @@ ErrorCode process(std::string input, double* out) ``` * Funkcja ta powinna przyjmować dane od użytkownika oraz dokonywać odpowiedniej kalkulacji -* Jeżeli dane są poprawne, ma zwrócić `ErrorCode:Ok`, a w zmiennej `out` ma zapisać wynik +* Jeżeli dane są poprawne, ma zwrócić `ErrorCode:OK`, a w zmiennej `out` ma zapisać wynik * Jeżeli wystąpi któryś z błędów, funkcja ma go zwrócić, a w `out` ma nic nie zapisywać ___ diff --git a/homework/advanced-calculator/advancedCalculator.cpp b/homework/advanced-calculator/advancedCalculator.cpp index 60d3f13f6..057ad7979 100644 --- a/homework/advanced-calculator/advancedCalculator.cpp +++ b/homework/advanced-calculator/advancedCalculator.cpp @@ -1,10 +1,11 @@ #include +#include #include "advancedCalculator.hpp" std::string errorCodeToString(ErrorCode code) { switch (code) { - case ErrorCode::Ok: + case ErrorCode::OK: return "Brak błędu."; case ErrorCode::BadCharacter: return "Nieprawidłowy znak — dozwolone są tylko liczby."; @@ -24,57 +25,71 @@ std::string errorCodeToString(ErrorCode code) { AdvancedCalculator::AdvancedCalculator() { operations['+'] = [](double a, double b, double* out) { *out = a + b; - return ErrorCode::Ok; - }; - operations['-'] = [](double a, double b, double *out) { + return ErrorCode::OK; + }; + operations['-'] = [](double a, double b, double* out) { *out = a - b; - return ErrorCode::Ok; - }; - operations['*'] = [](double a, double b, double *out) { + return ErrorCode::OK; + }; + operations['*'] = [](double a, double b, double* out) { *out = a * b; - return ErrorCode::Ok; - }; + return ErrorCode::OK; + }; operations['/'] = [](double a, double b, double* out) { if (b == 0.0) { return ErrorCode::DivideBy0; } - *out = a / b; - return ErrorCode::Ok; - }; + *out = a / b; + return ErrorCode::OK; + }; operations['%'] = [](double a, double b, double* out) { if (b == 0.0) { return ErrorCode::DivideBy0; - } else if (static_cast(a) != a || static_cast(b) != b) { + } + else if (static_cast(a) != a || static_cast(b) != b) { return ErrorCode::ModuleOfNonIntegerValue; } *out = static_cast(a) % static_cast(b); - return ErrorCode::Ok; - }; - operations['^'] = [](double a, double b, double* out) { + return ErrorCode::OK; + }; + operations['^'] = [](double a, double b, double* out) { *out = std::pow(a, b); - return ErrorCode::Ok; + return ErrorCode::OK; }; operations['$'] = [](double a, double b, double* out) { if (b == 0.0) { return ErrorCode::DivideBy0; - } else if (a < 0 && static_cast(b) % 2 == 0) { + } + else if (a < 0 && static_cast(b) % 2 == 0) { return ErrorCode::SqrtOfNegativeNumber; } *out = std::pow(a, 1.0 / b); - return ErrorCode::Ok; - }; + return ErrorCode::OK; + }; operations['!'] = [](double a, double, double* out) { *out = std::tgamma(a + 1); - return ErrorCode::Ok; - }; + return ErrorCode::OK; + }; } ErrorCode AdvancedCalculator::process(const std::string& input, double* out) const { - auto it = operations.find(op); - if (it != operations.end()) { - return it->second(a, b); + std::istringstream iss(input); + double a = 0; + double b = 0; + char op = 0; + + iss >> a >> op; + if (!iss || operations.find(op) == operations.end()) { + return ErrorCode::BadCharacter; + } + + if (op != '!') { + iss >> b; + if (!iss) { + return ErrorCode::BadFormat; + } } - return Result{ErrorCode::BadCharacter}; + return operations.at(op)(a, b, out); } diff --git a/homework/advanced-calculator/advancedCalculator.hpp b/homework/advanced-calculator/advancedCalculator.hpp index 932fd6d81..47470ab12 100644 --- a/homework/advanced-calculator/advancedCalculator.hpp +++ b/homework/advanced-calculator/advancedCalculator.hpp @@ -4,7 +4,7 @@ #include enum class ErrorCode { - Ok, + OK, BadCharacter, BadFormat, DivideBy0, diff --git a/homework/advanced-calculator/main.cpp b/homework/advanced-calculator/main.cpp index 1ec559ee9..a84d97d21 100644 --- a/homework/advanced-calculator/main.cpp +++ b/homework/advanced-calculator/main.cpp @@ -4,11 +4,23 @@ int main() { AdvancedCalculator calc; + std::string input; double result = 0; - if (calc.process("123 / 0", &result) == ErrorCode::DivideBy0) { - - } + std::cout << "Advanced calculator (quit or exit to close the program)\n"; + while (true) { + std::cout << "> "; + std::getline(std::cin, input); + if (input == "exit" || input == "quit") { + break; + } + ErrorCode code = calc.process(input, &result); + if (code == ErrorCode::OK) { + std::cout << "resut: " << result << "\n"; + } else { + std::cout << errorCodeToString(code) << "\n"; + } + } return 0; } \ No newline at end of file From f54df1ed5da1cac9bb6d21521915da7a922c8b09 Mon Sep 17 00:00:00 2001 From: Jarek Jeda Date: Wed, 13 Aug 2025 18:18:46 +0200 Subject: [PATCH 006/104] last --- homework/advanced-calculator/.clang-format | 9 ++++ homework/advanced-calculator/.gitignore | 51 ++++++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 homework/advanced-calculator/.clang-format create mode 100644 homework/advanced-calculator/.gitignore diff --git a/homework/advanced-calculator/.clang-format b/homework/advanced-calculator/.clang-format new file mode 100644 index 000000000..c7b6b9507 --- /dev/null +++ b/homework/advanced-calculator/.clang-format @@ -0,0 +1,9 @@ +Language: Cpp +BasedOnStyle: Chromium +UseTab: Never +IndentWidth: 4 +TabWidth: 4 +AllowShortIfStatementsOnASingleLine: Never +IndentCaseLabels: false +ColumnLimit: 0 +AccessModifierOffset: -4 diff --git a/homework/advanced-calculator/.gitignore b/homework/advanced-calculator/.gitignore new file mode 100644 index 000000000..cd81eca91 --- /dev/null +++ b/homework/advanced-calculator/.gitignore @@ -0,0 +1,51 @@ +.idea/ +*.iml +*.iws +*.eml +out/ +.DS_Store +.svn +log/*.log +tmp/** +node_modules/ +.sass-cache +css/reveal.min.css +js/reveal.min.js + +# Prerequisites +*.d + +# Builds, test, IDE +build/ +.vscode/ +googletest/ + +# Compiled Object files +*.slo +*.lo +*.o +*.obj + +# Precompiled Headers +*.gch +*.pch + +# Compiled Dynamic libraries +*.so +*.dylib +*.dll + +# Fortran module files +*.mod +*.smod + +# Compiled Static libraries +*.lai +*.la +*.a +*.lib + +# Executables +*.exe +*.out +*.app From 92a96e4a7efadd9c3814cb518855590be62c3a2c Mon Sep 17 00:00:00 2001 From: Jaroslaw Jeda <164629446+jarek1992@users.noreply.github.com> Date: Wed, 13 Aug 2025 18:19:46 +0200 Subject: [PATCH 007/104] Delete .clang-format --- .clang-format | 9 --------- 1 file changed, 9 deletions(-) delete mode 100644 .clang-format diff --git a/.clang-format b/.clang-format deleted file mode 100644 index c7b6b9507..000000000 --- a/.clang-format +++ /dev/null @@ -1,9 +0,0 @@ -Language: Cpp -BasedOnStyle: Chromium -UseTab: Never -IndentWidth: 4 -TabWidth: 4 -AllowShortIfStatementsOnASingleLine: Never -IndentCaseLabels: false -ColumnLimit: 0 -AccessModifierOffset: -4 From 5689513fcba158c304577e832792e3f19f0edc06 Mon Sep 17 00:00:00 2001 From: Jaroslaw Jeda <164629446+jarek1992@users.noreply.github.com> Date: Wed, 13 Aug 2025 18:20:05 +0200 Subject: [PATCH 008/104] Delete .gitignore --- .gitignore | 51 --------------------------------------------------- 1 file changed, 51 deletions(-) delete mode 100644 .gitignore diff --git a/.gitignore b/.gitignore deleted file mode 100644 index cd81eca91..000000000 --- a/.gitignore +++ /dev/null @@ -1,51 +0,0 @@ -.idea/ -*.iml -*.iws -*.eml -out/ -.DS_Store -.svn -log/*.log -tmp/** -node_modules/ -.sass-cache -css/reveal.min.css -js/reveal.min.js - -# Prerequisites -*.d - -# Builds, test, IDE -build/ -.vscode/ -googletest/ - -# Compiled Object files -*.slo -*.lo -*.o -*.obj - -# Precompiled Headers -*.gch -*.pch - -# Compiled Dynamic libraries -*.so -*.dylib -*.dll - -# Fortran module files -*.mod -*.smod - -# Compiled Static libraries -*.lai -*.la -*.a -*.lib - -# Executables -*.exe -*.out -*.app From b8e3ac0d1ce4263d661cde3cd58556810eb55686 Mon Sep 17 00:00:00 2001 From: Jarek Jeda Date: Wed, 13 Aug 2025 18:22:50 +0200 Subject: [PATCH 009/104] lst2 --- homework/advanced-calculator/.clang-format | 9 ---- homework/advanced-calculator/.gitignore | 51 ---------------------- 2 files changed, 60 deletions(-) delete mode 100644 homework/advanced-calculator/.clang-format delete mode 100644 homework/advanced-calculator/.gitignore diff --git a/homework/advanced-calculator/.clang-format b/homework/advanced-calculator/.clang-format deleted file mode 100644 index c7b6b9507..000000000 --- a/homework/advanced-calculator/.clang-format +++ /dev/null @@ -1,9 +0,0 @@ -Language: Cpp -BasedOnStyle: Chromium -UseTab: Never -IndentWidth: 4 -TabWidth: 4 -AllowShortIfStatementsOnASingleLine: Never -IndentCaseLabels: false -ColumnLimit: 0 -AccessModifierOffset: -4 diff --git a/homework/advanced-calculator/.gitignore b/homework/advanced-calculator/.gitignore deleted file mode 100644 index cd81eca91..000000000 --- a/homework/advanced-calculator/.gitignore +++ /dev/null @@ -1,51 +0,0 @@ -.idea/ -*.iml -*.iws -*.eml -out/ -.DS_Store -.svn -log/*.log -tmp/** -node_modules/ -.sass-cache -css/reveal.min.css -js/reveal.min.js - -# Prerequisites -*.d - -# Builds, test, IDE -build/ -.vscode/ -googletest/ - -# Compiled Object files -*.slo -*.lo -*.o -*.obj - -# Precompiled Headers -*.gch -*.pch - -# Compiled Dynamic libraries -*.so -*.dylib -*.dll - -# Fortran module files -*.mod -*.smod - -# Compiled Static libraries -*.lai -*.la -*.a -*.lib - -# Executables -*.exe -*.out -*.app From 223e534e3f4e3cfab6e2e677f5d185a64d8c49c1 Mon Sep 17 00:00:00 2001 From: Jarek Jeda Date: Wed, 13 Aug 2025 18:26:07 +0200 Subject: [PATCH 010/104] last2 --- .clang-format | 9 +++++++++ .gitignore | 52 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 .clang-format create mode 100644 .gitignore diff --git a/.clang-format b/.clang-format new file mode 100644 index 000000000..c7b6b9507 --- /dev/null +++ b/.clang-format @@ -0,0 +1,9 @@ +Language: Cpp +BasedOnStyle: Chromium +UseTab: Never +IndentWidth: 4 +TabWidth: 4 +AllowShortIfStatementsOnASingleLine: Never +IndentCaseLabels: false +ColumnLimit: 0 +AccessModifierOffset: -4 diff --git a/.gitignore b/.gitignore new file mode 100644 index 000000000..f9fb19b75 --- /dev/null +++ b/.gitignore @@ -0,0 +1,52 @@ +.vs/ +.idea/ +*.iml +*.iws +*.eml +out/ +.DS_Store +.svn +log/*.log +tmp/** +node_modules/ +.sass-cache +css/reveal.min.css +js/reveal.min.js + +# Prerequisites +*.d + +# Builds, test, IDE +build/ +.vscode/ +googletest/ + +# Compiled Object files +*.slo +*.lo +*.o +*.obj + +# Precompiled Headers +*.gch +*.pch + +# Compiled Dynamic libraries +*.so +*.dylib +*.dll + +# Fortran module files +*.mod +*.smod + +# Compiled Static libraries +*.lai +*.la +*.a +*.lib + +# Executables +*.exe +*.out +*.app From ea2a17c31c2406e453d17e031be8677e78f3fb46 Mon Sep 17 00:00:00 2001 From: Jarek Jeda Date: Wed, 13 Aug 2025 18:27:54 +0200 Subject: [PATCH 011/104] last3 --- .../advancedCalculator.cpp | 26 ++++++++----------- .../advancedCalculator.hpp | 13 +++++----- 2 files changed, 17 insertions(+), 22 deletions(-) diff --git a/homework/advanced-calculator/advancedCalculator.cpp b/homework/advanced-calculator/advancedCalculator.cpp index 057ad7979..ebee34833 100644 --- a/homework/advanced-calculator/advancedCalculator.cpp +++ b/homework/advanced-calculator/advancedCalculator.cpp @@ -26,50 +26,48 @@ AdvancedCalculator::AdvancedCalculator() { operations['+'] = [](double a, double b, double* out) { *out = a + b; return ErrorCode::OK; - }; + }; operations['-'] = [](double a, double b, double* out) { *out = a - b; return ErrorCode::OK; - }; + }; operations['*'] = [](double a, double b, double* out) { *out = a * b; return ErrorCode::OK; - }; + }; operations['/'] = [](double a, double b, double* out) { if (b == 0.0) { return ErrorCode::DivideBy0; } *out = a / b; return ErrorCode::OK; - }; + }; operations['%'] = [](double a, double b, double* out) { if (b == 0.0) { return ErrorCode::DivideBy0; - } - else if (static_cast(a) != a || static_cast(b) != b) { + } else if (static_cast(a) != a || static_cast(b) != b) { return ErrorCode::ModuleOfNonIntegerValue; } *out = static_cast(a) % static_cast(b); return ErrorCode::OK; - }; + }; operations['^'] = [](double a, double b, double* out) { *out = std::pow(a, b); return ErrorCode::OK; - }; + }; operations['$'] = [](double a, double b, double* out) { if (b == 0.0) { return ErrorCode::DivideBy0; - } - else if (a < 0 && static_cast(b) % 2 == 0) { + } else if (a < 0 && static_cast(b) % 2 == 0) { return ErrorCode::SqrtOfNegativeNumber; } *out = std::pow(a, 1.0 / b); return ErrorCode::OK; - }; + }; operations['!'] = [](double a, double, double* out) { *out = std::tgamma(a + 1); return ErrorCode::OK; - }; + }; } ErrorCode AdvancedCalculator::process(const std::string& input, double* out) const { @@ -82,7 +80,7 @@ ErrorCode AdvancedCalculator::process(const std::string& input, double* out) con if (!iss || operations.find(op) == operations.end()) { return ErrorCode::BadCharacter; } - + if (op != '!') { iss >> b; if (!iss) { @@ -91,5 +89,3 @@ ErrorCode AdvancedCalculator::process(const std::string& input, double* out) con } return operations.at(op)(a, b, out); } - - diff --git a/homework/advanced-calculator/advancedCalculator.hpp b/homework/advanced-calculator/advancedCalculator.hpp index 47470ab12..4524fc3ce 100644 --- a/homework/advanced-calculator/advancedCalculator.hpp +++ b/homework/advanced-calculator/advancedCalculator.hpp @@ -1,13 +1,13 @@ -#include #include +#include #include #include enum class ErrorCode { - OK, - BadCharacter, - BadFormat, - DivideBy0, + OK, + BadCharacter, + BadFormat, + DivideBy0, SqrtOfNegativeNumber, ModuleOfNonIntegerValue }; @@ -16,10 +16,9 @@ std::string errorCodeToString(ErrorCode code); class AdvancedCalculator { public: - AdvancedCalculator(); + AdvancedCalculator(); ErrorCode process(const std::string& input, double* out) const; private: std::map> operations; - }; \ No newline at end of file From aa4ce61dc557da2d08508671e78ec6dcb1654112 Mon Sep 17 00:00:00 2001 From: Jarek Jeda Date: Thu, 14 Aug 2025 13:54:58 +0200 Subject: [PATCH 012/104] process --- homework/advanced-calculator/advancedCalculator.cpp | 7 ++++++- homework/advanced-calculator/advancedCalculator.hpp | 10 ++++++++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/homework/advanced-calculator/advancedCalculator.cpp b/homework/advanced-calculator/advancedCalculator.cpp index ebee34833..8a3db2c26 100644 --- a/homework/advanced-calculator/advancedCalculator.cpp +++ b/homework/advanced-calculator/advancedCalculator.cpp @@ -70,7 +70,7 @@ AdvancedCalculator::AdvancedCalculator() { }; } -ErrorCode AdvancedCalculator::process(const std::string& input, double* out) const { +ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { std::istringstream iss(input); double a = 0; double b = 0; @@ -89,3 +89,8 @@ ErrorCode AdvancedCalculator::process(const std::string& input, double* out) con } return operations.at(op)(a, b, out); } + +ErrorCode process(const std::string& input, double* out) { + AdvancedCalculator calc; + return calc.process(input, out); +} \ No newline at end of file diff --git a/homework/advanced-calculator/advancedCalculator.hpp b/homework/advanced-calculator/advancedCalculator.hpp index 4524fc3ce..7084a87f2 100644 --- a/homework/advanced-calculator/advancedCalculator.hpp +++ b/homework/advanced-calculator/advancedCalculator.hpp @@ -1,3 +1,6 @@ +#pragma once + +#include #include #include #include @@ -17,8 +20,11 @@ std::string errorCodeToString(ErrorCode code); class AdvancedCalculator { public: AdvancedCalculator(); - ErrorCode process(const std::string& input, double* out) const; + ErrorCode process(const std::string& input, double* out); private: std::map> operations; -}; \ No newline at end of file +}; + +ErrorCode process(const std::string& input, double* out); + From 5f50d2bb905ed59cfb30e8320ad35baf48ab6ac8 Mon Sep 17 00:00:00 2001 From: Jarek Jeda Date: Thu, 14 Aug 2025 13:56:53 +0200 Subject: [PATCH 013/104] process2 --- homework/advanced-calculator/advancedCalculator.hpp | 1 - 1 file changed, 1 deletion(-) diff --git a/homework/advanced-calculator/advancedCalculator.hpp b/homework/advanced-calculator/advancedCalculator.hpp index 7084a87f2..edf3b4e6d 100644 --- a/homework/advanced-calculator/advancedCalculator.hpp +++ b/homework/advanced-calculator/advancedCalculator.hpp @@ -1,6 +1,5 @@ #pragma once -#include #include #include #include From 8e2fca2996f003af7e2c9ed526f6c3521e7632b1 Mon Sep 17 00:00:00 2001 From: Jarek Jeda Date: Sat, 16 Aug 2025 19:40:05 +0200 Subject: [PATCH 014/104] test_checks --- .../advancedCalculator.cpp | 31 ++++++++++++++++--- .../advancedCalculator.hpp | 2 +- 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/homework/advanced-calculator/advancedCalculator.cpp b/homework/advanced-calculator/advancedCalculator.cpp index 8a3db2c26..701992663 100644 --- a/homework/advanced-calculator/advancedCalculator.cpp +++ b/homework/advanced-calculator/advancedCalculator.cpp @@ -43,10 +43,10 @@ AdvancedCalculator::AdvancedCalculator() { return ErrorCode::OK; }; operations['%'] = [](double a, double b, double* out) { - if (b == 0.0) { - return ErrorCode::DivideBy0; - } else if (static_cast(a) != a || static_cast(b) != b) { + if (std::floor(a) != a || std::floor(b) != b) { return ErrorCode::ModuleOfNonIntegerValue; + } else if (b == 0.0) { + return ErrorCode::DivideBy0; } *out = static_cast(a) % static_cast(b); return ErrorCode::OK; @@ -56,6 +56,9 @@ AdvancedCalculator::AdvancedCalculator() { return ErrorCode::OK; }; operations['$'] = [](double a, double b, double* out) { + if (a < 0) { + return ErrorCode::SqrtOfNegativeNumber; + } if (b == 0.0) { return ErrorCode::DivideBy0; } else if (a < 0 && static_cast(b) % 2 == 0) { @@ -65,12 +68,24 @@ AdvancedCalculator::AdvancedCalculator() { return ErrorCode::OK; }; operations['!'] = [](double a, double, double* out) { - *out = std::tgamma(a + 1); + bool negative = a < 0; + a = std::fabs(a); + + if (a != std::floor(a)) { + return ErrorCode::BadFormat; + } + + double result = 1; + for (int i = 1; i <= static_cast(a); ++i) { + result *= i; + } + + *out = negative ? -result : result; return ErrorCode::OK; }; } -ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { +ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { std::istringstream iss(input); double a = 0; double b = 0; @@ -87,6 +102,12 @@ ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { return ErrorCode::BadFormat; } } + + char extra; + if (iss >> extra) { + return ErrorCode::BadCharacter; + } + return operations.at(op)(a, b, out); } diff --git a/homework/advanced-calculator/advancedCalculator.hpp b/homework/advanced-calculator/advancedCalculator.hpp index edf3b4e6d..bc1e7c05e 100644 --- a/homework/advanced-calculator/advancedCalculator.hpp +++ b/homework/advanced-calculator/advancedCalculator.hpp @@ -25,5 +25,5 @@ class AdvancedCalculator { std::map> operations; }; -ErrorCode process(const std::string& input, double* out); +ErrorCode process(const std::string& input, double* out) const; From 4d9f67a1bd88e3fd4f9187d44722f418478ec30b Mon Sep 17 00:00:00 2001 From: Jarek Jeda Date: Sat, 16 Aug 2025 19:47:07 +0200 Subject: [PATCH 015/104] test cdd --- homework/advanced-calculator/advancedCalculator.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/homework/advanced-calculator/advancedCalculator.hpp b/homework/advanced-calculator/advancedCalculator.hpp index bc1e7c05e..edf3b4e6d 100644 --- a/homework/advanced-calculator/advancedCalculator.hpp +++ b/homework/advanced-calculator/advancedCalculator.hpp @@ -25,5 +25,5 @@ class AdvancedCalculator { std::map> operations; }; -ErrorCode process(const std::string& input, double* out) const; +ErrorCode process(const std::string& input, double* out); From b2a2d61550d520936201a293a1d27d71cf6726f7 Mon Sep 17 00:00:00 2001 From: Jarek Jeda Date: Sat, 16 Aug 2025 20:48:38 +0200 Subject: [PATCH 016/104] check --- .../advancedCalculator.cpp | 62 +++++++++++-------- 1 file changed, 37 insertions(+), 25 deletions(-) diff --git a/homework/advanced-calculator/advancedCalculator.cpp b/homework/advanced-calculator/advancedCalculator.cpp index 701992663..dd7604091 100644 --- a/homework/advanced-calculator/advancedCalculator.cpp +++ b/homework/advanced-calculator/advancedCalculator.cpp @@ -68,48 +68,60 @@ AdvancedCalculator::AdvancedCalculator() { return ErrorCode::OK; }; operations['!'] = [](double a, double, double* out) { - bool negative = a < 0; - a = std::fabs(a); - - if (a != std::floor(a)) { + if (std::isnan(a) || std::isinf(a)) { return ErrorCode::BadFormat; } + bool negative = a < 0; + double x = std::fabs(a); double result = 1; - for (int i = 1; i <= static_cast(a); ++i) { - result *= i; + + if (x == std::floor(x)) { + for (int i = 1; i <= static_cast(x); ++i) { + result *= i; + } + } else { + result = tgamma(x + 1); } - + *out = negative ? -result : result; return ErrorCode::OK; }; } -ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { - std::istringstream iss(input); - double a = 0; - double b = 0; - char op = 0; +ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { - iss >> a >> op; - if (!iss || operations.find(op) == operations.end()) { - return ErrorCode::BadCharacter; + for (char c : input) { + if (!std::isdigit(c) && c != '+' && c != '-' && c != '*' && c != '/' && + c != '.' && c != '!' && c != ' ' && c != '(' && c != ')') { + return ErrorCode::BadCharacter; + } } - if (op != '!') { - iss >> b; - if (!iss) { + std::istringstream iss(input); + double a = 0; + double b = 0; + char op = 0; + + iss >> a >> op; + if (!iss || operations.find(op) == operations.end()) { return ErrorCode::BadFormat; } - } - char extra; - if (iss >> extra) { - return ErrorCode::BadCharacter; - } + if (op != '!') { + iss >> b; + if (!iss) { + return ErrorCode::BadFormat; + } + } - return operations.at(op)(a, b, out); -} + char extra; + if (iss >> extra) { + return ErrorCode::BadCharacter; + } + + return operations.at(op)(a, b, out); + } ErrorCode process(const std::string& input, double* out) { AdvancedCalculator calc; From 405b86fc77394024880640e6e00f07e89ee90494 Mon Sep 17 00:00:00 2001 From: Jarek Jeda Date: Wed, 3 Sep 2025 12:14:56 +0200 Subject: [PATCH 017/104] check2 --- homework/advanced-calculator/advancedCalculator.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/homework/advanced-calculator/advancedCalculator.cpp b/homework/advanced-calculator/advancedCalculator.cpp index dd7604091..0e764df26 100644 --- a/homework/advanced-calculator/advancedCalculator.cpp +++ b/homework/advanced-calculator/advancedCalculator.cpp @@ -126,4 +126,4 @@ ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { ErrorCode process(const std::string& input, double* out) { AdvancedCalculator calc; return calc.process(input, out); -} \ No newline at end of file +} From d4aa4953256d9dd2e25d24945643a6ca6e4bd831 Mon Sep 17 00:00:00 2001 From: Jarek Jeda Date: Wed, 3 Sep 2025 13:05:30 +0200 Subject: [PATCH 018/104] test_9_fixed --- homework/advanced-calculator/advancedCalculator.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/homework/advanced-calculator/advancedCalculator.cpp b/homework/advanced-calculator/advancedCalculator.cpp index 0e764df26..27e0c81d5 100644 --- a/homework/advanced-calculator/advancedCalculator.cpp +++ b/homework/advanced-calculator/advancedCalculator.cpp @@ -104,9 +104,12 @@ ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { char op = 0; iss >> a >> op; - if (!iss || operations.find(op) == operations.end()) { + if (!iss) { return ErrorCode::BadFormat; } + if (operations.find(op) == operations.end()) { + return ErrorCode::BadCharacter; + } if (op != '!') { iss >> b; From 5d170840d93721f4a14e4b77bbf6efe0c9772e40 Mon Sep 17 00:00:00 2001 From: Jarek Jeda Date: Wed, 3 Sep 2025 15:02:22 +0200 Subject: [PATCH 019/104] errors_process/!_fix --- .../advancedCalculator.cpp | 24 +++++++------------ 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/homework/advanced-calculator/advancedCalculator.cpp b/homework/advanced-calculator/advancedCalculator.cpp index 27e0c81d5..b960386fb 100644 --- a/homework/advanced-calculator/advancedCalculator.cpp +++ b/homework/advanced-calculator/advancedCalculator.cpp @@ -72,32 +72,26 @@ AdvancedCalculator::AdvancedCalculator() { return ErrorCode::BadFormat; } - bool negative = a < 0; - double x = std::fabs(a); - double result = 1; + if (a < 0 && a == std::floor(a)) { + return ErrorCode::BadFormat; + } - if (x == std::floor(x)) { - for (int i = 1; i <= static_cast(x); ++i) { + if (a == std::floor(a)) { + long long n = static_cast(a); + double result = 1; + for (long long i = 1; i <= n; ++i) { result *= i; } + *out = result; } else { - result = tgamma(x + 1); + *out = tgamma(a + 1); } - *out = negative ? -result : result; return ErrorCode::OK; }; } ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { - - for (char c : input) { - if (!std::isdigit(c) && c != '+' && c != '-' && c != '*' && c != '/' && - c != '.' && c != '!' && c != ' ' && c != '(' && c != ')') { - return ErrorCode::BadCharacter; - } - } - std::istringstream iss(input); double a = 0; double b = 0; From 1ec09915eb432ad53462b4fecb347289ed1f6960 Mon Sep 17 00:00:00 2001 From: Jarek Jeda Date: Wed, 3 Sep 2025 15:28:26 +0200 Subject: [PATCH 020/104] test 8 9 10_fixes --- .../advancedCalculator.cpp | 60 +++++++++++-------- 1 file changed, 36 insertions(+), 24 deletions(-) diff --git a/homework/advanced-calculator/advancedCalculator.cpp b/homework/advanced-calculator/advancedCalculator.cpp index b960386fb..16c1606a4 100644 --- a/homework/advanced-calculator/advancedCalculator.cpp +++ b/homework/advanced-calculator/advancedCalculator.cpp @@ -76,50 +76,62 @@ AdvancedCalculator::AdvancedCalculator() { return ErrorCode::BadFormat; } - if (a == std::floor(a)) { - long long n = static_cast(a); - double result = 1; - for (long long i = 1; i <= n; ++i) { + long double result = 1.0; + double x = std::fabs(a); + + if (x == std::floor(x)) { + for (int i = 1; i <= static_cast(x); ++i) { result *= i; } - *out = result; } else { - *out = tgamma(a + 1); + result = tgamma(x + 1); } + *out = (a < 0 ? -result : result); return ErrorCode::OK; }; } ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { - std::istringstream iss(input); - double a = 0; - double b = 0; - char op = 0; - - iss >> a >> op; - if (!iss) { + for (char c : input) { + if (c == ',') { return ErrorCode::BadFormat; } - if (operations.find(op) == operations.end()) { + if (!std::isdigit(c) && c != '+' && c != '-' && c != '*' && c != '/' && + c != '.' && c != '!' && c != ' ' && c != '(' && c != ')' && + c != '%' && c != '^' && c != '$') { return ErrorCode::BadCharacter; } + } - if (op != '!') { - iss >> b; - if (!iss) { - return ErrorCode::BadFormat; - } - } + std::istringstream iss(input); + double a = 0; + double b = 0; + char op = 0; - char extra; - if (iss >> extra) { - return ErrorCode::BadCharacter; + iss >> a >> op; + if (!iss) { + return ErrorCode::BadFormat; + } + if (operations.find(op) == operations.end()) { + return ErrorCode::BadCharacter; + } + + if (op != '!') { + iss >> b; + if (!iss) { + return ErrorCode::BadFormat; } + } - return operations.at(op)(a, b, out); + char extra; + if (iss >> extra) { + return ErrorCode::BadCharacter; } + return operations.at(op)(a, b, out); +} + ErrorCode process(const std::string& input, double* out) { AdvancedCalculator calc; return calc.process(input, out); From 3480bc94b749ee3b58180e8172489c145a10efdd Mon Sep 17 00:00:00 2001 From: Jarek Jeda Date: Wed, 3 Sep 2025 15:32:27 +0200 Subject: [PATCH 021/104] test8_fix --- homework/advanced-calculator/advancedCalculator.cpp | 4 ---- 1 file changed, 4 deletions(-) diff --git a/homework/advanced-calculator/advancedCalculator.cpp b/homework/advanced-calculator/advancedCalculator.cpp index 16c1606a4..9becb7c0e 100644 --- a/homework/advanced-calculator/advancedCalculator.cpp +++ b/homework/advanced-calculator/advancedCalculator.cpp @@ -72,10 +72,6 @@ AdvancedCalculator::AdvancedCalculator() { return ErrorCode::BadFormat; } - if (a < 0 && a == std::floor(a)) { - return ErrorCode::BadFormat; - } - long double result = 1.0; double x = std::fabs(a); From e3d3507353ebe5dec7fb9889879c2a07a246a748 Mon Sep 17 00:00:00 2001 From: Jarek Jeda Date: Wed, 3 Sep 2025 15:57:01 +0200 Subject: [PATCH 022/104] test 9,10_fixes --- homework/advanced-calculator/advancedCalculator.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/homework/advanced-calculator/advancedCalculator.cpp b/homework/advanced-calculator/advancedCalculator.cpp index 9becb7c0e..835bcebab 100644 --- a/homework/advanced-calculator/advancedCalculator.cpp +++ b/homework/advanced-calculator/advancedCalculator.cpp @@ -109,6 +109,7 @@ ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { if (!iss) { return ErrorCode::BadFormat; } + if (operations.find(op) == operations.end()) { return ErrorCode::BadCharacter; } From 40633b0b2796f629e6801d5ec41a87a2f0743efc Mon Sep 17 00:00:00 2001 From: Jarek Jeda Date: Wed, 3 Sep 2025 16:10:48 +0200 Subject: [PATCH 023/104] test9,10_fixes --- .../advanced-calculator/advancedCalculator.cpp | 16 ++++++++++------ .../advanced-calculator/advancedCalculator.hpp | 1 - homework/advanced-calculator/test.cpp | 2 +- 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/homework/advanced-calculator/advancedCalculator.cpp b/homework/advanced-calculator/advancedCalculator.cpp index 835bcebab..5d97f0535 100644 --- a/homework/advanced-calculator/advancedCalculator.cpp +++ b/homework/advanced-calculator/advancedCalculator.cpp @@ -90,14 +90,14 @@ AdvancedCalculator::AdvancedCalculator() { ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { for (char c : input) { - if (c == ',') { - return ErrorCode::BadFormat; - } if (!std::isdigit(c) && c != '+' && c != '-' && c != '*' && c != '/' && c != '.' && c != '!' && c != ' ' && c != '(' && c != ')' && c != '%' && c != '^' && c != '$') { return ErrorCode::BadCharacter; } + if (c == ',') { + return ErrorCode::BadFormat; + } } std::istringstream iss(input); @@ -121,9 +121,13 @@ ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { } } - char extra; - if (iss >> extra) { - return ErrorCode::BadCharacter; + std::string rest; + if (std::getline(iss, rest)) { + for (char c : rest) { + if (!std::isspace(c)) { + return ErrorCode::BadFormat; + } + } } return operations.at(op)(a, b, out); diff --git a/homework/advanced-calculator/advancedCalculator.hpp b/homework/advanced-calculator/advancedCalculator.hpp index edf3b4e6d..74cf9e233 100644 --- a/homework/advanced-calculator/advancedCalculator.hpp +++ b/homework/advanced-calculator/advancedCalculator.hpp @@ -26,4 +26,3 @@ class AdvancedCalculator { }; ErrorCode process(const std::string& input, double* out); - diff --git a/homework/advanced-calculator/test.cpp b/homework/advanced-calculator/test.cpp index 66c11b188..00714a005 100644 --- a/homework/advanced-calculator/test.cpp +++ b/homework/advanced-calculator/test.cpp @@ -170,7 +170,7 @@ TEST(advancedCalculatorTest, ShouldReturnDivideBy0) { ASSERT_EQ(process("123 / 0", &result), ErrorCode::DivideBy0); ASSERT_EQ(process("123 / 0.0", &result), ErrorCode::DivideBy0); - ASSERT_EQ(process("123 / -0", &result), ErrorCode::DivideBy0); + ASSERT_EQ(process("123 / -0", &result), ErrorCode::DivideBy0); ASSERT_EQ(process("123 / -0.0", &result), ErrorCode::DivideBy0); ASSERT_EQ(process("-123 / -0.0", &result), ErrorCode::DivideBy0); ASSERT_EQ(process("0.0 / 0", &result), ErrorCode::DivideBy0); From 1fc2cabe79f622a859fc13b6877bdaff107c06bf Mon Sep 17 00:00:00 2001 From: Jarek Jeda Date: Wed, 3 Sep 2025 16:15:23 +0200 Subject: [PATCH 024/104] test10_fixes --- homework/advanced-calculator/advancedCalculator.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/homework/advanced-calculator/advancedCalculator.cpp b/homework/advanced-calculator/advancedCalculator.cpp index 5d97f0535..aa749ef5f 100644 --- a/homework/advanced-calculator/advancedCalculator.cpp +++ b/homework/advanced-calculator/advancedCalculator.cpp @@ -90,14 +90,14 @@ AdvancedCalculator::AdvancedCalculator() { ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { for (char c : input) { + if (c == ',') { + return ErrorCode::BadFormat; + } if (!std::isdigit(c) && c != '+' && c != '-' && c != '*' && c != '/' && c != '.' && c != '!' && c != ' ' && c != '(' && c != ')' && c != '%' && c != '^' && c != '$') { return ErrorCode::BadCharacter; } - if (c == ',') { - return ErrorCode::BadFormat; - } } std::istringstream iss(input); From 546a8915e99d4b39109e9a9acbff7f2fcc1e90c8 Mon Sep 17 00:00:00 2001 From: Jarek Jeda Date: Wed, 3 Sep 2025 16:32:53 +0200 Subject: [PATCH 025/104] test9,10_fixes --- homework/advanced-calculator/advancedCalculator.cpp | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/homework/advanced-calculator/advancedCalculator.cpp b/homework/advanced-calculator/advancedCalculator.cpp index aa749ef5f..14179e23a 100644 --- a/homework/advanced-calculator/advancedCalculator.cpp +++ b/homework/advanced-calculator/advancedCalculator.cpp @@ -105,20 +105,16 @@ ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { double b = 0; char op = 0; - iss >> a >> op; - if (!iss) { + if (!(iss >> a >> op)) return ErrorCode::BadFormat; - } if (operations.find(op) == operations.end()) { return ErrorCode::BadCharacter; } if (op != '!') { - iss >> b; - if (!iss) { + if (!(iss >> b)) return ErrorCode::BadFormat; - } } std::string rest; From 5abe4de82a8ec106debead3057ec5b40a72bd3a8 Mon Sep 17 00:00:00 2001 From: Jarek Jeda Date: Wed, 3 Sep 2025 20:04:21 +0200 Subject: [PATCH 026/104] tests_9,10_fixes --- homework/advanced-calculator/advancedCalculator.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/homework/advanced-calculator/advancedCalculator.cpp b/homework/advanced-calculator/advancedCalculator.cpp index 14179e23a..ffbb4062e 100644 --- a/homework/advanced-calculator/advancedCalculator.cpp +++ b/homework/advanced-calculator/advancedCalculator.cpp @@ -91,13 +91,16 @@ AdvancedCalculator::AdvancedCalculator() { ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { for (char c : input) { if (c == ',') { - return ErrorCode::BadFormat; + return ErrorCode::BadCharacter; } if (!std::isdigit(c) && c != '+' && c != '-' && c != '*' && c != '/' && c != '.' && c != '!' && c != ' ' && c != '(' && c != ')' && c != '%' && c != '^' && c != '$') { return ErrorCode::BadCharacter; } + if (!input.empty() && (input[0] == '+' || input[0] == '-')) { + return ErrorCode::BadFormat; + } } std::istringstream iss(input); From 3df7945edf6dc849a89724fd77fcfb7a95147a8a Mon Sep 17 00:00:00 2001 From: Jarek Jeda Date: Wed, 3 Sep 2025 20:06:30 +0200 Subject: [PATCH 027/104] tests --- homework/advanced-calculator/advancedCalculator.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/homework/advanced-calculator/advancedCalculator.cpp b/homework/advanced-calculator/advancedCalculator.cpp index ffbb4062e..9e4c468d1 100644 --- a/homework/advanced-calculator/advancedCalculator.cpp +++ b/homework/advanced-calculator/advancedCalculator.cpp @@ -91,16 +91,16 @@ AdvancedCalculator::AdvancedCalculator() { ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { for (char c : input) { if (c == ',') { - return ErrorCode::BadCharacter; + return ErrorCode::BadFormat; + } + if (!input.empty() && (input[0] == '+' || input[0] == '-')) { + return ErrorCode::BadFormat; } if (!std::isdigit(c) && c != '+' && c != '-' && c != '*' && c != '/' && c != '.' && c != '!' && c != ' ' && c != '(' && c != ')' && c != '%' && c != '^' && c != '$') { return ErrorCode::BadCharacter; } - if (!input.empty() && (input[0] == '+' || input[0] == '-')) { - return ErrorCode::BadFormat; - } } std::istringstream iss(input); From c56fd79247119e90f208e98f70786333e7b4419b Mon Sep 17 00:00:00 2001 From: Jarek Jeda Date: Wed, 3 Sep 2025 20:07:41 +0200 Subject: [PATCH 028/104] tests --- homework/advanced-calculator/advancedCalculator.cpp | 3 --- 1 file changed, 3 deletions(-) diff --git a/homework/advanced-calculator/advancedCalculator.cpp b/homework/advanced-calculator/advancedCalculator.cpp index 9e4c468d1..14179e23a 100644 --- a/homework/advanced-calculator/advancedCalculator.cpp +++ b/homework/advanced-calculator/advancedCalculator.cpp @@ -93,9 +93,6 @@ ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { if (c == ',') { return ErrorCode::BadFormat; } - if (!input.empty() && (input[0] == '+' || input[0] == '-')) { - return ErrorCode::BadFormat; - } if (!std::isdigit(c) && c != '+' && c != '-' && c != '*' && c != '/' && c != '.' && c != '!' && c != ' ' && c != '(' && c != ')' && c != '%' && c != '^' && c != '$') { From 2b6d47d8c4686138c1016ad01828c277e4f6db4d Mon Sep 17 00:00:00 2001 From: Jarek Jeda Date: Wed, 24 Sep 2025 17:21:54 +0200 Subject: [PATCH 029/104] error9/10 --- homework/advanced-calculator/advancedCalculator.cpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/homework/advanced-calculator/advancedCalculator.cpp b/homework/advanced-calculator/advancedCalculator.cpp index 14179e23a..30e20d5ea 100644 --- a/homework/advanced-calculator/advancedCalculator.cpp +++ b/homework/advanced-calculator/advancedCalculator.cpp @@ -89,10 +89,13 @@ AdvancedCalculator::AdvancedCalculator() { } ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { - for (char c : input) { - if (c == ',') { + if (!input.empty() && (input[0] == '+' || input[0] == '-')) { + if (input.size() > 1 && std::isdigit(input[1])) { return ErrorCode::BadFormat; } + } + + for (char c : input) { if (!std::isdigit(c) && c != '+' && c != '-' && c != '*' && c != '/' && c != '.' && c != '!' && c != ' ' && c != '(' && c != ')' && c != '%' && c != '^' && c != '$') { From 1019001057c497513178580efe934414bc9a50f8 Mon Sep 17 00:00:00 2001 From: Jarek Jeda Date: Wed, 24 Sep 2025 17:40:02 +0200 Subject: [PATCH 030/104] changes --- homework/advanced-calculator/advancedCalculator.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/homework/advanced-calculator/advancedCalculator.cpp b/homework/advanced-calculator/advancedCalculator.cpp index 30e20d5ea..104e3513c 100644 --- a/homework/advanced-calculator/advancedCalculator.cpp +++ b/homework/advanced-calculator/advancedCalculator.cpp @@ -1,7 +1,7 @@ -#include -#include +#include "advancedCalculator.hpp" -#include "advancedCalculator.hpp" +#include +#include std::string errorCodeToString(ErrorCode code) { switch (code) { @@ -99,7 +99,7 @@ ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { if (!std::isdigit(c) && c != '+' && c != '-' && c != '*' && c != '/' && c != '.' && c != '!' && c != ' ' && c != '(' && c != ')' && c != '%' && c != '^' && c != '$') { - return ErrorCode::BadCharacter; + return ErrorCode::BadCharacter; } } From d3754a40dbbce103303575582d9d5ff33e5fc263 Mon Sep 17 00:00:00 2001 From: Jarek Jeda Date: Wed, 24 Sep 2025 17:43:32 +0200 Subject: [PATCH 031/104] test --- .../advancedCalculator.cpp | 57 +++++++++++-------- 1 file changed, 32 insertions(+), 25 deletions(-) diff --git a/homework/advanced-calculator/advancedCalculator.cpp b/homework/advanced-calculator/advancedCalculator.cpp index 104e3513c..da7827ff9 100644 --- a/homework/advanced-calculator/advancedCalculator.cpp +++ b/homework/advanced-calculator/advancedCalculator.cpp @@ -89,49 +89,56 @@ AdvancedCalculator::AdvancedCalculator() { } ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { - if (!input.empty() && (input[0] == '+' || input[0] == '-')) { - if (input.size() > 1 && std::isdigit(input[1])) { - return ErrorCode::BadFormat; + std::string expr; + for (char c : input) { + if (!std::isspace(static_cast(c))) { + expr.push_back(c); } } - for (char c : input) { + if (expr.empty()) + return ErrorCode::BadFormat; + + for (char c : expr) { if (!std::isdigit(c) && c != '+' && c != '-' && c != '*' && c != '/' && - c != '.' && c != '!' && c != ' ' && c != '(' && c != ')' && - c != '%' && c != '^' && c != '$') { - return ErrorCode::BadCharacter; + c != '.' && c != '!' && c != '%' && c != '^' && c != '$') { + return ErrorCode::BadCharacter; } } - std::istringstream iss(input); - double a = 0; - double b = 0; char op = 0; + size_t opPos = std::string::npos; + for (size_t i = 0; i < expr.size(); ++i) { + char c = expr[i]; + if (operations.count(c)) { + if (op != 0) { + return ErrorCode::BadFormat; + } + op = c; + opPos = i; + } + } - if (!(iss >> a >> op)) + if (op == 0) return ErrorCode::BadFormat; - if (operations.find(op) == operations.end()) { - return ErrorCode::BadCharacter; - } + double a = 0, b = 0; - if (op != '!') { - if (!(iss >> b)) - return ErrorCode::BadFormat; - } - - std::string rest; - if (std::getline(iss, rest)) { - for (char c : rest) { - if (!std::isspace(c)) { - return ErrorCode::BadFormat; - } + try { + if (op == '!') { + a = std::stod(expr.substr(0, opPos)); + } else { + a = std::stod(expr.substr(0, opPos)); + b = std::stod(expr.substr(opPos + 1)); } + } catch (...) { + return ErrorCode::BadFormat; } return operations.at(op)(a, b, out); } + ErrorCode process(const std::string& input, double* out) { AdvancedCalculator calc; return calc.process(input, out); From cdd86c3f013aa8c54d5f866903d32aabeb860761 Mon Sep 17 00:00:00 2001 From: Jarek Jeda Date: Wed, 24 Sep 2025 17:48:35 +0200 Subject: [PATCH 032/104] ds --- homework/advanced-calculator/advancedCalculator.cpp | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/homework/advanced-calculator/advancedCalculator.cpp b/homework/advanced-calculator/advancedCalculator.cpp index da7827ff9..9682c0241 100644 --- a/homework/advanced-calculator/advancedCalculator.cpp +++ b/homework/advanced-calculator/advancedCalculator.cpp @@ -89,16 +89,17 @@ AdvancedCalculator::AdvancedCalculator() { } ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { + // 1. Usuń spacje std::string expr; for (char c : input) { if (!std::isspace(static_cast(c))) { expr.push_back(c); } } - if (expr.empty()) return ErrorCode::BadFormat; + // 2. Sprawdź znaki for (char c : expr) { if (!std::isdigit(c) && c != '+' && c != '-' && c != '*' && c != '/' && c != '.' && c != '!' && c != '%' && c != '^' && c != '$') { @@ -106,12 +107,15 @@ ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { } } + // 3. Szukaj operatora (pierwszy znak może być częścią liczby!) char op = 0; size_t opPos = std::string::npos; - for (size_t i = 0; i < expr.size(); ++i) { + + for (size_t i = 1; i < expr.size(); ++i) { // zaczynamy od 1, żeby nie traktować '+'/'-' na początku jako operatora char c = expr[i]; if (operations.count(c)) { if (op != 0) { + // więcej niż jeden operator return ErrorCode::BadFormat; } op = c; @@ -126,8 +130,10 @@ ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { try { if (op == '!') { + // factorial: tylko lewa liczba a = std::stod(expr.substr(0, opPos)); } else { + // binary operator a = std::stod(expr.substr(0, opPos)); b = std::stod(expr.substr(opPos + 1)); } @@ -135,10 +141,10 @@ ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { return ErrorCode::BadFormat; } + // 4. Wykonaj działanie return operations.at(op)(a, b, out); } - ErrorCode process(const std::string& input, double* out) { AdvancedCalculator calc; return calc.process(input, out); From ac9d5116d3e59afeaf3d5f3fccb268d6be4f5a8b Mon Sep 17 00:00:00 2001 From: Jarek Jeda Date: Wed, 24 Sep 2025 18:07:30 +0200 Subject: [PATCH 033/104] gt --- .../advancedCalculator.cpp | 63 ++++++++----------- 1 file changed, 26 insertions(+), 37 deletions(-) diff --git a/homework/advanced-calculator/advancedCalculator.cpp b/homework/advanced-calculator/advancedCalculator.cpp index 9682c0241..9df233b0d 100644 --- a/homework/advanced-calculator/advancedCalculator.cpp +++ b/homework/advanced-calculator/advancedCalculator.cpp @@ -89,59 +89,48 @@ AdvancedCalculator::AdvancedCalculator() { } ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { - // 1. Usuń spacje - std::string expr; + // 1. Walidacja znaków for (char c : input) { - if (!std::isspace(static_cast(c))) { - expr.push_back(c); + if (c == ',') { + return ErrorCode::BadFormat; } - } - if (expr.empty()) - return ErrorCode::BadFormat; - - // 2. Sprawdź znaki - for (char c : expr) { if (!std::isdigit(c) && c != '+' && c != '-' && c != '*' && c != '/' && - c != '.' && c != '!' && c != '%' && c != '^' && c != '$') { + c != '.' && c != '!' && c != ' ' && c != '(' && c != ')' && + c != '%' && c != '^' && c != '$') { return ErrorCode::BadCharacter; } } - // 3. Szukaj operatora (pierwszy znak może być częścią liczby!) + // 2. Parsowanie + std::istringstream iss(input); + double a = 0, b = 0; char op = 0; - size_t opPos = std::string::npos; - for (size_t i = 1; i < expr.size(); ++i) { // zaczynamy od 1, żeby nie traktować '+'/'-' na początku jako operatora - char c = expr[i]; - if (operations.count(c)) { - if (op != 0) { - // więcej niż jeden operator - return ErrorCode::BadFormat; - } - op = c; - opPos = i; - } + if (!(iss >> a >> op)) { + return ErrorCode::BadFormat; } - if (op == 0) - return ErrorCode::BadFormat; + // 3. Operator znany? + if (operations.find(op) == operations.end()) { + return ErrorCode::BadCharacter; + } - double a = 0, b = 0; + // 4. Unary operator factorial + if (op != '!') { + if (!(iss >> b)) { + return ErrorCode::BadFormat; + } + } - try { - if (op == '!') { - // factorial: tylko lewa liczba - a = std::stod(expr.substr(0, opPos)); - } else { - // binary operator - a = std::stod(expr.substr(0, opPos)); - b = std::stod(expr.substr(opPos + 1)); + // 5. Czy zostały dodatkowe znaki (ignorujemy spacje)? + char extra; + while (iss >> extra) { + if (!std::isspace(extra)) { + return ErrorCode::BadFormat; } - } catch (...) { - return ErrorCode::BadFormat; } - // 4. Wykonaj działanie + // 6. Wykonanie operacji return operations.at(op)(a, b, out); } From 1f974cd81aa71b19905ff1c043f6ddf00f2705ac Mon Sep 17 00:00:00 2001 From: Jarek Jeda Date: Wed, 24 Sep 2025 18:23:51 +0200 Subject: [PATCH 034/104] 9,10 error --- .../advancedCalculator.cpp | 41 +++++++++++-------- 1 file changed, 23 insertions(+), 18 deletions(-) diff --git a/homework/advanced-calculator/advancedCalculator.cpp b/homework/advanced-calculator/advancedCalculator.cpp index 9df233b0d..d2a53f428 100644 --- a/homework/advanced-calculator/advancedCalculator.cpp +++ b/homework/advanced-calculator/advancedCalculator.cpp @@ -89,11 +89,8 @@ AdvancedCalculator::AdvancedCalculator() { } ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { - // 1. Walidacja znaków + // 1. Sprawdzenie nieprawidłowych znaków for (char c : input) { - if (c == ',') { - return ErrorCode::BadFormat; - } if (!std::isdigit(c) && c != '+' && c != '-' && c != '*' && c != '/' && c != '.' && c != '!' && c != ' ' && c != '(' && c != ')' && c != '%' && c != '^' && c != '$') { @@ -101,36 +98,44 @@ ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { } } - // 2. Parsowanie std::istringstream iss(input); double a = 0, b = 0; char op = 0; - if (!(iss >> a >> op)) { + // 2. Wczytanie pierwszej liczby + if (!(iss >> a)) + return ErrorCode::BadFormat; + + // 3. Wczytanie operatora + if (!(iss >> op)) return ErrorCode::BadFormat; - } - // 3. Operator znany? - if (operations.find(op) == operations.end()) { + if (operations.find(op) == operations.end()) return ErrorCode::BadCharacter; - } - // 4. Unary operator factorial + // 4. Wczytanie drugiej liczby, jeśli to nie jest '!' if (op != '!') { - if (!(iss >> b)) { + // Sprawdzenie, czy następny znak nie jest operatorem (podwójne operatory) + char next = iss.peek(); + if (next == '+' || next == '-' || next == '*' || next == '/' || + next == '%' || next == '^' || next == '$') { return ErrorCode::BadFormat; } - } - // 5. Czy zostały dodatkowe znaki (ignorujemy spacje)? - char extra; - while (iss >> extra) { - if (!std::isspace(extra)) { + if (!(iss >> b)) return ErrorCode::BadFormat; + } + + // 5. Sprawdzenie, czy nie ma dodatkowych nie-spacji znaków + std::string rest; + if (std::getline(iss, rest)) { + for (char c : rest) { + if (!std::isspace(c)) + return ErrorCode::BadFormat; } } - // 6. Wykonanie operacji + // 6. Wywołanie odpowiedniej operacji return operations.at(op)(a, b, out); } From 2f07088d4a60013d28ac833792c8fa92d099b3a4 Mon Sep 17 00:00:00 2001 From: Jarek Jeda Date: Wed, 24 Sep 2025 18:28:17 +0200 Subject: [PATCH 035/104] 9,10 error --- .../advancedCalculator.cpp | 46 +++++++++++-------- 1 file changed, 28 insertions(+), 18 deletions(-) diff --git a/homework/advanced-calculator/advancedCalculator.cpp b/homework/advanced-calculator/advancedCalculator.cpp index d2a53f428..b2d8f048a 100644 --- a/homework/advanced-calculator/advancedCalculator.cpp +++ b/homework/advanced-calculator/advancedCalculator.cpp @@ -89,7 +89,7 @@ AdvancedCalculator::AdvancedCalculator() { } ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { - // 1. Sprawdzenie nieprawidłowych znaków + // Sprawdzenie nieprawidłowych znaków for (char c : input) { if (!std::isdigit(c) && c != '+' && c != '-' && c != '*' && c != '/' && c != '.' && c != '!' && c != ' ' && c != '(' && c != ')' && @@ -99,43 +99,53 @@ ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { } std::istringstream iss(input); + std::string token; double a = 0, b = 0; char op = 0; - // 2. Wczytanie pierwszej liczby - if (!(iss >> a)) + // Wczytanie pierwszej liczby (ze znakiem + lub -) + if (!(iss >> token)) return ErrorCode::BadFormat; + try { + a = std::stod(token); + } catch (...) { + return ErrorCode::BadFormat; + } - // 3. Wczytanie operatora + // Wczytanie operatora if (!(iss >> op)) return ErrorCode::BadFormat; - if (operations.find(op) == operations.end()) return ErrorCode::BadCharacter; - // 4. Wczytanie drugiej liczby, jeśli to nie jest '!' - if (op != '!') { - // Sprawdzenie, czy następny znak nie jest operatorem (podwójne operatory) - char next = iss.peek(); - if (next == '+' || next == '-' || next == '*' || next == '/' || - next == '%' || next == '^' || next == '$') { - return ErrorCode::BadFormat; + // W przypadku operatora "!" nie ma drugiej liczby + if (op == '!') { + std::string rest; + if (std::getline(iss, rest)) { + for (char c : rest) + if (!std::isspace(c)) + return ErrorCode::BadFormat; } + return operations.at(op)(a, 0, out); + } - if (!(iss >> b)) - return ErrorCode::BadFormat; + // Wczytanie drugiej liczby + if (!(iss >> token)) + return ErrorCode::BadFormat; + try { + b = std::stod(token); + } catch (...) { + return ErrorCode::BadFormat; } - // 5. Sprawdzenie, czy nie ma dodatkowych nie-spacji znaków + // Po drugiej liczbie nie może być nic poza spacjami std::string rest; if (std::getline(iss, rest)) { - for (char c : rest) { + for (char c : rest) if (!std::isspace(c)) return ErrorCode::BadFormat; - } } - // 6. Wywołanie odpowiedniej operacji return operations.at(op)(a, b, out); } From 953ebf1edf8685f5d0e0bd908c39792db6cd0f1e Mon Sep 17 00:00:00 2001 From: Jarek Jeda Date: Wed, 24 Sep 2025 18:30:21 +0200 Subject: [PATCH 036/104] fd --- .../advancedCalculator.cpp | 37 +++++-------------- 1 file changed, 9 insertions(+), 28 deletions(-) diff --git a/homework/advanced-calculator/advancedCalculator.cpp b/homework/advanced-calculator/advancedCalculator.cpp index b2d8f048a..efef49588 100644 --- a/homework/advanced-calculator/advancedCalculator.cpp +++ b/homework/advanced-calculator/advancedCalculator.cpp @@ -89,7 +89,7 @@ AdvancedCalculator::AdvancedCalculator() { } ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { - // Sprawdzenie nieprawidłowych znaków + // Sprawdzenie niepoprawnych znaków for (char c : input) { if (!std::isdigit(c) && c != '+' && c != '-' && c != '*' && c != '/' && c != '.' && c != '!' && c != ' ' && c != '(' && c != ')' && @@ -99,18 +99,12 @@ ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { } std::istringstream iss(input); - std::string token; double a = 0, b = 0; char op = 0; - // Wczytanie pierwszej liczby (ze znakiem + lub -) - if (!(iss >> token)) + // Wczytanie pierwszej liczby (ze znakiem) + if (!(iss >> a)) return ErrorCode::BadFormat; - try { - a = std::stod(token); - } catch (...) { - return ErrorCode::BadFormat; - } // Wczytanie operatora if (!(iss >> op)) @@ -118,27 +112,13 @@ ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { if (operations.find(op) == operations.end()) return ErrorCode::BadCharacter; - // W przypadku operatora "!" nie ma drugiej liczby - if (op == '!') { - std::string rest; - if (std::getline(iss, rest)) { - for (char c : rest) - if (!std::isspace(c)) - return ErrorCode::BadFormat; - } - return operations.at(op)(a, 0, out); - } - - // Wczytanie drugiej liczby - if (!(iss >> token)) - return ErrorCode::BadFormat; - try { - b = std::stod(token); - } catch (...) { - return ErrorCode::BadFormat; + // Operator "!" nie wymaga drugiej liczby + if (op != '!') { + if (!(iss >> b)) + return ErrorCode::BadFormat; } - // Po drugiej liczbie nie może być nic poza spacjami + // Sprawdzenie, czy nie ma dodatkowych znaków poza spacjami std::string rest; if (std::getline(iss, rest)) { for (char c : rest) @@ -146,6 +126,7 @@ ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { return ErrorCode::BadFormat; } + // Wykonanie operacji return operations.at(op)(a, b, out); } From c09e4f699dd24306a5bd085dac80f1c5f5c11da3 Mon Sep 17 00:00:00 2001 From: Jarek Jeda Date: Wed, 24 Sep 2025 18:34:12 +0200 Subject: [PATCH 037/104] fd --- homework/advanced-calculator/advancedCalculator.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/homework/advanced-calculator/advancedCalculator.cpp b/homework/advanced-calculator/advancedCalculator.cpp index efef49588..a2a66d438 100644 --- a/homework/advanced-calculator/advancedCalculator.cpp +++ b/homework/advanced-calculator/advancedCalculator.cpp @@ -89,7 +89,7 @@ AdvancedCalculator::AdvancedCalculator() { } ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { - // Sprawdzenie niepoprawnych znaków + // Sprawdzenie niedozwolonych znaków for (char c : input) { if (!std::isdigit(c) && c != '+' && c != '-' && c != '*' && c != '/' && c != '.' && c != '!' && c != ' ' && c != '(' && c != ')' && @@ -118,12 +118,13 @@ ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { return ErrorCode::BadFormat; } - // Sprawdzenie, czy nie ma dodatkowych znaków poza spacjami + // Sprawdzenie, czy nie ma więcej danych poza spacjami std::string rest; if (std::getline(iss, rest)) { - for (char c : rest) + for (char c : rest) { if (!std::isspace(c)) return ErrorCode::BadFormat; + } } // Wykonanie operacji From 8de23888521d8a4b80f6319ab342cdc1b235844a Mon Sep 17 00:00:00 2001 From: Jarek Jeda Date: Wed, 24 Sep 2025 18:38:28 +0200 Subject: [PATCH 038/104] new --- homework/advanced-calculator/advancedCalculator.cpp | 6 ------ 1 file changed, 6 deletions(-) diff --git a/homework/advanced-calculator/advancedCalculator.cpp b/homework/advanced-calculator/advancedCalculator.cpp index a2a66d438..87ec446be 100644 --- a/homework/advanced-calculator/advancedCalculator.cpp +++ b/homework/advanced-calculator/advancedCalculator.cpp @@ -89,7 +89,6 @@ AdvancedCalculator::AdvancedCalculator() { } ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { - // Sprawdzenie niedozwolonych znaków for (char c : input) { if (!std::isdigit(c) && c != '+' && c != '-' && c != '*' && c != '/' && c != '.' && c != '!' && c != ' ' && c != '(' && c != ')' && @@ -102,23 +101,19 @@ ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { double a = 0, b = 0; char op = 0; - // Wczytanie pierwszej liczby (ze znakiem) if (!(iss >> a)) return ErrorCode::BadFormat; - // Wczytanie operatora if (!(iss >> op)) return ErrorCode::BadFormat; if (operations.find(op) == operations.end()) return ErrorCode::BadCharacter; - // Operator "!" nie wymaga drugiej liczby if (op != '!') { if (!(iss >> b)) return ErrorCode::BadFormat; } - // Sprawdzenie, czy nie ma więcej danych poza spacjami std::string rest; if (std::getline(iss, rest)) { for (char c : rest) { @@ -127,7 +122,6 @@ ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { } } - // Wykonanie operacji return operations.at(op)(a, b, out); } From 42ad96cc20d85dff038d2962510dafb27fd41301 Mon Sep 17 00:00:00 2001 From: Jarek Jeda Date: Sat, 27 Sep 2025 18:50:46 +0200 Subject: [PATCH 039/104] test9_fix --- homework/advanced-calculator/advancedCalculator.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/homework/advanced-calculator/advancedCalculator.cpp b/homework/advanced-calculator/advancedCalculator.cpp index 87ec446be..911275a00 100644 --- a/homework/advanced-calculator/advancedCalculator.cpp +++ b/homework/advanced-calculator/advancedCalculator.cpp @@ -95,6 +95,9 @@ ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { c != '%' && c != '^' && c != '$') { return ErrorCode::BadCharacter; } + if (c == ',') { + return ErrorCode::BadFormat; + } } std::istringstream iss(input); From d76bbeb961f3f06321e4307fad07aaa072e05c67 Mon Sep 17 00:00:00 2001 From: Jarek Jeda Date: Sat, 27 Sep 2025 19:00:45 +0200 Subject: [PATCH 040/104] test9_fix2 --- homework/advanced-calculator/advancedCalculator.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/homework/advanced-calculator/advancedCalculator.cpp b/homework/advanced-calculator/advancedCalculator.cpp index 911275a00..f9a9a3805 100644 --- a/homework/advanced-calculator/advancedCalculator.cpp +++ b/homework/advanced-calculator/advancedCalculator.cpp @@ -90,14 +90,14 @@ AdvancedCalculator::AdvancedCalculator() { ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { for (char c : input) { + if (c == ',') { + return ErrorCode::BadFormat; + } if (!std::isdigit(c) && c != '+' && c != '-' && c != '*' && c != '/' && c != '.' && c != '!' && c != ' ' && c != '(' && c != ')' && c != '%' && c != '^' && c != '$') { return ErrorCode::BadCharacter; } - if (c == ',') { - return ErrorCode::BadFormat; - } } std::istringstream iss(input); From f64337d28248d3753035f0d1ea186a39b1220880 Mon Sep 17 00:00:00 2001 From: Jarek Jeda Date: Sat, 27 Sep 2025 19:05:33 +0200 Subject: [PATCH 041/104] fix 9,10 --- homework/advanced-calculator/advancedCalculator.cpp | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/homework/advanced-calculator/advancedCalculator.cpp b/homework/advanced-calculator/advancedCalculator.cpp index f9a9a3805..c453ce3dc 100644 --- a/homework/advanced-calculator/advancedCalculator.cpp +++ b/homework/advanced-calculator/advancedCalculator.cpp @@ -89,15 +89,17 @@ AdvancedCalculator::AdvancedCalculator() { } ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { - for (char c : input) { - if (c == ',') { - return ErrorCode::BadFormat; - } + for (size_t i = 0; i < input.size(); ++i) { + char c = input[i]; + if (!std::isdigit(c) && c != '+' && c != '-' && c != '*' && c != '/' && c != '.' && c != '!' && c != ' ' && c != '(' && c != ')' && c != '%' && c != '^' && c != '$') { return ErrorCode::BadCharacter; } + if ((c == '+' || c == '-') && i == 0 && std::isdigit(input[i + 1])) { + return ErrorCode::BadFormat; + } } std::istringstream iss(input); From a510475ceb7ff1702692ee340d63432cc2e19710 Mon Sep 17 00:00:00 2001 From: Jarek Jeda Date: Sat, 27 Sep 2025 19:07:26 +0200 Subject: [PATCH 042/104] fix --- .../advancedCalculator.cpp | 24 +++++++++++++------ 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/homework/advanced-calculator/advancedCalculator.cpp b/homework/advanced-calculator/advancedCalculator.cpp index c453ce3dc..fd694768a 100644 --- a/homework/advanced-calculator/advancedCalculator.cpp +++ b/homework/advanced-calculator/advancedCalculator.cpp @@ -89,44 +89,54 @@ AdvancedCalculator::AdvancedCalculator() { } ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { - for (size_t i = 0; i < input.size(); ++i) { - char c = input[i]; - + // 1. Sprawdź znaki + for (char c : input) { if (!std::isdigit(c) && c != '+' && c != '-' && c != '*' && c != '/' && c != '.' && c != '!' && c != ' ' && c != '(' && c != ')' && c != '%' && c != '^' && c != '$') { return ErrorCode::BadCharacter; } - if ((c == '+' || c == '-') && i == 0 && std::isdigit(input[i + 1])) { - return ErrorCode::BadFormat; - } } + // 2. Przygotuj parser std::istringstream iss(input); double a = 0, b = 0; char op = 0; + // liczba A if (!(iss >> a)) return ErrorCode::BadFormat; + // operator if (!(iss >> op)) return ErrorCode::BadFormat; + if (operations.find(op) == operations.end()) return ErrorCode::BadCharacter; + // liczba B (jeśli nie jest to operacja unarna) if (op != '!') { if (!(iss >> b)) return ErrorCode::BadFormat; } + // 3. Specjalne reguły formatu + // przypadek: "+8 - 32.1" -> BadFormat (liczba zaczyna się od '+') + if (!input.empty() && input[0] == '+') { + return ErrorCode::BadFormat; + } + + // sprawdź resztę (powinny być tylko spacje) std::string rest; if (std::getline(iss, rest)) { for (char c : rest) { - if (!std::isspace(c)) + if (!std::isspace(c)) { return ErrorCode::BadFormat; + } } } + // 4. Wykonaj działanie return operations.at(op)(a, b, out); } From 0c03c7cc51514a4b99f3780eb23845936e7d1d31 Mon Sep 17 00:00:00 2001 From: Jarek Jeda Date: Sat, 27 Sep 2025 19:09:33 +0200 Subject: [PATCH 043/104] test9_fix --- homework/advanced-calculator/advancedCalculator.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/homework/advanced-calculator/advancedCalculator.cpp b/homework/advanced-calculator/advancedCalculator.cpp index fd694768a..924de0e67 100644 --- a/homework/advanced-calculator/advancedCalculator.cpp +++ b/homework/advanced-calculator/advancedCalculator.cpp @@ -91,6 +91,10 @@ AdvancedCalculator::AdvancedCalculator() { ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { // 1. Sprawdź znaki for (char c : input) { + if (c == ',') { + // test 9 oczekuje BadFormat dla przecinka + return ErrorCode::BadFormat; + } if (!std::isdigit(c) && c != '+' && c != '-' && c != '*' && c != '/' && c != '.' && c != '!' && c != ' ' && c != '(' && c != ')' && c != '%' && c != '^' && c != '$') { From b48ad530c920a16dc3609131fd82abadd9fe7289 Mon Sep 17 00:00:00 2001 From: Jarek Jeda Date: Sat, 27 Sep 2025 19:11:17 +0200 Subject: [PATCH 044/104] fixes --- .../advancedCalculator.cpp | 32 +++++++++++++------ 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/homework/advanced-calculator/advancedCalculator.cpp b/homework/advanced-calculator/advancedCalculator.cpp index 924de0e67..2e2d7722f 100644 --- a/homework/advanced-calculator/advancedCalculator.cpp +++ b/homework/advanced-calculator/advancedCalculator.cpp @@ -89,16 +89,30 @@ AdvancedCalculator::AdvancedCalculator() { } ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { - // 1. Sprawdź znaki + bool inNumber = false; + bool dotUsed = false; + for (char c : input) { - if (c == ',') { - // test 9 oczekuje BadFormat dla przecinka - return ErrorCode::BadFormat; - } - if (!std::isdigit(c) && c != '+' && c != '-' && c != '*' && c != '/' && - c != '.' && c != '!' && c != ' ' && c != '(' && c != ')' && - c != '%' && c != '^' && c != '$') { - return ErrorCode::BadCharacter; + if (std::isdigit(c)) { + inNumber = true; + } else if (c == '.') { + if (dotUsed) { + return ErrorCode::BadFormat; // druga kropka w tej samej liczbie + } + dotUsed = true; + inNumber = true; + } else { + // zakończenie liczby → reset + if (inNumber) { + inNumber = false; + dotUsed = false; + } + + if (c != '+' && c != '-' && c != '*' && c != '/' && + c != '!' && c != ' ' && c != '(' && c != ')' && + c != '%' && c != '^') { + return ErrorCode::BadCharacter; // znak spoza listy + } } } From 2baece686ceaa7cb929882126b4a35175939a2e4 Mon Sep 17 00:00:00 2001 From: Jarek Jeda Date: Sat, 27 Sep 2025 19:14:38 +0200 Subject: [PATCH 045/104] fixes --- .../advancedCalculator.cpp | 97 +++++++++++-------- 1 file changed, 56 insertions(+), 41 deletions(-) diff --git a/homework/advanced-calculator/advancedCalculator.cpp b/homework/advanced-calculator/advancedCalculator.cpp index 2e2d7722f..39c9719df 100644 --- a/homework/advanced-calculator/advancedCalculator.cpp +++ b/homework/advanced-calculator/advancedCalculator.cpp @@ -89,72 +89,87 @@ AdvancedCalculator::AdvancedCalculator() { } ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { - bool inNumber = false; - bool dotUsed = false; + enum class State { Start, + Number, + Operator }; + State state = State::Start; - for (char c : input) { - if (std::isdigit(c)) { - inNumber = true; - } else if (c == '.') { - if (dotUsed) { - return ErrorCode::BadFormat; // druga kropka w tej samej liczbie - } - dotUsed = true; - inNumber = true; - } else { - // zakończenie liczby → reset - if (inNumber) { - inNumber = false; - dotUsed = false; - } + double a = 0, b = 0; + char op = 0; - if (c != '+' && c != '-' && c != '*' && c != '/' && - c != '!' && c != ' ' && c != '(' && c != ')' && - c != '%' && c != '^') { - return ErrorCode::BadCharacter; // znak spoza listy - } + std::istringstream iss(input); + std::string token; + + // Wczytaj pierwszą liczbę (może mieć znak i kropkę) + if (!(iss >> token)) + return ErrorCode::BadFormat; + + // Sprawdź format liczby + bool dotUsed = false; + for (size_t i = 0; i < token.size(); ++i) { + char c = token[i]; + if (c == '.') { + if (dotUsed) + return ErrorCode::BadFormat; + dotUsed = true; + } else if (c == '+' || c == '-') { + if (i != 0) + return ErrorCode::BadFormat; // znak tylko na początku + } else if (!std::isdigit(c)) { + return ErrorCode::BadCharacter; } } - // 2. Przygotuj parser - std::istringstream iss(input); - double a = 0, b = 0; - char op = 0; - - // liczba A - if (!(iss >> a)) + try { + a = std::stod(token); + } catch (...) { return ErrorCode::BadFormat; + } - // operator + // Wczytaj operator if (!(iss >> op)) return ErrorCode::BadFormat; if (operations.find(op) == operations.end()) return ErrorCode::BadCharacter; - // liczba B (jeśli nie jest to operacja unarna) + // Operator jednoargumentowy (!) nie wymaga drugiej liczby if (op != '!') { - if (!(iss >> b)) + if (!(iss >> token)) return ErrorCode::BadFormat; - } - // 3. Specjalne reguły formatu - // przypadek: "+8 - 32.1" -> BadFormat (liczba zaczyna się od '+') - if (!input.empty() && input[0] == '+') { - return ErrorCode::BadFormat; + dotUsed = false; + for (size_t i = 0; i < token.size(); ++i) { + char c = token[i]; + if (c == '.') { + if (dotUsed) + return ErrorCode::BadFormat; + dotUsed = true; + } else if (c == '+' || c == '-') { + if (i != 0) + return ErrorCode::BadFormat; + } else if (!std::isdigit(c)) { + return ErrorCode::BadCharacter; + } + } + + try { + b = std::stod(token); + } catch (...) { + return ErrorCode::BadFormat; + } } - // sprawdź resztę (powinny być tylko spacje) + // Sprawdź, czy reszta strumienia jest pusta lub same spacje std::string rest; if (std::getline(iss, rest)) { for (char c : rest) { - if (!std::isspace(c)) { + if (!std::isspace(c)) return ErrorCode::BadFormat; - } } } - // 4. Wykonaj działanie + // Wywołanie funkcji operacji return operations.at(op)(a, b, out); } From eb7f0e6adbfea4620d875fcb078ebae86c3dfe09 Mon Sep 17 00:00:00 2001 From: Jarek Jeda Date: Sat, 27 Sep 2025 19:18:50 +0200 Subject: [PATCH 046/104] fixes --- .../advancedCalculator.cpp | 81 ++++++------------- 1 file changed, 23 insertions(+), 58 deletions(-) diff --git a/homework/advanced-calculator/advancedCalculator.cpp b/homework/advanced-calculator/advancedCalculator.cpp index 39c9719df..651c0bb07 100644 --- a/homework/advanced-calculator/advancedCalculator.cpp +++ b/homework/advanced-calculator/advancedCalculator.cpp @@ -89,87 +89,52 @@ AdvancedCalculator::AdvancedCalculator() { } ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { - enum class State { Start, - Number, - Operator }; - State state = State::Start; - - double a = 0, b = 0; - char op = 0; - - std::istringstream iss(input); - std::string token; - - // Wczytaj pierwszą liczbę (może mieć znak i kropkę) - if (!(iss >> token)) - return ErrorCode::BadFormat; - - // Sprawdź format liczby - bool dotUsed = false; - for (size_t i = 0; i < token.size(); ++i) { - char c = token[i]; - if (c == '.') { - if (dotUsed) - return ErrorCode::BadFormat; - dotUsed = true; - } else if (c == '+' || c == '-') { - if (i != 0) - return ErrorCode::BadFormat; // znak tylko na początku - } else if (!std::isdigit(c)) { + // 1. Sprawdzenie niedozwolonych znaków + for (char c : input) { + if (!std::isdigit(c) && c != '+' && c != '-' && c != '*' && c != '/' && + c != '.' && c != '!' && c != ' ' && c != '(' && c != ')' && + c != '%' && c != '^' && c != '$') { return ErrorCode::BadCharacter; } } - try { - a = std::stod(token); - } catch (...) { + std::istringstream iss(input); + double a = 0.0, b = 0.0; + char op = 0; + + // 2. Wczytaj pierwszą liczbę (ze znakiem, jeśli jest na początku) + if (!(iss >> a)) { return ErrorCode::BadFormat; } - // Wczytaj operator - if (!(iss >> op)) + // 3. Wczytaj operator + if (!(iss >> op)) { + // Jeśli operator nie istnieje i jest końcem stringa, OK tylko dla '!' return ErrorCode::BadFormat; + } - if (operations.find(op) == operations.end()) + if (operations.find(op) == operations.end()) { return ErrorCode::BadCharacter; + } - // Operator jednoargumentowy (!) nie wymaga drugiej liczby + // 4. Wczytaj drugą liczbę jeśli operator wymaga if (op != '!') { - if (!(iss >> token)) - return ErrorCode::BadFormat; - - dotUsed = false; - for (size_t i = 0; i < token.size(); ++i) { - char c = token[i]; - if (c == '.') { - if (dotUsed) - return ErrorCode::BadFormat; - dotUsed = true; - } else if (c == '+' || c == '-') { - if (i != 0) - return ErrorCode::BadFormat; - } else if (!std::isdigit(c)) { - return ErrorCode::BadCharacter; - } - } - - try { - b = std::stod(token); - } catch (...) { + if (!(iss >> b)) { return ErrorCode::BadFormat; } } - // Sprawdź, czy reszta strumienia jest pusta lub same spacje + // 5. Sprawdzenie reszty stringa – tylko spacje są dozwolone std::string rest; if (std::getline(iss, rest)) { for (char c : rest) { - if (!std::isspace(c)) + if (!std::isspace(c)) { return ErrorCode::BadFormat; + } } } - // Wywołanie funkcji operacji + // 6. Wywołanie odpowiedniej funkcji operacji return operations.at(op)(a, b, out); } From b2697e91b45b98762d40f737c483efb2c18b1e85 Mon Sep 17 00:00:00 2001 From: Jarek Jeda Date: Sat, 27 Sep 2025 19:22:51 +0200 Subject: [PATCH 047/104] fixes --- homework/advanced-calculator/advancedCalculator.cpp | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/homework/advanced-calculator/advancedCalculator.cpp b/homework/advanced-calculator/advancedCalculator.cpp index 651c0bb07..56980e0fa 100644 --- a/homework/advanced-calculator/advancedCalculator.cpp +++ b/homework/advanced-calculator/advancedCalculator.cpp @@ -91,13 +91,19 @@ AdvancedCalculator::AdvancedCalculator() { ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { // 1. Sprawdzenie niedozwolonych znaków for (char c : input) { + // Złe znaki, które nigdy nie powinny się pojawić if (!std::isdigit(c) && c != '+' && c != '-' && c != '*' && c != '/' && c != '.' && c != '!' && c != ' ' && c != '(' && c != ')' && - c != '%' && c != '^' && c != '$') { - return ErrorCode::BadCharacter; + c != '%' && c != '^' && c != '$' && c != ',') { + return ErrorCode::BadCharacter; // test 10 nadal przejdzie } } + // Jeśli w liczbie pojawił się przecinek, to jest złe formatowanie + if (input.find(',') != std::string::npos) { + return ErrorCode::BadFormat; + } + std::istringstream iss(input); double a = 0.0, b = 0.0; char op = 0; From bf6b39b62f51434936b605b05f7750650d6eb784 Mon Sep 17 00:00:00 2001 From: Jarek Jeda Date: Sat, 27 Sep 2025 19:27:48 +0200 Subject: [PATCH 048/104] fixes --- .../advanced-calculator/advancedCalculator.cpp | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-) diff --git a/homework/advanced-calculator/advancedCalculator.cpp b/homework/advanced-calculator/advancedCalculator.cpp index 56980e0fa..c1de19778 100644 --- a/homework/advanced-calculator/advancedCalculator.cpp +++ b/homework/advanced-calculator/advancedCalculator.cpp @@ -89,17 +89,14 @@ AdvancedCalculator::AdvancedCalculator() { } ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { - // 1. Sprawdzenie niedozwolonych znaków for (char c : input) { - // Złe znaki, które nigdy nie powinny się pojawić if (!std::isdigit(c) && c != '+' && c != '-' && c != '*' && c != '/' && c != '.' && c != '!' && c != ' ' && c != '(' && c != ')' && c != '%' && c != '^' && c != '$' && c != ',') { - return ErrorCode::BadCharacter; // test 10 nadal przejdzie + return ErrorCode::BadCharacter; } } - // Jeśli w liczbie pojawił się przecinek, to jest złe formatowanie if (input.find(',') != std::string::npos) { return ErrorCode::BadFormat; } @@ -108,14 +105,10 @@ ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { double a = 0.0, b = 0.0; char op = 0; - // 2. Wczytaj pierwszą liczbę (ze znakiem, jeśli jest na początku) if (!(iss >> a)) { return ErrorCode::BadFormat; } - - // 3. Wczytaj operator if (!(iss >> op)) { - // Jeśli operator nie istnieje i jest końcem stringa, OK tylko dla '!' return ErrorCode::BadFormat; } @@ -123,14 +116,11 @@ ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { return ErrorCode::BadCharacter; } - // 4. Wczytaj drugą liczbę jeśli operator wymaga if (op != '!') { - if (!(iss >> b)) { + if (!(iss >> b)) return ErrorCode::BadFormat; - } } - // 5. Sprawdzenie reszty stringa – tylko spacje są dozwolone std::string rest; if (std::getline(iss, rest)) { for (char c : rest) { @@ -139,8 +129,6 @@ ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { } } } - - // 6. Wywołanie odpowiedniej funkcji operacji return operations.at(op)(a, b, out); } From 6e438c75eb9db3659b4509dc56559dee763357f1 Mon Sep 17 00:00:00 2001 From: Jarek Jeda Date: Sat, 27 Sep 2025 19:32:20 +0200 Subject: [PATCH 049/104] fixes --- .../advanced-calculator/advancedCalculator.cpp | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/homework/advanced-calculator/advancedCalculator.cpp b/homework/advanced-calculator/advancedCalculator.cpp index c1de19778..c95f22de9 100644 --- a/homework/advanced-calculator/advancedCalculator.cpp +++ b/homework/advanced-calculator/advancedCalculator.cpp @@ -117,8 +117,22 @@ ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { } if (op != '!') { - if (!(iss >> b)) + // Pobieramy pozycję w strumieniu, żeby sprawdzić pierwszy znak drugiej liczby + std::streampos pos = iss.tellg(); + std::string remainder; + iss >> remainder; + if (remainder.empty()) { return ErrorCode::BadFormat; + } + // Jeśli druga liczba zaczyna się od + lub -, a nie jest częścią operatora, to błąd + if (remainder[0] == '+' || remainder[0] == '-') { + return ErrorCode::BadFormat; + } + // Wczytujemy drugą liczbę + std::istringstream iss2(remainder); + if (!(iss2 >> b)) { + return ErrorCode::BadFormat; + } } std::string rest; From 718e2fe74c74420e3852492c6f1a6f34367b9969 Mon Sep 17 00:00:00 2001 From: Jarek Jeda Date: Sat, 27 Sep 2025 19:34:07 +0200 Subject: [PATCH 050/104] fixes2 --- .../advancedCalculator.cpp | 55 +++++++++---------- 1 file changed, 26 insertions(+), 29 deletions(-) diff --git a/homework/advanced-calculator/advancedCalculator.cpp b/homework/advanced-calculator/advancedCalculator.cpp index c95f22de9..03cce9893 100644 --- a/homework/advanced-calculator/advancedCalculator.cpp +++ b/homework/advanced-calculator/advancedCalculator.cpp @@ -89,14 +89,16 @@ AdvancedCalculator::AdvancedCalculator() { } ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { + // Sprawdzenie niedozwolonych znaków for (char c : input) { if (!std::isdigit(c) && c != '+' && c != '-' && c != '*' && c != '/' && - c != '.' && c != '!' && c != ' ' && c != '(' && c != ')' && - c != '%' && c != '^' && c != '$' && c != ',') { + c != '.' && c != '!' && c != ' ' && + c != '%' && c != '^' && c != '$') { return ErrorCode::BadCharacter; } } + // Niedozwolone przecinki if (input.find(',') != std::string::npos) { return ErrorCode::BadFormat; } @@ -105,44 +107,39 @@ ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { double a = 0.0, b = 0.0; char op = 0; - if (!(iss >> a)) { + // Wczytanie pierwszej liczby + if (!(iss >> a)) return ErrorCode::BadFormat; - } - if (!(iss >> op)) { - return ErrorCode::BadFormat; - } - if (operations.find(op) == operations.end()) { + // Wczytanie operatora + if (!(iss >> op)) + return ErrorCode::BadFormat; + if (operations.find(op) == operations.end()) return ErrorCode::BadCharacter; - } + // W przypadku operatorów binarnych if (op != '!') { - // Pobieramy pozycję w strumieniu, żeby sprawdzić pierwszy znak drugiej liczby - std::streampos pos = iss.tellg(); - std::string remainder; - iss >> remainder; - if (remainder.empty()) { - return ErrorCode::BadFormat; - } - // Jeśli druga liczba zaczyna się od + lub -, a nie jest częścią operatora, to błąd - if (remainder[0] == '+' || remainder[0] == '-') { - return ErrorCode::BadFormat; - } - // Wczytujemy drugą liczbę - std::istringstream iss2(remainder); - if (!(iss2 >> b)) { + // Wczytanie drugiej liczby + if (!(iss >> b)) return ErrorCode::BadFormat; - } - } - std::string rest; - if (std::getline(iss, rest)) { + // Sprawdzenie, czy po drugiej liczbie nie ma dodatkowych znaków (poza spacjami) + std::string rest; + std::getline(iss, rest); for (char c : rest) { - if (!std::isspace(c)) { + if (!std::isspace(c)) + return ErrorCode::BadFormat; + } + } else { + // Operator ! – po nim nie może być nic poza spacjami + std::string rest; + std::getline(iss, rest); + for (char c : rest) { + if (!std::isspace(c)) return ErrorCode::BadFormat; - } } } + return operations.at(op)(a, b, out); } From 3853f67be7dea694f7d97e61338b514966ff45cc Mon Sep 17 00:00:00 2001 From: Jarek Jeda Date: Sat, 27 Sep 2025 19:37:28 +0200 Subject: [PATCH 051/104] fixes2 --- .../advancedCalculator.cpp | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/homework/advanced-calculator/advancedCalculator.cpp b/homework/advanced-calculator/advancedCalculator.cpp index 03cce9893..857a25d94 100644 --- a/homework/advanced-calculator/advancedCalculator.cpp +++ b/homework/advanced-calculator/advancedCalculator.cpp @@ -117,21 +117,21 @@ ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { if (operations.find(op) == operations.end()) return ErrorCode::BadCharacter; - // W przypadku operatorów binarnych if (op != '!') { - // Wczytanie drugiej liczby - if (!(iss >> b)) + // wczytanie drugiej liczby + std::string token; + if (!(iss >> token)) return ErrorCode::BadFormat; - // Sprawdzenie, czy po drugiej liczbie nie ma dodatkowych znaków (poza spacjami) - std::string rest; - std::getline(iss, rest); - for (char c : rest) { - if (!std::isspace(c)) - return ErrorCode::BadFormat; - } - } else { - // Operator ! – po nim nie może być nic poza spacjami + // jeśli druga liczba zaczyna się od + lub -, traktujemy jako błąd formatu + if (token[0] == '+' || token[0] == '-') + return ErrorCode::BadFormat; + + std::istringstream iss2(token); + if (!(iss2 >> b)) + return ErrorCode::BadFormat; + + // sprawdzamy resztę std::string rest; std::getline(iss, rest); for (char c : rest) { From 765363b0d82cda8cfa3a1890c527dc9461090146 Mon Sep 17 00:00:00 2001 From: Jarek Jeda Date: Sat, 27 Sep 2025 19:46:44 +0200 Subject: [PATCH 052/104] fixes2 --- .../advancedCalculator.cpp | 25 ++++++------------- 1 file changed, 8 insertions(+), 17 deletions(-) diff --git a/homework/advanced-calculator/advancedCalculator.cpp b/homework/advanced-calculator/advancedCalculator.cpp index 857a25d94..6fb3c4e4a 100644 --- a/homework/advanced-calculator/advancedCalculator.cpp +++ b/homework/advanced-calculator/advancedCalculator.cpp @@ -117,27 +117,18 @@ ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { if (operations.find(op) == operations.end()) return ErrorCode::BadCharacter; + // Operator binarny if (op != '!') { - // wczytanie drugiej liczby - std::string token; - if (!(iss >> token)) - return ErrorCode::BadFormat; - - // jeśli druga liczba zaczyna się od + lub -, traktujemy jako błąd formatu - if (token[0] == '+' || token[0] == '-') + if (!(iss >> b)) return ErrorCode::BadFormat; + } - std::istringstream iss2(token); - if (!(iss2 >> b)) + // Sprawdzenie, czy po wczytaniu liczb nie ma dodatkowych znaków + std::string rest; + std::getline(iss, rest); + for (char c : rest) { + if (!std::isspace(c)) return ErrorCode::BadFormat; - - // sprawdzamy resztę - std::string rest; - std::getline(iss, rest); - for (char c : rest) { - if (!std::isspace(c)) - return ErrorCode::BadFormat; - } } return operations.at(op)(a, b, out); From df3eb741ee67bdcc0062fbbe471cb5d52e5bed7a Mon Sep 17 00:00:00 2001 From: Jarek Jeda Date: Sat, 27 Sep 2025 19:48:52 +0200 Subject: [PATCH 053/104] fixes2 --- homework/advanced-calculator/advancedCalculator.cpp | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/homework/advanced-calculator/advancedCalculator.cpp b/homework/advanced-calculator/advancedCalculator.cpp index 6fb3c4e4a..874e5e28c 100644 --- a/homework/advanced-calculator/advancedCalculator.cpp +++ b/homework/advanced-calculator/advancedCalculator.cpp @@ -119,11 +119,16 @@ ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { // Operator binarny if (op != '!') { - if (!(iss >> b)) + // Wczytanie drugiej liczby (uwzględnia znak +/-) + std::string token; + if (!(iss >> token)) + return ErrorCode::BadFormat; + std::istringstream iss2(token); + if (!(iss2 >> b)) return ErrorCode::BadFormat; } - // Sprawdzenie, czy po wczytaniu liczb nie ma dodatkowych znaków + // Sprawdzenie, czy po wczytaniu liczb nie ma dodatkowych znaków (poza spacjami) std::string rest; std::getline(iss, rest); for (char c : rest) { From 691bd6ed77bf9e7293f13df4343710f90b364c82 Mon Sep 17 00:00:00 2001 From: Jarek Jeda Date: Sat, 27 Sep 2025 19:50:49 +0200 Subject: [PATCH 054/104] fixes2 --- .../advanced-calculator/advancedCalculator.cpp | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/homework/advanced-calculator/advancedCalculator.cpp b/homework/advanced-calculator/advancedCalculator.cpp index 874e5e28c..7a7814857 100644 --- a/homework/advanced-calculator/advancedCalculator.cpp +++ b/homework/advanced-calculator/advancedCalculator.cpp @@ -117,18 +117,19 @@ ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { if (operations.find(op) == operations.end()) return ErrorCode::BadCharacter; - // Operator binarny if (op != '!') { - // Wczytanie drugiej liczby (uwzględnia znak +/-) - std::string token; - if (!(iss >> token)) - return ErrorCode::BadFormat; - std::istringstream iss2(token); - if (!(iss2 >> b)) + // Wczytanie drugiej liczby (łącznie ze znakiem + lub -) + char sign = '+'; + if (iss.peek() == '+' || iss.peek() == '-') { + iss >> sign; + } + if (!(iss >> b)) return ErrorCode::BadFormat; + if (sign == '-') + b = -b; } - // Sprawdzenie, czy po wczytaniu liczb nie ma dodatkowych znaków (poza spacjami) + // Sprawdzenie, czy po operacji nie ma dodatkowych znaków std::string rest; std::getline(iss, rest); for (char c : rest) { From d58ac45d78418e08478ca80dd5228ed4b3a0d453 Mon Sep 17 00:00:00 2001 From: Jarek Jeda Date: Sat, 27 Sep 2025 20:04:47 +0200 Subject: [PATCH 055/104] fixes2 --- .../advancedCalculator.cpp | 21 +++++++++---------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/homework/advanced-calculator/advancedCalculator.cpp b/homework/advanced-calculator/advancedCalculator.cpp index 7a7814857..831411b92 100644 --- a/homework/advanced-calculator/advancedCalculator.cpp +++ b/homework/advanced-calculator/advancedCalculator.cpp @@ -107,29 +107,28 @@ ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { double a = 0.0, b = 0.0; char op = 0; - // Wczytanie pierwszej liczby + // Pierwsza liczba if (!(iss >> a)) return ErrorCode::BadFormat; - // Wczytanie operatora + // Operator if (!(iss >> op)) return ErrorCode::BadFormat; if (operations.find(op) == operations.end()) return ErrorCode::BadCharacter; if (op != '!') { - // Wczytanie drugiej liczby (łącznie ze znakiem + lub -) - char sign = '+'; - if (iss.peek() == '+' || iss.peek() == '-') { - iss >> sign; - } - if (!(iss >> b)) + // Druga liczba może mieć znak + std::string token; + if (!(iss >> token)) + return ErrorCode::BadFormat; + + std::istringstream iss2(token); + if (!(iss2 >> b)) return ErrorCode::BadFormat; - if (sign == '-') - b = -b; } - // Sprawdzenie, czy po operacji nie ma dodatkowych znaków + // Sprawdzenie reszty ciągu std::string rest; std::getline(iss, rest); for (char c : rest) { From 1cfd9610ccbf77a4328a963de0008c08b4431387 Mon Sep 17 00:00:00 2001 From: Jarek Jeda Date: Sat, 27 Sep 2025 20:14:50 +0200 Subject: [PATCH 056/104] fixes2 --- homework/advanced-calculator/advancedCalculator.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/homework/advanced-calculator/advancedCalculator.cpp b/homework/advanced-calculator/advancedCalculator.cpp index 831411b92..4808af4bb 100644 --- a/homework/advanced-calculator/advancedCalculator.cpp +++ b/homework/advanced-calculator/advancedCalculator.cpp @@ -90,11 +90,11 @@ AdvancedCalculator::AdvancedCalculator() { ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { // Sprawdzenie niedozwolonych znaków - for (char c : input) { + for (unsigned char c : input) { if (!std::isdigit(c) && c != '+' && c != '-' && c != '*' && c != '/' && c != '.' && c != '!' && c != ' ' && c != '%' && c != '^' && c != '$') { - return ErrorCode::BadCharacter; + return (c == ',' ? ErrorCode::BadFormat : ErrorCode::BadCharacter); } } From 89d61c2af082618943daae768669ddc61ee2202b Mon Sep 17 00:00:00 2001 From: Jarek Jeda Date: Sat, 27 Sep 2025 20:16:37 +0200 Subject: [PATCH 057/104] fixes2 --- homework/advanced-calculator/advancedCalculator.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/homework/advanced-calculator/advancedCalculator.cpp b/homework/advanced-calculator/advancedCalculator.cpp index 4808af4bb..c7cea9b06 100644 --- a/homework/advanced-calculator/advancedCalculator.cpp +++ b/homework/advanced-calculator/advancedCalculator.cpp @@ -94,7 +94,10 @@ ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { if (!std::isdigit(c) && c != '+' && c != '-' && c != '*' && c != '/' && c != '.' && c != '!' && c != ' ' && c != '%' && c != '^' && c != '$') { - return (c == ',' ? ErrorCode::BadFormat : ErrorCode::BadCharacter); + // jeśli trafi się przecinek → BadFormat + if (c == ',') + return ErrorCode::BadFormat; + return ErrorCode::BadCharacter; } } From 3fd3f23cf72db9730d418c7b589fecee99f1e70d Mon Sep 17 00:00:00 2001 From: Jarek Jeda Date: Sat, 27 Sep 2025 20:19:08 +0200 Subject: [PATCH 058/104] fixes42 --- .../advancedCalculator.cpp | 44 ++++++++++++------- 1 file changed, 28 insertions(+), 16 deletions(-) diff --git a/homework/advanced-calculator/advancedCalculator.cpp b/homework/advanced-calculator/advancedCalculator.cpp index c7cea9b06..fb903ef76 100644 --- a/homework/advanced-calculator/advancedCalculator.cpp +++ b/homework/advanced-calculator/advancedCalculator.cpp @@ -90,18 +90,15 @@ AdvancedCalculator::AdvancedCalculator() { ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { // Sprawdzenie niedozwolonych znaków - for (unsigned char c : input) { + for (char c : input) { if (!std::isdigit(c) && c != '+' && c != '-' && c != '*' && c != '/' && c != '.' && c != '!' && c != ' ' && c != '%' && c != '^' && c != '$') { - // jeśli trafi się przecinek → BadFormat - if (c == ',') - return ErrorCode::BadFormat; return ErrorCode::BadCharacter; } } - // Niedozwolone przecinki + // Przecinek = zły format if (input.find(',') != std::string::npos) { return ErrorCode::BadFormat; } @@ -117,29 +114,44 @@ ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { // Operator if (!(iss >> op)) return ErrorCode::BadFormat; + if (operations.find(op) == operations.end()) return ErrorCode::BadCharacter; - if (op != '!') { - // Druga liczba może mieć znak + if (op == '!') { + // factorial musi być ostatni w napisie + std::string rest; + std::getline(iss, rest); + for (char c : rest) { + if (!std::isspace(c)) + return ErrorCode::BadFormat; + } + return operations.at(op)(a, 0.0, out); + } else { + // Wczytanie drugiej liczby (może mieć + / -) std::string token; if (!(iss >> token)) return ErrorCode::BadFormat; + // drugi operator od razu po pierwszym → BadFormat (np. "++", "^%") + if (token.size() > 1 && (token[0] == '+' || token[0] == '-') && (token[1] == '+' || token[1] == '-' || token[1] == '*' || token[1] == '/' || token[1] == '^' || token[1] == '%' || token[1] == '$' || token[1] == '!')) { + return ErrorCode::BadFormat; + } + std::istringstream iss2(token); if (!(iss2 >> b)) return ErrorCode::BadFormat; - } - // Sprawdzenie reszty ciągu - std::string rest; - std::getline(iss, rest); - for (char c : rest) { - if (!std::isspace(c)) - return ErrorCode::BadFormat; - } + // sprawdź czy nie ma jeszcze czegoś po drugiej liczbie + std::string rest; + std::getline(iss, rest); + for (char c : rest) { + if (!std::isspace(c)) + return ErrorCode::BadFormat; + } - return operations.at(op)(a, b, out); + return operations.at(op)(a, b, out); + } } ErrorCode process(const std::string& input, double* out) { From fb4482451875f46e341ae062c0f204ce013fb4f7 Mon Sep 17 00:00:00 2001 From: Jarek Jeda Date: Sat, 27 Sep 2025 20:22:05 +0200 Subject: [PATCH 059/104] fixes4 --- .../advancedCalculator.cpp | 31 ++++++------------- 1 file changed, 10 insertions(+), 21 deletions(-) diff --git a/homework/advanced-calculator/advancedCalculator.cpp b/homework/advanced-calculator/advancedCalculator.cpp index fb903ef76..22a5a07d2 100644 --- a/homework/advanced-calculator/advancedCalculator.cpp +++ b/homework/advanced-calculator/advancedCalculator.cpp @@ -89,7 +89,12 @@ AdvancedCalculator::AdvancedCalculator() { } ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { - // Sprawdzenie niedozwolonych znaków + // Najpierw sprawdzamy przecinek – to zawsze BadFormat + if (input.find(',') != std::string::npos) { + return ErrorCode::BadFormat; + } + + // Sprawdzenie innych niedozwolonych znaków for (char c : input) { if (!std::isdigit(c) && c != '+' && c != '-' && c != '*' && c != '/' && c != '.' && c != '!' && c != ' ' && @@ -98,11 +103,6 @@ ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { } } - // Przecinek = zły format - if (input.find(',') != std::string::npos) { - return ErrorCode::BadFormat; - } - std::istringstream iss(input); double a = 0.0, b = 0.0; char op = 0; @@ -119,7 +119,7 @@ ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { return ErrorCode::BadCharacter; if (op == '!') { - // factorial musi być ostatni w napisie + // factorial – po nim już nic std::string rest; std::getline(iss, rest); for (char c : rest) { @@ -128,28 +128,17 @@ ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { } return operations.at(op)(a, 0.0, out); } else { - // Wczytanie drugiej liczby (może mieć + / -) - std::string token; - if (!(iss >> token)) - return ErrorCode::BadFormat; - - // drugi operator od razu po pierwszym → BadFormat (np. "++", "^%") - if (token.size() > 1 && (token[0] == '+' || token[0] == '-') && (token[1] == '+' || token[1] == '-' || token[1] == '*' || token[1] == '/' || token[1] == '^' || token[1] == '%' || token[1] == '$' || token[1] == '!')) { + // druga liczba + if (!(iss >> b)) return ErrorCode::BadFormat; - } - std::istringstream iss2(token); - if (!(iss2 >> b)) - return ErrorCode::BadFormat; - - // sprawdź czy nie ma jeszcze czegoś po drugiej liczbie + // po drugiej liczbie już nic poza spacjami std::string rest; std::getline(iss, rest); for (char c : rest) { if (!std::isspace(c)) return ErrorCode::BadFormat; } - return operations.at(op)(a, b, out); } } From d26c7bc6eed444fcc3f2f30199d52060bd7b1f4b Mon Sep 17 00:00:00 2001 From: Jarek Jeda Date: Sat, 27 Sep 2025 20:28:27 +0200 Subject: [PATCH 060/104] fixes6 --- .../advancedCalculator.cpp | 44 ++++++++++++++----- 1 file changed, 32 insertions(+), 12 deletions(-) diff --git a/homework/advanced-calculator/advancedCalculator.cpp b/homework/advanced-calculator/advancedCalculator.cpp index 22a5a07d2..de51c81be 100644 --- a/homework/advanced-calculator/advancedCalculator.cpp +++ b/homework/advanced-calculator/advancedCalculator.cpp @@ -89,29 +89,40 @@ AdvancedCalculator::AdvancedCalculator() { } ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { - // Najpierw sprawdzamy przecinek – to zawsze BadFormat - if (input.find(',') != std::string::npos) { + if (!out) { return ErrorCode::BadFormat; } - // Sprawdzenie innych niedozwolonych znaków + bool hasComma = false; + bool hasOtherInvalid = false; + + // Sprawdzanie niedozwolonych znaków for (char c : input) { - if (!std::isdigit(c) && c != '+' && c != '-' && c != '*' && c != '/' && - c != '.' && c != '!' && c != ' ' && - c != '%' && c != '^' && c != '$') { - return ErrorCode::BadCharacter; + if (c == ',') { + hasComma = true; + } else if (!std::isdigit(c) && c != '+' && c != '-' && c != '*' && + c != '/' && c != '.' && c != '!' && c != ' ' && + c != '%' && c != '^' && c != '$') { + hasOtherInvalid = true; } } + if (hasOtherInvalid) { + return ErrorCode::BadCharacter; // priorytet dla innych złych znaków + } + if (hasComma) { + return ErrorCode::BadFormat; // tylko jeśli nie było innych złych znaków + } + std::istringstream iss(input); double a = 0.0, b = 0.0; char op = 0; - // Pierwsza liczba + // Wczytanie pierwszej liczby if (!(iss >> a)) return ErrorCode::BadFormat; - // Operator + // Wczytanie operatora if (!(iss >> op)) return ErrorCode::BadFormat; @@ -119,7 +130,7 @@ ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { return ErrorCode::BadCharacter; if (op == '!') { - // factorial – po nim już nic + // factorial – po nim nie powinno być nic std::string rest; std::getline(iss, rest); for (char c : rest) { @@ -128,17 +139,26 @@ ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { } return operations.at(op)(a, 0.0, out); } else { - // druga liczba + // Wczytanie drugiej liczby (może mieć znak +/-) + char sign = '+'; + if (iss.peek() == '+' || iss.peek() == '-') { + iss >> sign; + } + if (!(iss >> b)) return ErrorCode::BadFormat; - // po drugiej liczbie już nic poza spacjami + if (sign == '-') + b = -b; + + // Po drugiej liczbie nie powinno być nic poza spacjami std::string rest; std::getline(iss, rest); for (char c : rest) { if (!std::isspace(c)) return ErrorCode::BadFormat; } + return operations.at(op)(a, b, out); } } From 0315a533aa80ba69b6f292f11c6a2afef52664f4 Mon Sep 17 00:00:00 2001 From: Jarek Jeda Date: Sat, 27 Sep 2025 20:31:38 +0200 Subject: [PATCH 061/104] fixes6 --- homework/advanced-calculator/advancedCalculator.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/homework/advanced-calculator/advancedCalculator.cpp b/homework/advanced-calculator/advancedCalculator.cpp index de51c81be..124f17277 100644 --- a/homework/advanced-calculator/advancedCalculator.cpp +++ b/homework/advanced-calculator/advancedCalculator.cpp @@ -142,7 +142,11 @@ ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { // Wczytanie drugiej liczby (może mieć znak +/-) char sign = '+'; if (iss.peek() == '+' || iss.peek() == '-') { - iss >> sign; + char next = iss.peek(); + // jeśli następny znak jest też operatorem i nie jest częścią liczby, to BadFormat + if (next == op) { + return ErrorCode::BadFormat; + } } if (!(iss >> b)) From e87caa31455ed254ce70273f7fbb981cfdf1f70c Mon Sep 17 00:00:00 2001 From: Jarek Jeda Date: Sat, 27 Sep 2025 20:36:56 +0200 Subject: [PATCH 062/104] fixes6 --- .../advancedCalculator.cpp | 94 ++++++++++--------- 1 file changed, 51 insertions(+), 43 deletions(-) diff --git a/homework/advanced-calculator/advancedCalculator.cpp b/homework/advanced-calculator/advancedCalculator.cpp index 124f17277..bbfe984d6 100644 --- a/homework/advanced-calculator/advancedCalculator.cpp +++ b/homework/advanced-calculator/advancedCalculator.cpp @@ -89,48 +89,44 @@ AdvancedCalculator::AdvancedCalculator() { } ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { - if (!out) { + if (!out) return ErrorCode::BadFormat; - } - - bool hasComma = false; - bool hasOtherInvalid = false; - // Sprawdzanie niedozwolonych znaków + // Sprawdzenie niedozwolonych znaków for (char c : input) { - if (c == ',') { - hasComma = true; - } else if (!std::isdigit(c) && c != '+' && c != '-' && c != '*' && - c != '/' && c != '.' && c != '!' && c != ' ' && - c != '%' && c != '^' && c != '$') { - hasOtherInvalid = true; + if (!std::isdigit(c) && c != '+' && c != '-' && c != '*' && c != '/' && + c != '.' && c != '!' && c != ' ' && c != '%' && c != '^' && c != '$') { + return ErrorCode::BadCharacter; } } - if (hasOtherInvalid) { - return ErrorCode::BadCharacter; // priorytet dla innych złych znaków - } - if (hasComma) { - return ErrorCode::BadFormat; // tylko jeśli nie było innych złych znaków + // Niedozwolone przecinki + if (input.find(',') != std::string::npos) { + return ErrorCode::BadFormat; } std::istringstream iss(input); + std::string token; double a = 0.0, b = 0.0; char op = 0; - // Wczytanie pierwszej liczby - if (!(iss >> a)) + // Wczytanie pierwszej liczby wraz ze znakiem + lub - + if (!(iss >> token)) return ErrorCode::BadFormat; + try { + a = std::stod(token); + } catch (...) { + return ErrorCode::BadFormat; + } // Wczytanie operatora if (!(iss >> op)) return ErrorCode::BadFormat; - if (operations.find(op) == operations.end()) return ErrorCode::BadCharacter; if (op == '!') { - // factorial – po nim nie powinno być nic + // factorial – po nim nie może być nic poza spacjami std::string rest; std::getline(iss, rest); for (char c : rest) { @@ -138,33 +134,45 @@ ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { return ErrorCode::BadFormat; } return operations.at(op)(a, 0.0, out); - } else { - // Wczytanie drugiej liczby (może mieć znak +/-) - char sign = '+'; - if (iss.peek() == '+' || iss.peek() == '-') { - char next = iss.peek(); - // jeśli następny znak jest też operatorem i nie jest częścią liczby, to BadFormat - if (next == op) { - return ErrorCode::BadFormat; - } - } - - if (!(iss >> b)) - return ErrorCode::BadFormat; + } - if (sign == '-') - b = -b; + // Wczytanie drugiej liczby wraz ze znakiem + lub - + if (!(iss >> token)) + return ErrorCode::BadFormat; - // Po drugiej liczbie nie powinno być nic poza spacjami - std::string rest; - std::getline(iss, rest); - for (char c : rest) { - if (!std::isspace(c)) - return ErrorCode::BadFormat; + // Obsługa sytuacji typu "--77.321" lub "+32.1" + if (token.size() > 1 && (token[0] == '+' || token[0] == '-') && + (token[1] == '+' || token[1] == '-')) { + // podwójny operator, np. "++" lub "--" + // przekształcamy na pojedynczy znak: -- -> +, ++ -> + + int sign = 1; + for (char c : token) { + if (c == '-') + sign *= -1; + } + try { + b = std::stod(token.substr(token.find_first_of("0123456789."))); + b *= sign; + } catch (...) { + return ErrorCode::BadFormat; + } + } else { + try { + b = std::stod(token); + } catch (...) { + return ErrorCode::BadFormat; } + } - return operations.at(op)(a, b, out); + // Sprawdzenie, czy po drugiej liczbie nie ma dodatkowych znaków + std::string rest; + std::getline(iss, rest); + for (char c : rest) { + if (!std::isspace(c)) + return ErrorCode::BadFormat; } + + return operations.at(op)(a, b, out); } ErrorCode process(const std::string& input, double* out) { From 3911d6cdba38d4abb6854c7f63f05fc4c66fc1d8 Mon Sep 17 00:00:00 2001 From: Jarek Jeda Date: Sat, 27 Sep 2025 20:38:31 +0200 Subject: [PATCH 063/104] fixes6 --- .../advancedCalculator.cpp | 60 +++++++++---------- 1 file changed, 28 insertions(+), 32 deletions(-) diff --git a/homework/advanced-calculator/advancedCalculator.cpp b/homework/advanced-calculator/advancedCalculator.cpp index bbfe984d6..4b9a06d5e 100644 --- a/homework/advanced-calculator/advancedCalculator.cpp +++ b/homework/advanced-calculator/advancedCalculator.cpp @@ -92,29 +92,29 @@ ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { if (!out) return ErrorCode::BadFormat; - // Sprawdzenie niedozwolonych znaków + // Sprawdzenie niedozwolonych znaków i przecinków for (char c : input) { + if (c == ',') + return ErrorCode::BadFormat; if (!std::isdigit(c) && c != '+' && c != '-' && c != '*' && c != '/' && c != '.' && c != '!' && c != ' ' && c != '%' && c != '^' && c != '$') { return ErrorCode::BadCharacter; } } - // Niedozwolone przecinki - if (input.find(',') != std::string::npos) { - return ErrorCode::BadFormat; - } - std::istringstream iss(input); std::string token; double a = 0.0, b = 0.0; char op = 0; - // Wczytanie pierwszej liczby wraz ze znakiem + lub - + // Wczytanie pierwszej liczby (z możliwym znakiem + lub -) if (!(iss >> token)) return ErrorCode::BadFormat; try { - a = std::stod(token); + size_t pos; + a = std::stod(token, &pos); + if (pos != token.size()) + return ErrorCode::BadFormat; // np. "12.4.3" } catch (...) { return ErrorCode::BadFormat; } @@ -126,7 +126,7 @@ ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { return ErrorCode::BadCharacter; if (op == '!') { - // factorial – po nim nie może być nic poza spacjami + // Factorial – po nim nie może być nic oprócz spacji std::string rest; std::getline(iss, rest); for (char c : rest) { @@ -136,32 +136,28 @@ ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { return operations.at(op)(a, 0.0, out); } - // Wczytanie drugiej liczby wraz ze znakiem + lub - + // Wczytanie drugiej liczby (obsługa prefiksu + lub -) if (!(iss >> token)) return ErrorCode::BadFormat; - // Obsługa sytuacji typu "--77.321" lub "+32.1" - if (token.size() > 1 && (token[0] == '+' || token[0] == '-') && - (token[1] == '+' || token[1] == '-')) { - // podwójny operator, np. "++" lub "--" - // przekształcamy na pojedynczy znak: -- -> +, ++ -> + - int sign = 1; - for (char c : token) { - if (c == '-') - sign *= -1; - } - try { - b = std::stod(token.substr(token.find_first_of("0123456789."))); - b *= sign; - } catch (...) { - return ErrorCode::BadFormat; - } - } else { - try { - b = std::stod(token); - } catch (...) { - return ErrorCode::BadFormat; - } + // Obsługa podwójnych znaków, np. "--77.321", "++12.3" + int sign = 1; + while (!token.empty() && (token[0] == '+' || token[0] == '-')) { + if (token[0] == '-') + sign *= -1; + token.erase(0, 1); + } + + if (token.empty()) + return ErrorCode::BadFormat; + + try { + size_t pos; + b = std::stod(token, &pos) * sign; + if (pos != token.size()) + return ErrorCode::BadFormat; // np. "12.4.3" + } catch (...) { + return ErrorCode::BadFormat; } // Sprawdzenie, czy po drugiej liczbie nie ma dodatkowych znaków From 4aa12daf5b165757efc737d900f6b4adc45a2761 Mon Sep 17 00:00:00 2001 From: Jarek Jeda Date: Sat, 27 Sep 2025 20:44:54 +0200 Subject: [PATCH 064/104] fixes6 --- .../advancedCalculator.cpp | 106 ++++++++---------- 1 file changed, 49 insertions(+), 57 deletions(-) diff --git a/homework/advanced-calculator/advancedCalculator.cpp b/homework/advanced-calculator/advancedCalculator.cpp index 4b9a06d5e..f205bd83f 100644 --- a/homework/advanced-calculator/advancedCalculator.cpp +++ b/homework/advanced-calculator/advancedCalculator.cpp @@ -92,81 +92,73 @@ ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { if (!out) return ErrorCode::BadFormat; - // Sprawdzenie niedozwolonych znaków i przecinków + std::string str; for (char c : input) { - if (c == ',') - return ErrorCode::BadFormat; - if (!std::isdigit(c) && c != '+' && c != '-' && c != '*' && c != '/' && - c != '.' && c != '!' && c != ' ' && c != '%' && c != '^' && c != '$') { + if (!std::isspace(c)) + str += c; // usuń wszystkie spacje + } + + if (str.empty()) + return ErrorCode::BadFormat; + + // sprawdzenie niepoprawnych znaków + for (char c : str) { + if (!std::isdigit(c) && c != '+' && c != '-' && c != '*' && + c != '/' && c != '.' && c != '!' && + c != '%' && c != '^' && c != '$') { + if (c == ',') + return ErrorCode::BadFormat; return ErrorCode::BadCharacter; } } - std::istringstream iss(input); - std::string token; - double a = 0.0, b = 0.0; - char op = 0; + // factorial specjalny przypadek + if (str.back() == '!') { + std::string left = str.substr(0, str.size() - 1); + if (left.empty()) + return ErrorCode::BadFormat; - // Wczytanie pierwszej liczby (z możliwym znakiem + lub -) - if (!(iss >> token)) - return ErrorCode::BadFormat; - try { - size_t pos; - a = std::stod(token, &pos); - if (pos != token.size()) - return ErrorCode::BadFormat; // np. "12.4.3" - } catch (...) { - return ErrorCode::BadFormat; + char* endptr = nullptr; + double a = std::strtod(left.c_str(), &endptr); + if (*endptr != '\0') + return ErrorCode::BadFormat; + + return operations.at('!')(a, 0.0, out); } - // Wczytanie operatora - if (!(iss >> op)) - return ErrorCode::BadFormat; - if (operations.find(op) == operations.end()) - return ErrorCode::BadCharacter; - - if (op == '!') { - // Factorial – po nim nie może być nic oprócz spacji - std::string rest; - std::getline(iss, rest); - for (char c : rest) { - if (!std::isspace(c)) - return ErrorCode::BadFormat; + // szukamy operatora (poza znakiem pierwszej liczby) + size_t pos = 0; + if (str[0] == '+' || str[0] == '-') + pos = 1; + + size_t opPos = std::string::npos; + char op = 0; + for (; pos < str.size(); ++pos) { + if (operations.find(str[pos]) != operations.end()) { + opPos = pos; + op = str[pos]; + break; } - return operations.at(op)(a, 0.0, out); } - // Wczytanie drugiej liczby (obsługa prefiksu + lub -) - if (!(iss >> token)) + if (opPos == std::string::npos) return ErrorCode::BadFormat; - // Obsługa podwójnych znaków, np. "--77.321", "++12.3" - int sign = 1; - while (!token.empty() && (token[0] == '+' || token[0] == '-')) { - if (token[0] == '-') - sign *= -1; - token.erase(0, 1); - } + std::string left = str.substr(0, opPos); + std::string right = str.substr(opPos + 1); - if (token.empty()) + if (left.empty() || right.empty()) return ErrorCode::BadFormat; - try { - size_t pos; - b = std::stod(token, &pos) * sign; - if (pos != token.size()) - return ErrorCode::BadFormat; // np. "12.4.3" - } catch (...) { + char* endptr = nullptr; + double a = std::strtod(left.c_str(), &endptr); + if (*endptr != '\0') return ErrorCode::BadFormat; - } - // Sprawdzenie, czy po drugiej liczbie nie ma dodatkowych znaków - std::string rest; - std::getline(iss, rest); - for (char c : rest) { - if (!std::isspace(c)) - return ErrorCode::BadFormat; - } + double b = 0.0; + b = std::strtod(right.c_str(), &endptr); + if (*endptr != '\0') + return ErrorCode::BadFormat; return operations.at(op)(a, b, out); } From 7095ac2188897fd4fcd3d66fe494bfeaa21e6c7a Mon Sep 17 00:00:00 2001 From: Jarek Jeda Date: Sat, 27 Sep 2025 21:00:12 +0200 Subject: [PATCH 065/104] fixes6 --- .../advancedCalculator.cpp | 86 +++++++------------ 1 file changed, 30 insertions(+), 56 deletions(-) diff --git a/homework/advanced-calculator/advancedCalculator.cpp b/homework/advanced-calculator/advancedCalculator.cpp index f205bd83f..364d782a3 100644 --- a/homework/advanced-calculator/advancedCalculator.cpp +++ b/homework/advanced-calculator/advancedCalculator.cpp @@ -89,76 +89,50 @@ AdvancedCalculator::AdvancedCalculator() { } ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { - if (!out) - return ErrorCode::BadFormat; - - std::string str; - for (char c : input) { - if (!std::isspace(c)) - str += c; // usuń wszystkie spacje - } - - if (str.empty()) - return ErrorCode::BadFormat; - - // sprawdzenie niepoprawnych znaków - for (char c : str) { - if (!std::isdigit(c) && c != '+' && c != '-' && c != '*' && - c != '/' && c != '.' && c != '!' && + // Sprawdzenie niedozwolonych znaków + for (unsigned char c : input) { + if (!std::isdigit(c) && c != '+' && c != '-' && c != '*' && c != '/' && + c != '.' && c != '!' && c != ' ' && c != '%' && c != '^' && c != '$') { + // jeśli trafi się przecinek → BadFormat if (c == ',') return ErrorCode::BadFormat; return ErrorCode::BadCharacter; } } - // factorial specjalny przypadek - if (str.back() == '!') { - std::string left = str.substr(0, str.size() - 1); - if (left.empty()) - return ErrorCode::BadFormat; - - char* endptr = nullptr; - double a = std::strtod(left.c_str(), &endptr); - if (*endptr != '\0') - return ErrorCode::BadFormat; - - return operations.at('!')(a, 0.0, out); - } - - // szukamy operatora (poza znakiem pierwszej liczby) - size_t pos = 0; - if (str[0] == '+' || str[0] == '-') - pos = 1; - - size_t opPos = std::string::npos; + std::istringstream iss(input); + double a = 0.0, b = 0.0; char op = 0; - for (; pos < str.size(); ++pos) { - if (operations.find(str[pos]) != operations.end()) { - opPos = pos; - op = str[pos]; - break; - } - } - if (opPos == std::string::npos) + // Pierwsza liczba + if (!(iss >> a)) return ErrorCode::BadFormat; - std::string left = str.substr(0, opPos); - std::string right = str.substr(opPos + 1); - - if (left.empty() || right.empty()) + // Operator + if (!(iss >> op)) return ErrorCode::BadFormat; + if (operations.find(op) == operations.end()) + return ErrorCode::BadCharacter; - char* endptr = nullptr; - double a = std::strtod(left.c_str(), &endptr); - if (*endptr != '\0') - return ErrorCode::BadFormat; + if (op != '!') { + // Druga liczba może mieć znak + std::string token; + if (!(iss >> token)) + return ErrorCode::BadFormat; - double b = 0.0; - b = std::strtod(right.c_str(), &endptr); - if (*endptr != '\0') - return ErrorCode::BadFormat; + std::istringstream iss2(token); + if (!(iss2 >> b)) + return ErrorCode::BadFormat; + } + + // Sprawdzenie reszty ciągu + std::string rest; + std::getline(iss, rest); + for (char c : rest) { + if (!std::isspace(c)) + return ErrorCode::BadFormat; + } return operations.at(op)(a, b, out); } From 89f7a1b5d862d697724bcf54b9c6f4754cfaad8f Mon Sep 17 00:00:00 2001 From: Jarek Jeda Date: Sat, 27 Sep 2025 21:01:46 +0200 Subject: [PATCH 066/104] fixes6 --- homework/advanced-calculator/advancedCalculator.cpp | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/homework/advanced-calculator/advancedCalculator.cpp b/homework/advanced-calculator/advancedCalculator.cpp index 364d782a3..831411b92 100644 --- a/homework/advanced-calculator/advancedCalculator.cpp +++ b/homework/advanced-calculator/advancedCalculator.cpp @@ -90,17 +90,19 @@ AdvancedCalculator::AdvancedCalculator() { ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { // Sprawdzenie niedozwolonych znaków - for (unsigned char c : input) { + for (char c : input) { if (!std::isdigit(c) && c != '+' && c != '-' && c != '*' && c != '/' && c != '.' && c != '!' && c != ' ' && c != '%' && c != '^' && c != '$') { - // jeśli trafi się przecinek → BadFormat - if (c == ',') - return ErrorCode::BadFormat; return ErrorCode::BadCharacter; } } + // Niedozwolone przecinki + if (input.find(',') != std::string::npos) { + return ErrorCode::BadFormat; + } + std::istringstream iss(input); double a = 0.0, b = 0.0; char op = 0; From b966aefef9067bb74867eb3baca35dce45780551 Mon Sep 17 00:00:00 2001 From: Jarek Jeda Date: Mon, 29 Sep 2025 16:22:21 +0200 Subject: [PATCH 067/104] fix9 --- .../advanced-calculator/advancedCalculator.cpp | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/homework/advanced-calculator/advancedCalculator.cpp b/homework/advanced-calculator/advancedCalculator.cpp index 831411b92..894e4a99f 100644 --- a/homework/advanced-calculator/advancedCalculator.cpp +++ b/homework/advanced-calculator/advancedCalculator.cpp @@ -89,7 +89,9 @@ AdvancedCalculator::AdvancedCalculator() { } ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { - // Sprawdzenie niedozwolonych znaków + if (input.find(',') != std::string::npos) { + return ErrorCode::BadFormat; + } for (char c : input) { if (!std::isdigit(c) && c != '+' && c != '-' && c != '*' && c != '/' && c != '.' && c != '!' && c != ' ' && @@ -98,27 +100,19 @@ ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { } } - // Niedozwolone przecinki - if (input.find(',') != std::string::npos) { - return ErrorCode::BadFormat; - } - std::istringstream iss(input); double a = 0.0, b = 0.0; char op = 0; - // Pierwsza liczba if (!(iss >> a)) return ErrorCode::BadFormat; - // Operator if (!(iss >> op)) return ErrorCode::BadFormat; if (operations.find(op) == operations.end()) return ErrorCode::BadCharacter; if (op != '!') { - // Druga liczba może mieć znak std::string token; if (!(iss >> token)) return ErrorCode::BadFormat; @@ -128,14 +122,12 @@ ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { return ErrorCode::BadFormat; } - // Sprawdzenie reszty ciągu std::string rest; std::getline(iss, rest); for (char c : rest) { if (!std::isspace(c)) return ErrorCode::BadFormat; } - return operations.at(op)(a, b, out); } From 1101d77f1f022efe8ce7929de4e53f14f040cae6 Mon Sep 17 00:00:00 2001 From: Jarek Jeda Date: Mon, 29 Sep 2025 16:27:21 +0200 Subject: [PATCH 068/104] fix9 --- .../advanced-calculator/advancedCalculator.cpp | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/homework/advanced-calculator/advancedCalculator.cpp b/homework/advanced-calculator/advancedCalculator.cpp index 894e4a99f..e517e3de4 100644 --- a/homework/advanced-calculator/advancedCalculator.cpp +++ b/homework/advanced-calculator/advancedCalculator.cpp @@ -89,17 +89,18 @@ AdvancedCalculator::AdvancedCalculator() { } ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { - if (input.find(',') != std::string::npos) { - return ErrorCode::BadFormat; - } for (char c : input) { - if (!std::isdigit(c) && c != '+' && c != '-' && c != '*' && c != '/' && - c != '.' && c != '!' && c != ' ' && - c != '%' && c != '^' && c != '$') { - return ErrorCode::BadCharacter; + if (!(std::isdigit(c) || c == '.' || c == ' ' || + operations.count(c) || c == ',')) { + return ErrorCode::BadCharacter; // np. litery, średnik itd. } } + // 2. Sprawdzenie przecinka + if (input.find(',') != std::string::npos) { + return ErrorCode::BadFormat; // np. 5,1! + } + std::istringstream iss(input); double a = 0.0, b = 0.0; char op = 0; From 51dfa3de64b9c5f42e6ebdfb189727325d4bc7d4 Mon Sep 17 00:00:00 2001 From: Jarek Jeda Date: Mon, 29 Sep 2025 16:44:43 +0200 Subject: [PATCH 069/104] fix_error_9/10 --- .../advancedCalculator.cpp | 40 ++++++++++++------- 1 file changed, 25 insertions(+), 15 deletions(-) diff --git a/homework/advanced-calculator/advancedCalculator.cpp b/homework/advanced-calculator/advancedCalculator.cpp index e517e3de4..3f4f42ab9 100644 --- a/homework/advanced-calculator/advancedCalculator.cpp +++ b/homework/advanced-calculator/advancedCalculator.cpp @@ -89,14 +89,15 @@ AdvancedCalculator::AdvancedCalculator() { } ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { + // 1. Sprawdzenie niedozwolonych znaków for (char c : input) { if (!(std::isdigit(c) || c == '.' || c == ' ' || - operations.count(c) || c == ',')) { - return ErrorCode::BadCharacter; // np. litery, średnik itd. + operations.count(c))) { + return ErrorCode::BadCharacter; // np. litery, średnik } } - // 2. Sprawdzenie przecinka + // 2. Sprawdzenie przecinka w liczbach if (input.find(',') != std::string::npos) { return ErrorCode::BadFormat; // np. 5,1! } @@ -108,28 +109,37 @@ ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { if (!(iss >> a)) return ErrorCode::BadFormat; - if (!(iss >> op)) - return ErrorCode::BadFormat; - if (operations.find(op) == operations.end()) - return ErrorCode::BadCharacter; + if (!(iss >> op)) { + // tylko jedna liczba, np. dla silni unarnej + *out = a; + return ErrorCode::OK; + } - if (op != '!') { - std::string token; - if (!(iss >> token)) - return ErrorCode::BadFormat; + if (operations.find(op) == operations.end()) { + return ErrorCode::BadCharacter; + } - std::istringstream iss2(token); - if (!(iss2 >> b)) - return ErrorCode::BadFormat; + if (op == '!') { + // factorial nie wymaga drugiej liczby + std::string leftover; + if (iss >> leftover) + return ErrorCode::BadFormat; // np. "5! 2" + return operations['!'](a, 0, out); } + // dla operatorów binarnych + if (!(iss >> b)) + return ErrorCode::BadFormat; + + // sprawdzamy, czy po liczbie nie ma dodatkowych znaków std::string rest; std::getline(iss, rest); for (char c : rest) { if (!std::isspace(c)) return ErrorCode::BadFormat; } - return operations.at(op)(a, b, out); + + return operations[op](a, b, out); } ErrorCode process(const std::string& input, double* out) { From 5c195a21f44b868b6adf0a42fac8056ecb4547bd Mon Sep 17 00:00:00 2001 From: Jarek Jeda Date: Mon, 29 Sep 2025 16:50:28 +0200 Subject: [PATCH 070/104] fix_error_9 --- .../advancedCalculator.cpp | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/homework/advanced-calculator/advancedCalculator.cpp b/homework/advanced-calculator/advancedCalculator.cpp index 3f4f42ab9..074b8051b 100644 --- a/homework/advanced-calculator/advancedCalculator.cpp +++ b/homework/advanced-calculator/advancedCalculator.cpp @@ -89,17 +89,16 @@ AdvancedCalculator::AdvancedCalculator() { } ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { - // 1. Sprawdzenie niedozwolonych znaków - for (char c : input) { - if (!(std::isdigit(c) || c == '.' || c == ' ' || - operations.count(c))) { - return ErrorCode::BadCharacter; // np. litery, średnik - } + // 1. Sprawdzenie przecinka w liczbach + if (input.find(',') != std::string::npos) { + return ErrorCode::BadFormat; } - // 2. Sprawdzenie przecinka w liczbach - if (input.find(',') != std::string::npos) { - return ErrorCode::BadFormat; // np. 5,1! + // 2. Sprawdzenie niedozwolonych znaków + for (char c : input) { + if (!(std::isdigit(c) || c == '.' || c == ' ' || operations.count(c))) { + return ErrorCode::BadCharacter; + } } std::istringstream iss(input); @@ -123,7 +122,7 @@ ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { // factorial nie wymaga drugiej liczby std::string leftover; if (iss >> leftover) - return ErrorCode::BadFormat; // np. "5! 2" + return ErrorCode::BadFormat; return operations['!'](a, 0, out); } From 78680ff9ef6f8f04b9e730c0111e5ef93dca449e Mon Sep 17 00:00:00 2001 From: Jarek Jeda Date: Mon, 29 Sep 2025 16:56:10 +0200 Subject: [PATCH 071/104] fix_error_9 --- .../advancedCalculator.cpp | 42 +++++++------------ 1 file changed, 16 insertions(+), 26 deletions(-) diff --git a/homework/advanced-calculator/advancedCalculator.cpp b/homework/advanced-calculator/advancedCalculator.cpp index 074b8051b..65c7d480f 100644 --- a/homework/advanced-calculator/advancedCalculator.cpp +++ b/homework/advanced-calculator/advancedCalculator.cpp @@ -89,48 +89,38 @@ AdvancedCalculator::AdvancedCalculator() { } ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { - // 1. Sprawdzenie przecinka w liczbach - if (input.find(',') != std::string::npos) { - return ErrorCode::BadFormat; - } - - // 2. Sprawdzenie niedozwolonych znaków + std::string s; for (char c : input) { - if (!(std::isdigit(c) || c == '.' || c == ' ' || operations.count(c))) { + if (!std::isdigit(c) && c != '.' && c != ' ' && operations.count(c) == 0) { + if (c == ',') + return ErrorCode::BadFormat; return ErrorCode::BadCharacter; } + s += c; } - std::istringstream iss(input); + std::istringstream iss(s); double a = 0.0, b = 0.0; char op = 0; - if (!(iss >> a)) + // Niepoprawny początkowy operator + if (s[0] == '+' || s[0] == '-' || s[0] == '*' || s[0] == '/' || + s[0] == '%' || s[0] == '^' || s[0] == '$' || s[0] == '!') { return ErrorCode::BadFormat; - - if (!(iss >> op)) { - // tylko jedna liczba, np. dla silni unarnej - *out = a; - return ErrorCode::OK; } - if (operations.find(op) == operations.end()) { + if (!(iss >> a)) + return ErrorCode::BadFormat; + if (!(iss >> op)) + return ErrorCode::BadFormat; + if (operations.find(op) == operations.end()) return ErrorCode::BadCharacter; - } - if (op == '!') { - // factorial nie wymaga drugiej liczby - std::string leftover; - if (iss >> leftover) + if (op != '!') { + if (!(iss >> b)) return ErrorCode::BadFormat; - return operations['!'](a, 0, out); } - // dla operatorów binarnych - if (!(iss >> b)) - return ErrorCode::BadFormat; - - // sprawdzamy, czy po liczbie nie ma dodatkowych znaków std::string rest; std::getline(iss, rest); for (char c : rest) { From 40806adb45a5f893603089e2abc97e8bba022983 Mon Sep 17 00:00:00 2001 From: Jarek Jeda Date: Mon, 29 Sep 2025 17:03:15 +0200 Subject: [PATCH 072/104] fix_error_9 --- .../advancedCalculator.cpp | 49 +++++++++++++------ 1 file changed, 33 insertions(+), 16 deletions(-) diff --git a/homework/advanced-calculator/advancedCalculator.cpp b/homework/advanced-calculator/advancedCalculator.cpp index 65c7d480f..c4b59d79b 100644 --- a/homework/advanced-calculator/advancedCalculator.cpp +++ b/homework/advanced-calculator/advancedCalculator.cpp @@ -89,38 +89,55 @@ AdvancedCalculator::AdvancedCalculator() { } ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { - std::string s; + // 1. Sprawdzenie niedozwolonych znaków for (char c : input) { - if (!std::isdigit(c) && c != '.' && c != ' ' && operations.count(c) == 0) { - if (c == ',') - return ErrorCode::BadFormat; - return ErrorCode::BadCharacter; + if (!(std::isdigit(c) || c == '.' || c == ' ' || + operations.count(c))) { + return ErrorCode::BadCharacter; // np. litery, średnik } - s += c; } - std::istringstream iss(s); + + // 2. Sprawdzenie przecinka w liczbach + if (input.find(',') != std::string::npos) { + return ErrorCode::BadFormat; // np. 5,1! + } + + std::istringstream iss(input); double a = 0.0, b = 0.0; char op = 0; - // Niepoprawny początkowy operator - if (s[0] == '+' || s[0] == '-' || s[0] == '*' || s[0] == '/' || - s[0] == '%' || s[0] == '^' || s[0] == '$' || s[0] == '!') { + if (input[0] == '+' || input[0] == '-' || input[0] == '*' || input[0] == '/' || + input[0] == '%' || input[0] == '^' || input[0] == '$' || input[0] == '!') { return ErrorCode::BadFormat; } if (!(iss >> a)) return ErrorCode::BadFormat; - if (!(iss >> op)) - return ErrorCode::BadFormat; - if (operations.find(op) == operations.end()) + + if (!(iss >> op)) { + // tylko jedna liczba, np. dla silni unarnej + *out = a; + return ErrorCode::OK; + } + + if (operations.find(op) == operations.end()) { return ErrorCode::BadCharacter; + } - if (op != '!') { - if (!(iss >> b)) - return ErrorCode::BadFormat; + if (op == '!') { + // factorial nie wymaga drugiej liczby + std::string leftover; + if (iss >> leftover) + return ErrorCode::BadFormat; // np. "5! 2" + return operations['!'](a, 0, out); } + // dla operatorów binarnych + if (!(iss >> b)) + return ErrorCode::BadFormat; + + // sprawdzamy, czy po liczbie nie ma dodatkowych znaków std::string rest; std::getline(iss, rest); for (char c : rest) { From fc49e0954cb983067bd9a107e254f0522e1a328b Mon Sep 17 00:00:00 2001 From: Jarek Jeda Date: Mon, 29 Sep 2025 17:09:51 +0200 Subject: [PATCH 073/104] fix_error_9 --- homework/advanced-calculator/advancedCalculator.cpp | 6 ------ 1 file changed, 6 deletions(-) diff --git a/homework/advanced-calculator/advancedCalculator.cpp b/homework/advanced-calculator/advancedCalculator.cpp index c4b59d79b..3f4f42ab9 100644 --- a/homework/advanced-calculator/advancedCalculator.cpp +++ b/homework/advanced-calculator/advancedCalculator.cpp @@ -97,7 +97,6 @@ ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { } } - // 2. Sprawdzenie przecinka w liczbach if (input.find(',') != std::string::npos) { return ErrorCode::BadFormat; // np. 5,1! @@ -107,11 +106,6 @@ ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { double a = 0.0, b = 0.0; char op = 0; - if (input[0] == '+' || input[0] == '-' || input[0] == '*' || input[0] == '/' || - input[0] == '%' || input[0] == '^' || input[0] == '$' || input[0] == '!') { - return ErrorCode::BadFormat; - } - if (!(iss >> a)) return ErrorCode::BadFormat; From f47df5d477422eb3ec30911bdc74097fd3af4601 Mon Sep 17 00:00:00 2001 From: Jarek Jeda Date: Mon, 29 Sep 2025 20:22:33 +0200 Subject: [PATCH 074/104] fix_error_9 --- homework/advanced-calculator/advancedCalculator.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/homework/advanced-calculator/advancedCalculator.cpp b/homework/advanced-calculator/advancedCalculator.cpp index 3f4f42ab9..4b499cff2 100644 --- a/homework/advanced-calculator/advancedCalculator.cpp +++ b/homework/advanced-calculator/advancedCalculator.cpp @@ -92,7 +92,7 @@ ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { // 1. Sprawdzenie niedozwolonych znaków for (char c : input) { if (!(std::isdigit(c) || c == '.' || c == ' ' || - operations.count(c))) { + operations.count(c) || c == ',')) { return ErrorCode::BadCharacter; // np. litery, średnik } } From 1e43ea2092c9908d96029f0cb03fbdf14864f2ee Mon Sep 17 00:00:00 2001 From: Jarek Jeda Date: Mon, 29 Sep 2025 20:29:00 +0200 Subject: [PATCH 075/104] fix_error_9 --- homework/advanced-calculator/advancedCalculator.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/homework/advanced-calculator/advancedCalculator.cpp b/homework/advanced-calculator/advancedCalculator.cpp index 4b499cff2..85ee91372 100644 --- a/homework/advanced-calculator/advancedCalculator.cpp +++ b/homework/advanced-calculator/advancedCalculator.cpp @@ -106,6 +106,10 @@ ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { double a = 0.0, b = 0.0; char op = 0; + if (!input.empty() && operations.count(input[0]) && input[0] != '!') { + return ErrorCode::BadFormat; // wyrażenie nie może zaczynać się od operatora binarnego + } + if (!(iss >> a)) return ErrorCode::BadFormat; From e41f0cd6b144cfd065011450c02e5c43280116ee Mon Sep 17 00:00:00 2001 From: Jarek Jeda Date: Mon, 29 Sep 2025 20:35:46 +0200 Subject: [PATCH 076/104] fix_error_9 --- .../advancedCalculator.cpp | 34 +++++++++---------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/homework/advanced-calculator/advancedCalculator.cpp b/homework/advanced-calculator/advancedCalculator.cpp index 85ee91372..5abda125c 100644 --- a/homework/advanced-calculator/advancedCalculator.cpp +++ b/homework/advanced-calculator/advancedCalculator.cpp @@ -89,53 +89,53 @@ AdvancedCalculator::AdvancedCalculator() { } ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { - // 1. Sprawdzenie niedozwolonych znaków + if (input.empty()) + return ErrorCode::BadFormat; + + // 1. Sprawdzenie niedozwolonych znaków (tylko cyfry, '.', spacje i znaki operacji) for (char c : input) { - if (!(std::isdigit(c) || c == '.' || c == ' ' || - operations.count(c) || c == ',')) { - return ErrorCode::BadCharacter; // np. litery, średnik + if (!(std::isdigit(c) || c == '.' || std::isspace(c) || operations.count(c))) { + return ErrorCode::BadCharacter; } } - // 2. Sprawdzenie przecinka w liczbach - if (input.find(',') != std::string::npos) { - return ErrorCode::BadFormat; // np. 5,1! - } + // 2. Sprawdzenie przecinka + if (input.find(',') != std::string::npos) + return ErrorCode::BadFormat; std::istringstream iss(input); double a = 0.0, b = 0.0; char op = 0; - if (!input.empty() && operations.count(input[0]) && input[0] != '!') { - return ErrorCode::BadFormat; // wyrażenie nie może zaczynać się od operatora binarnego + // 3. Wczytanie pierwszej liczby (nie może zaczynać się od operatora binarnego) + char firstChar = input.find_first_not_of(' '); + if (firstChar != std::string::npos && operations.count(input[firstChar]) && input[firstChar] != '!') { + return ErrorCode::BadFormat; } if (!(iss >> a)) return ErrorCode::BadFormat; if (!(iss >> op)) { - // tylko jedna liczba, np. dla silni unarnej *out = a; return ErrorCode::OK; } - if (operations.find(op) == operations.end()) { + if (operations.find(op) == operations.end()) return ErrorCode::BadCharacter; - } + // 4. Operator unarny ! if (op == '!') { - // factorial nie wymaga drugiej liczby std::string leftover; if (iss >> leftover) - return ErrorCode::BadFormat; // np. "5! 2" + return ErrorCode::BadFormat; return operations['!'](a, 0, out); } - // dla operatorów binarnych + // 5. Operator binarny if (!(iss >> b)) return ErrorCode::BadFormat; - // sprawdzamy, czy po liczbie nie ma dodatkowych znaków std::string rest; std::getline(iss, rest); for (char c : rest) { From ec53c54be1edca6e14e8e4527b3ef3b6fc4c04a9 Mon Sep 17 00:00:00 2001 From: Jarek Jeda Date: Mon, 29 Sep 2025 20:47:56 +0200 Subject: [PATCH 077/104] fix_error_9 --- .../advancedCalculator.cpp | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/homework/advanced-calculator/advancedCalculator.cpp b/homework/advanced-calculator/advancedCalculator.cpp index 5abda125c..6550711ae 100644 --- a/homework/advanced-calculator/advancedCalculator.cpp +++ b/homework/advanced-calculator/advancedCalculator.cpp @@ -92,7 +92,7 @@ ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { if (input.empty()) return ErrorCode::BadFormat; - // 1. Sprawdzenie niedozwolonych znaków (tylko cyfry, '.', spacje i znaki operacji) + // 1. Sprawdzenie niedozwolonych znaków for (char c : input) { if (!(std::isdigit(c) || c == '.' || std::isspace(c) || operations.count(c))) { return ErrorCode::BadCharacter; @@ -107,35 +107,34 @@ ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { double a = 0.0, b = 0.0; char op = 0; - // 3. Wczytanie pierwszej liczby (nie może zaczynać się od operatora binarnego) - char firstChar = input.find_first_not_of(' '); - if (firstChar != std::string::npos && operations.count(input[firstChar]) && input[firstChar] != '!') { - return ErrorCode::BadFormat; - } - + // 3. Wczytanie pierwszej liczby if (!(iss >> a)) return ErrorCode::BadFormat; + // 4. Wczytanie operatora if (!(iss >> op)) { + // tylko jedna liczba → OK (np. "42") *out = a; return ErrorCode::OK; } + // 5. Sprawdzenie, czy operator jest znany if (operations.find(op) == operations.end()) return ErrorCode::BadCharacter; - // 4. Operator unarny ! + // 6. Operator unarny ! if (op == '!') { std::string leftover; if (iss >> leftover) - return ErrorCode::BadFormat; + return ErrorCode::BadFormat; // np. "5! 2" return operations['!'](a, 0, out); } - // 5. Operator binarny + // 7. Operator binarny if (!(iss >> b)) - return ErrorCode::BadFormat; + return ErrorCode::BadFormat; // np. "+ 2" + // 8. Sprawdzenie dodatkowych znaków po drugiej liczbie std::string rest; std::getline(iss, rest); for (char c : rest) { @@ -143,6 +142,7 @@ ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { return ErrorCode::BadFormat; } + // 9. Wywołanie odpowiedniej operacji return operations[op](a, b, out); } From 9d6362e897d31d5503fafc6db37e71c20035c6ea Mon Sep 17 00:00:00 2001 From: Jarek Jeda Date: Mon, 29 Sep 2025 20:52:45 +0200 Subject: [PATCH 078/104] fix_error_9 --- .../advancedCalculator.cpp | 103 +++++++++++++++--- 1 file changed, 85 insertions(+), 18 deletions(-) diff --git a/homework/advanced-calculator/advancedCalculator.cpp b/homework/advanced-calculator/advancedCalculator.cpp index 6550711ae..b520a9982 100644 --- a/homework/advanced-calculator/advancedCalculator.cpp +++ b/homework/advanced-calculator/advancedCalculator.cpp @@ -22,6 +22,72 @@ std::string errorCodeToString(ErrorCode code) { } } +// AdvancedCalculator::AdvancedCalculator() { +// operations['+'] = [](double a, double b, double* out) { +// *out = a + b; +// return ErrorCode::OK; +// }; +// operations['-'] = [](double a, double b, double* out) { +// *out = a - b; +// return ErrorCode::OK; +// }; +// operations['*'] = [](double a, double b, double* out) { +// *out = a * b; +// return ErrorCode::OK; +// }; +// operations['/'] = [](double a, double b, double* out) { +// if (b == 0.0) { +// return ErrorCode::DivideBy0; +// } +// *out = a / b; +// return ErrorCode::OK; +// }; +// operations['%'] = [](double a, double b, double* out) { +// if (std::floor(a) != a || std::floor(b) != b) { +// return ErrorCode::ModuleOfNonIntegerValue; +// } else if (b == 0.0) { +// return ErrorCode::DivideBy0; +// } +// *out = static_cast(a) % static_cast(b); +// return ErrorCode::OK; +// }; +// operations['^'] = [](double a, double b, double* out) { +// *out = std::pow(a, b); +// return ErrorCode::OK; +// }; +// operations['$'] = [](double a, double b, double* out) { +// if (a < 0) { +// return ErrorCode::SqrtOfNegativeNumber; +// } +// if (b == 0.0) { +// return ErrorCode::DivideBy0; +// } else if (a < 0 && static_cast(b) % 2 == 0) { +// return ErrorCode::SqrtOfNegativeNumber; +// } +// *out = std::pow(a, 1.0 / b); +// return ErrorCode::OK; +// }; +// operations['!'] = [](double a, double, double* out) { +// if (std::isnan(a) || std::isinf(a)) { +// return ErrorCode::BadFormat; +// } +// +// long double result = 1.0; +// double x = std::fabs(a); +// +// if (x == std::floor(x)) { +// for (int i = 1; i <= static_cast(x); ++i) { +// result *= i; +// } +// } else { +// result = tgamma(x + 1); +// } +// +// *out = (a < 0 ? -result : result); +// return ErrorCode::OK; +// }; +// } + AdvancedCalculator::AdvancedCalculator() { operations['+'] = [](double a, double b, double* out) { *out = a + b; @@ -36,18 +102,16 @@ AdvancedCalculator::AdvancedCalculator() { return ErrorCode::OK; }; operations['/'] = [](double a, double b, double* out) { - if (b == 0.0) { + if (b == 0.0) return ErrorCode::DivideBy0; - } *out = a / b; return ErrorCode::OK; }; operations['%'] = [](double a, double b, double* out) { - if (std::floor(a) != a || std::floor(b) != b) { - return ErrorCode::ModuleOfNonIntegerValue; - } else if (b == 0.0) { + if (b == 0.0) return ErrorCode::DivideBy0; - } + if (std::floor(a) != a || std::floor(b) != b) + return ErrorCode::ModuleOfNonIntegerValue; *out = static_cast(a) % static_cast(b); return ErrorCode::OK; }; @@ -56,29 +120,23 @@ AdvancedCalculator::AdvancedCalculator() { return ErrorCode::OK; }; operations['$'] = [](double a, double b, double* out) { - if (a < 0) { - return ErrorCode::SqrtOfNegativeNumber; - } - if (b == 0.0) { + if (b == 0.0) return ErrorCode::DivideBy0; - } else if (a < 0 && static_cast(b) % 2 == 0) { + if (a < 0 && static_cast(b) % 2 == 0) return ErrorCode::SqrtOfNegativeNumber; - } *out = std::pow(a, 1.0 / b); return ErrorCode::OK; }; operations['!'] = [](double a, double, double* out) { - if (std::isnan(a) || std::isinf(a)) { + if (std::isnan(a) || std::isinf(a)) return ErrorCode::BadFormat; - } long double result = 1.0; double x = std::fabs(a); if (x == std::floor(x)) { - for (int i = 1; i <= static_cast(x); ++i) { + for (int i = 1; i <= static_cast(x); ++i) result *= i; - } } else { result = tgamma(x + 1); } @@ -92,14 +150,23 @@ ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { if (input.empty()) return ErrorCode::BadFormat; - // 1. Sprawdzenie niedozwolonych znaków + // 0. Sprawdzenie pierwszego nie-spacji znaku + size_t firstPos = input.find_first_not_of(' '); + if (firstPos != std::string::npos) { + char firstChar = input[firstPos]; + if (operations.count(firstChar) && firstChar != '!') { + return ErrorCode::BadFormat; // np. "+8 - 32.1" + } + } + + // 1. Sprawdzenie niedozwolonych znaków (cyfry, '.', spacje i operatory) for (char c : input) { if (!(std::isdigit(c) || c == '.' || std::isspace(c) || operations.count(c))) { return ErrorCode::BadCharacter; } } - // 2. Sprawdzenie przecinka + // 2. Sprawdzenie przecinka w liczbach if (input.find(',') != std::string::npos) return ErrorCode::BadFormat; From 2d176034906d58f6ba4fae9fc04ddeb8a864aff7 Mon Sep 17 00:00:00 2001 From: Jarek Jeda Date: Mon, 29 Sep 2025 20:55:42 +0200 Subject: [PATCH 079/104] fix_error_9 --- .../advancedCalculator.cpp | 42 +++++++------------ 1 file changed, 14 insertions(+), 28 deletions(-) diff --git a/homework/advanced-calculator/advancedCalculator.cpp b/homework/advanced-calculator/advancedCalculator.cpp index b520a9982..59bb55146 100644 --- a/homework/advanced-calculator/advancedCalculator.cpp +++ b/homework/advanced-calculator/advancedCalculator.cpp @@ -122,7 +122,7 @@ AdvancedCalculator::AdvancedCalculator() { operations['$'] = [](double a, double b, double* out) { if (b == 0.0) return ErrorCode::DivideBy0; - if (a < 0 && static_cast(b) % 2 == 0) + if (a < 0 && std::floor(b) == b && static_cast(b) % 2 == 0) return ErrorCode::SqrtOfNegativeNumber; *out = std::pow(a, 1.0 / b); return ErrorCode::OK; @@ -130,17 +130,14 @@ AdvancedCalculator::AdvancedCalculator() { operations['!'] = [](double a, double, double* out) { if (std::isnan(a) || std::isinf(a)) return ErrorCode::BadFormat; - long double result = 1.0; double x = std::fabs(a); - if (x == std::floor(x)) { for (int i = 1; i <= static_cast(x); ++i) result *= i; } else { result = tgamma(x + 1); } - *out = (a < 0 ? -result : result); return ErrorCode::OK; }; @@ -150,46 +147,36 @@ ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { if (input.empty()) return ErrorCode::BadFormat; - // 0. Sprawdzenie pierwszego nie-spacji znaku - size_t firstPos = input.find_first_not_of(' '); - if (firstPos != std::string::npos) { - char firstChar = input[firstPos]; - if (operations.count(firstChar) && firstChar != '!') { - return ErrorCode::BadFormat; // np. "+8 - 32.1" - } - } + // Sprawdzenie przecinka → BadFormat + if (input.find(',') != std::string::npos) + return ErrorCode::BadFormat; - // 1. Sprawdzenie niedozwolonych znaków (cyfry, '.', spacje i operatory) + // Sprawdzenie niedozwolonych znaków → BadCharacter for (char c : input) { - if (!(std::isdigit(c) || c == '.' || std::isspace(c) || operations.count(c))) { + if (!(std::isdigit(c) || std::isspace(c) || c == '.' || operations.count(c))) { return ErrorCode::BadCharacter; } } - // 2. Sprawdzenie przecinka w liczbach - if (input.find(',') != std::string::npos) - return ErrorCode::BadFormat; - std::istringstream iss(input); double a = 0.0, b = 0.0; char op = 0; - // 3. Wczytanie pierwszej liczby + // Wczytanie pierwszej liczby (może mieć prefiks + lub -) if (!(iss >> a)) return ErrorCode::BadFormat; - // 4. Wczytanie operatora + // Wczytanie operatora if (!(iss >> op)) { - // tylko jedna liczba → OK (np. "42") - *out = a; + *out = a; // tylko jedna liczba → OK return ErrorCode::OK; } - // 5. Sprawdzenie, czy operator jest znany + // Sprawdzenie, czy operator jest znany if (operations.find(op) == operations.end()) return ErrorCode::BadCharacter; - // 6. Operator unarny ! + // Operator unarny ! if (op == '!') { std::string leftover; if (iss >> leftover) @@ -197,11 +184,11 @@ ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { return operations['!'](a, 0, out); } - // 7. Operator binarny + // Operator binarny if (!(iss >> b)) - return ErrorCode::BadFormat; // np. "+ 2" + return ErrorCode::BadFormat; - // 8. Sprawdzenie dodatkowych znaków po drugiej liczbie + // Sprawdzenie dodatkowych znaków po drugiej liczbie std::string rest; std::getline(iss, rest); for (char c : rest) { @@ -209,7 +196,6 @@ ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { return ErrorCode::BadFormat; } - // 9. Wywołanie odpowiedniej operacji return operations[op](a, b, out); } From c9230e0c37bfa8b7c1860d0a497aa9d7fdc6fa8b Mon Sep 17 00:00:00 2001 From: Jarek Jeda Date: Tue, 30 Sep 2025 21:02:14 +0200 Subject: [PATCH 080/104] fix_error_9 --- .../advancedCalculator.cpp | 185 ++++++------------ 1 file changed, 65 insertions(+), 120 deletions(-) diff --git a/homework/advanced-calculator/advancedCalculator.cpp b/homework/advanced-calculator/advancedCalculator.cpp index 59bb55146..4ee2cce46 100644 --- a/homework/advanced-calculator/advancedCalculator.cpp +++ b/homework/advanced-calculator/advancedCalculator.cpp @@ -22,126 +22,71 @@ std::string errorCodeToString(ErrorCode code) { } } -// AdvancedCalculator::AdvancedCalculator() { -// operations['+'] = [](double a, double b, double* out) { -// *out = a + b; -// return ErrorCode::OK; -// }; -// operations['-'] = [](double a, double b, double* out) { -// *out = a - b; -// return ErrorCode::OK; -// }; -// operations['*'] = [](double a, double b, double* out) { -// *out = a * b; -// return ErrorCode::OK; -// }; -// operations['/'] = [](double a, double b, double* out) { -// if (b == 0.0) { -// return ErrorCode::DivideBy0; -// } -// *out = a / b; -// return ErrorCode::OK; -// }; -// operations['%'] = [](double a, double b, double* out) { -// if (std::floor(a) != a || std::floor(b) != b) { -// return ErrorCode::ModuleOfNonIntegerValue; -// } else if (b == 0.0) { -// return ErrorCode::DivideBy0; -// } -// *out = static_cast(a) % static_cast(b); -// return ErrorCode::OK; -// }; -// operations['^'] = [](double a, double b, double* out) { -// *out = std::pow(a, b); -// return ErrorCode::OK; -// }; -// operations['$'] = [](double a, double b, double* out) { -// if (a < 0) { -// return ErrorCode::SqrtOfNegativeNumber; -// } -// if (b == 0.0) { -// return ErrorCode::DivideBy0; -// } else if (a < 0 && static_cast(b) % 2 == 0) { -// return ErrorCode::SqrtOfNegativeNumber; -// } -// *out = std::pow(a, 1.0 / b); -// return ErrorCode::OK; -// }; -// operations['!'] = [](double a, double, double* out) { -// if (std::isnan(a) || std::isinf(a)) { -// return ErrorCode::BadFormat; -// } -// -// long double result = 1.0; -// double x = std::fabs(a); -// -// if (x == std::floor(x)) { -// for (int i = 1; i <= static_cast(x); ++i) { -// result *= i; -// } -// } else { -// result = tgamma(x + 1); -// } -// -// *out = (a < 0 ? -result : result); -// return ErrorCode::OK; -// }; -// } - -AdvancedCalculator::AdvancedCalculator() { - operations['+'] = [](double a, double b, double* out) { - *out = a + b; - return ErrorCode::OK; - }; - operations['-'] = [](double a, double b, double* out) { - *out = a - b; - return ErrorCode::OK; - }; - operations['*'] = [](double a, double b, double* out) { - *out = a * b; - return ErrorCode::OK; - }; - operations['/'] = [](double a, double b, double* out) { - if (b == 0.0) - return ErrorCode::DivideBy0; - *out = a / b; - return ErrorCode::OK; - }; - operations['%'] = [](double a, double b, double* out) { - if (b == 0.0) - return ErrorCode::DivideBy0; - if (std::floor(a) != a || std::floor(b) != b) - return ErrorCode::ModuleOfNonIntegerValue; - *out = static_cast(a) % static_cast(b); - return ErrorCode::OK; - }; - operations['^'] = [](double a, double b, double* out) { - *out = std::pow(a, b); - return ErrorCode::OK; - }; - operations['$'] = [](double a, double b, double* out) { - if (b == 0.0) - return ErrorCode::DivideBy0; - if (a < 0 && std::floor(b) == b && static_cast(b) % 2 == 0) - return ErrorCode::SqrtOfNegativeNumber; - *out = std::pow(a, 1.0 / b); - return ErrorCode::OK; - }; - operations['!'] = [](double a, double, double* out) { - if (std::isnan(a) || std::isinf(a)) - return ErrorCode::BadFormat; - long double result = 1.0; - double x = std::fabs(a); - if (x == std::floor(x)) { - for (int i = 1; i <= static_cast(x); ++i) - result *= i; - } else { - result = tgamma(x + 1); - } - *out = (a < 0 ? -result : result); - return ErrorCode::OK; - }; -} + AdvancedCalculator::AdvancedCalculator() { + operations['+'] = [](double a, double b, double* out) { + *out = a + b; + return ErrorCode::OK; + }; + operations['-'] = [](double a, double b, double* out) { + *out = a - b; + return ErrorCode::OK; + }; + operations['*'] = [](double a, double b, double* out) { + *out = a * b; + return ErrorCode::OK; + }; + operations['/'] = [](double a, double b, double* out) { + if (b == 0.0) { + return ErrorCode::DivideBy0; + } + *out = a / b; + return ErrorCode::OK; + }; + operations['%'] = [](double a, double b, double* out) { + if (std::floor(a) != a || std::floor(b) != b) { + return ErrorCode::ModuleOfNonIntegerValue; + } else if (b == 0.0) { + return ErrorCode::DivideBy0; + } + *out = static_cast(a) % static_cast(b); + return ErrorCode::OK; + }; + operations['^'] = [](double a, double b, double* out) { + *out = std::pow(a, b); + return ErrorCode::OK; + }; + operations['$'] = [](double a, double b, double* out) { + if (a < 0) { + return ErrorCode::SqrtOfNegativeNumber; + } + if (b == 0.0) { + return ErrorCode::DivideBy0; + } else if (a < 0 && static_cast(b) % 2 == 0) { + return ErrorCode::SqrtOfNegativeNumber; + } + *out = std::pow(a, 1.0 / b); + return ErrorCode::OK; + }; + operations['!'] = [](double a, double, double* out) { + if (std::isnan(a) || std::isinf(a)) { + return ErrorCode::BadFormat; + } + + long double result = 1.0; + double x = std::fabs(a); + + if (x == std::floor(x)) { + for (int i = 1; i <= static_cast(x); ++i) { + result *= i; + } + } else { + result = tgamma(x + 1); + } + + *out = (a < 0 ? -result : result); + return ErrorCode::OK; + }; + } ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { if (input.empty()) From fdc781df779937b425a2c0e03f6b56070ba09a4e Mon Sep 17 00:00:00 2001 From: Jarek Jeda Date: Tue, 30 Sep 2025 21:07:56 +0200 Subject: [PATCH 081/104] fix_error_9 --- .../advancedCalculator.cpp | 140 +++++++++--------- 1 file changed, 70 insertions(+), 70 deletions(-) diff --git a/homework/advanced-calculator/advancedCalculator.cpp b/homework/advanced-calculator/advancedCalculator.cpp index 4ee2cce46..e63afcac7 100644 --- a/homework/advanced-calculator/advancedCalculator.cpp +++ b/homework/advanced-calculator/advancedCalculator.cpp @@ -22,87 +22,87 @@ std::string errorCodeToString(ErrorCode code) { } } - AdvancedCalculator::AdvancedCalculator() { - operations['+'] = [](double a, double b, double* out) { - *out = a + b; - return ErrorCode::OK; - }; - operations['-'] = [](double a, double b, double* out) { - *out = a - b; - return ErrorCode::OK; - }; - operations['*'] = [](double a, double b, double* out) { - *out = a * b; - return ErrorCode::OK; - }; - operations['/'] = [](double a, double b, double* out) { - if (b == 0.0) { - return ErrorCode::DivideBy0; - } - *out = a / b; - return ErrorCode::OK; - }; - operations['%'] = [](double a, double b, double* out) { - if (std::floor(a) != a || std::floor(b) != b) { - return ErrorCode::ModuleOfNonIntegerValue; - } else if (b == 0.0) { - return ErrorCode::DivideBy0; - } - *out = static_cast(a) % static_cast(b); - return ErrorCode::OK; - }; - operations['^'] = [](double a, double b, double* out) { - *out = std::pow(a, b); - return ErrorCode::OK; - }; - operations['$'] = [](double a, double b, double* out) { - if (a < 0) { - return ErrorCode::SqrtOfNegativeNumber; - } - if (b == 0.0) { - return ErrorCode::DivideBy0; - } else if (a < 0 && static_cast(b) % 2 == 0) { - return ErrorCode::SqrtOfNegativeNumber; - } - *out = std::pow(a, 1.0 / b); - return ErrorCode::OK; - }; - operations['!'] = [](double a, double, double* out) { - if (std::isnan(a) || std::isinf(a)) { - return ErrorCode::BadFormat; - } - - long double result = 1.0; - double x = std::fabs(a); - - if (x == std::floor(x)) { - for (int i = 1; i <= static_cast(x); ++i) { - result *= i; - } - } else { - result = tgamma(x + 1); - } - - *out = (a < 0 ? -result : result); - return ErrorCode::OK; - }; - } +AdvancedCalculator::AdvancedCalculator() { + operations['+'] = [](double a, double b, double* out) { + *out = a + b; + return ErrorCode::OK; + }; + operations['-'] = [](double a, double b, double* out) { + *out = a - b; + return ErrorCode::OK; + }; + operations['*'] = [](double a, double b, double* out) { + *out = a * b; + return ErrorCode::OK; + }; + operations['/'] = [](double a, double b, double* out) { + if (b == 0.0) { + return ErrorCode::DivideBy0; + } + *out = a / b; + return ErrorCode::OK; + }; + operations['%'] = [](double a, double b, double* out) { + if (std::floor(a) != a || std::floor(b) != b) { + return ErrorCode::ModuleOfNonIntegerValue; + } else if (b == 0.0) { + return ErrorCode::DivideBy0; + } + *out = static_cast(a) % static_cast(b); + return ErrorCode::OK; + }; + operations['^'] = [](double a, double b, double* out) { + *out = std::pow(a, b); + return ErrorCode::OK; + }; + operations['$'] = [](double a, double b, double* out) { + if (a < 0) { + return ErrorCode::SqrtOfNegativeNumber; + } + if (b == 0.0) { + return ErrorCode::DivideBy0; + } else if (a < 0 && static_cast(b) % 2 == 0) { + return ErrorCode::SqrtOfNegativeNumber; + } + *out = std::pow(a, 1.0 / b); + return ErrorCode::OK; + }; + operations['!'] = [](double a, double, double* out) { + if (std::isnan(a) || std::isinf(a)) { + return ErrorCode::BadFormat; + } + + long double result = 1.0; + double x = std::fabs(a); + + if (x == std::floor(x)) { + for (int i = 1; i <= static_cast(x); ++i) { + result *= i; + } + } else { + result = tgamma(x + 1); + } + + *out = (a < 0 ? -result : result); + return ErrorCode::OK; + }; +} ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { if (input.empty()) return ErrorCode::BadFormat; - // Sprawdzenie przecinka → BadFormat - if (input.find(',') != std::string::npos) - return ErrorCode::BadFormat; - - // Sprawdzenie niedozwolonych znaków → BadCharacter + // 1. Sprawdzenie niedozwolonych znaków for (char c : input) { if (!(std::isdigit(c) || std::isspace(c) || c == '.' || operations.count(c))) { return ErrorCode::BadCharacter; } } + // 2. Sprawdzenie przecinka (jeśli są tylko cyfry i operator, ale zawiera ',') + if (input.find(',') != std::string::npos) + return ErrorCode::BadFormat; + std::istringstream iss(input); double a = 0.0, b = 0.0; char op = 0; From 88c39bfe945e318940f2f016dcf965266176a73b Mon Sep 17 00:00:00 2001 From: Jarek Jeda Date: Tue, 30 Sep 2025 21:10:16 +0200 Subject: [PATCH 082/104] fix_error_9 --- homework/advanced-calculator/advancedCalculator.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/homework/advanced-calculator/advancedCalculator.cpp b/homework/advanced-calculator/advancedCalculator.cpp index e63afcac7..7d8e9045d 100644 --- a/homework/advanced-calculator/advancedCalculator.cpp +++ b/homework/advanced-calculator/advancedCalculator.cpp @@ -94,7 +94,7 @@ ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { // 1. Sprawdzenie niedozwolonych znaków for (char c : input) { - if (!(std::isdigit(c) || std::isspace(c) || c == '.' || operations.count(c))) { + if (!(std::isdigit(c) || std::isspace(c) || c == '.' || c == ',' || operations.count(c))) { return ErrorCode::BadCharacter; } } From dc5df73f2d7dbc189762cc32f44c22ec4721ec7f Mon Sep 17 00:00:00 2001 From: Jarek Jeda Date: Tue, 30 Sep 2025 21:14:20 +0200 Subject: [PATCH 083/104] fix_error_9 --- homework/advanced-calculator/advancedCalculator.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/homework/advanced-calculator/advancedCalculator.cpp b/homework/advanced-calculator/advancedCalculator.cpp index 7d8e9045d..7cb09aef0 100644 --- a/homework/advanced-calculator/advancedCalculator.cpp +++ b/homework/advanced-calculator/advancedCalculator.cpp @@ -107,7 +107,12 @@ ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { double a = 0.0, b = 0.0; char op = 0; - // Wczytanie pierwszej liczby (może mieć prefiks + lub -) + iss >> std::ws; + if (iss.peek() == '+') { + return ErrorCode::BadFormat; + } + + // Wczytanie pierwszej liczby (może mSieć prefiks + lub -) if (!(iss >> a)) return ErrorCode::BadFormat; From 0eebfd4786b61028c50867ebc00c79d6081b0c12 Mon Sep 17 00:00:00 2001 From: Jarek Jeda Date: Tue, 30 Sep 2025 22:42:08 +0200 Subject: [PATCH 084/104] fix_error_9 --- .../advancedCalculator.cpp | 29 +++++++++++++++---- 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/homework/advanced-calculator/advancedCalculator.cpp b/homework/advanced-calculator/advancedCalculator.cpp index 7cb09aef0..fb485215a 100644 --- a/homework/advanced-calculator/advancedCalculator.cpp +++ b/homework/advanced-calculator/advancedCalculator.cpp @@ -103,19 +103,31 @@ ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { if (input.find(',') != std::string::npos) return ErrorCode::BadFormat; + auto isValidNumber = [](const std::string& token) { + int dotCount = 0; + for (size_t i = 0; i < token.size(); ++i) { + char c = token[i]; + if (c == '.') { + dotCount++; + if (dotCount > 1) + return false; // za dużo kropek + } else if (!std::isdigit(c) && !(i == 0 && (c == '-' || c == '+'))) { + return false; // niedozwolony znak + } + } + return true; + }; + std::istringstream iss(input); + std::string tokenA, tokenB; double a = 0.0, b = 0.0; char op = 0; - iss >> std::ws; - if (iss.peek() == '+') { + if (!(iss >> tokenA) || !isValidNumber(tokenA)) { return ErrorCode::BadFormat; + a = std::stod(tokenA); } - // Wczytanie pierwszej liczby (może mSieć prefiks + lub -) - if (!(iss >> a)) - return ErrorCode::BadFormat; - // Wczytanie operatora if (!(iss >> op)) { *out = a; // tylko jedna liczba → OK @@ -134,6 +146,11 @@ ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { return operations['!'](a, 0, out); } + if (!(iss >> tokenB) || !isValidNumber(tokenB)) { + return ErrorCode::BadFormat; + b = std::stod(tokenB); + } + // Operator binarny if (!(iss >> b)) return ErrorCode::BadFormat; From e660915c0440c59b0309f2ac714d75c92c9e52e9 Mon Sep 17 00:00:00 2001 From: Jarek Jeda Date: Tue, 30 Sep 2025 22:44:33 +0200 Subject: [PATCH 085/104] fix_error_9 --- homework/advanced-calculator/advancedCalculator.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/homework/advanced-calculator/advancedCalculator.cpp b/homework/advanced-calculator/advancedCalculator.cpp index fb485215a..b781a2fe9 100644 --- a/homework/advanced-calculator/advancedCalculator.cpp +++ b/homework/advanced-calculator/advancedCalculator.cpp @@ -125,8 +125,8 @@ ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { if (!(iss >> tokenA) || !isValidNumber(tokenA)) { return ErrorCode::BadFormat; - a = std::stod(tokenA); } + a = std::stod(tokenA); // Wczytanie operatora if (!(iss >> op)) { @@ -148,8 +148,8 @@ ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { if (!(iss >> tokenB) || !isValidNumber(tokenB)) { return ErrorCode::BadFormat; - b = std::stod(tokenB); } + b = std::stod(tokenB); // Operator binarny if (!(iss >> b)) From f640825f73f68cea3c15a7d24e0860fa6cc08eab Mon Sep 17 00:00:00 2001 From: Jarek Jeda Date: Tue, 30 Sep 2025 22:46:50 +0200 Subject: [PATCH 086/104] fix_error_9 --- homework/advanced-calculator/advancedCalculator.cpp | 4 ---- 1 file changed, 4 deletions(-) diff --git a/homework/advanced-calculator/advancedCalculator.cpp b/homework/advanced-calculator/advancedCalculator.cpp index b781a2fe9..de3bff548 100644 --- a/homework/advanced-calculator/advancedCalculator.cpp +++ b/homework/advanced-calculator/advancedCalculator.cpp @@ -151,10 +151,6 @@ ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { } b = std::stod(tokenB); - // Operator binarny - if (!(iss >> b)) - return ErrorCode::BadFormat; - // Sprawdzenie dodatkowych znaków po drugiej liczbie std::string rest; std::getline(iss, rest); From 17cd9548068c913b13fba1545a4727658b31cbe8 Mon Sep 17 00:00:00 2001 From: Jarek Jeda Date: Thu, 2 Oct 2025 21:21:36 +0200 Subject: [PATCH 087/104] fix_error_9 --- .../advancedCalculator.cpp | 54 ++++++++----------- 1 file changed, 21 insertions(+), 33 deletions(-) diff --git a/homework/advanced-calculator/advancedCalculator.cpp b/homework/advanced-calculator/advancedCalculator.cpp index de3bff548..bba9a9213 100644 --- a/homework/advanced-calculator/advancedCalculator.cpp +++ b/homework/advanced-calculator/advancedCalculator.cpp @@ -92,66 +92,53 @@ ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { if (input.empty()) return ErrorCode::BadFormat; - // 1. Sprawdzenie niedozwolonych znaków - for (char c : input) { - if (!(std::isdigit(c) || std::isspace(c) || c == '.' || c == ',' || operations.count(c))) { - return ErrorCode::BadCharacter; + // 0. Sprawdzenie, czy pierwszy nie-spacja znak nie jest operatorem binarnym + size_t firstPos = input.find_first_not_of(' '); + if (firstPos != std::string::npos) { + char firstChar = input[firstPos]; + if (operations.count(firstChar) && firstChar != '!') { + return ErrorCode::BadFormat; // np. "+8 - 32" jest złym formatem } } - // 2. Sprawdzenie przecinka (jeśli są tylko cyfry i operator, ale zawiera ',') - if (input.find(',') != std::string::npos) - return ErrorCode::BadFormat; - - auto isValidNumber = [](const std::string& token) { - int dotCount = 0; - for (size_t i = 0; i < token.size(); ++i) { - char c = token[i]; - if (c == '.') { - dotCount++; - if (dotCount > 1) - return false; // za dużo kropek - } else if (!std::isdigit(c) && !(i == 0 && (c == '-' || c == '+'))) { - return false; // niedozwolony znak - } + // 1. Sprawdzenie niedozwolonych znaków (cyfry, spacje, '.', operatory) + for (char c : input) { + if (!(std::isdigit(c) || std::isspace(c) || c == '.' || operations.count(c))) { + return ErrorCode::BadCharacter; } - return true; - }; + } std::istringstream iss(input); - std::string tokenA, tokenB; double a = 0.0, b = 0.0; char op = 0; - if (!(iss >> tokenA) || !isValidNumber(tokenA)) { + // 2. Wczytanie pierwszej liczby + if (!(iss >> a)) return ErrorCode::BadFormat; - } - a = std::stod(tokenA); - // Wczytanie operatora + // 3. Wczytanie operatora (jeśli jest) if (!(iss >> op)) { *out = a; // tylko jedna liczba → OK return ErrorCode::OK; } - // Sprawdzenie, czy operator jest znany + // 4. Sprawdzenie, czy operator jest obsługiwany if (operations.find(op) == operations.end()) return ErrorCode::BadCharacter; - // Operator unarny ! + // 5. Operator unarny ! if (op == '!') { std::string leftover; if (iss >> leftover) - return ErrorCode::BadFormat; // np. "5! 2" + return ErrorCode::BadFormat; return operations['!'](a, 0, out); } - if (!(iss >> tokenB) || !isValidNumber(tokenB)) { + // 6. Operator binarny – wczytanie drugiej liczby + if (!(iss >> b)) return ErrorCode::BadFormat; - } - b = std::stod(tokenB); - // Sprawdzenie dodatkowych znaków po drugiej liczbie + // 7. Sprawdzenie dodatkowych znaków po drugiej liczbie std::string rest; std::getline(iss, rest); for (char c : rest) { @@ -159,6 +146,7 @@ ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { return ErrorCode::BadFormat; } + // 8. Wywołanie operacji return operations[op](a, b, out); } From a40e735e79d194820e3f5f3329d61e71f6e006c5 Mon Sep 17 00:00:00 2001 From: Jarek Jeda Date: Mon, 6 Oct 2025 13:00:36 +0200 Subject: [PATCH 088/104] error fixes --- .../advancedCalculator.cpp | 41 +++++++------------ 1 file changed, 15 insertions(+), 26 deletions(-) diff --git a/homework/advanced-calculator/advancedCalculator.cpp b/homework/advanced-calculator/advancedCalculator.cpp index bba9a9213..609132403 100644 --- a/homework/advanced-calculator/advancedCalculator.cpp +++ b/homework/advanced-calculator/advancedCalculator.cpp @@ -92,16 +92,7 @@ ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { if (input.empty()) return ErrorCode::BadFormat; - // 0. Sprawdzenie, czy pierwszy nie-spacja znak nie jest operatorem binarnym - size_t firstPos = input.find_first_not_of(' '); - if (firstPos != std::string::npos) { - char firstChar = input[firstPos]; - if (operations.count(firstChar) && firstChar != '!') { - return ErrorCode::BadFormat; // np. "+8 - 32" jest złym formatem - } - } - - // 1. Sprawdzenie niedozwolonych znaków (cyfry, spacje, '.', operatory) + // Sprawdzenie niedozwolonych znaków for (char c : input) { if (!(std::isdigit(c) || std::isspace(c) || c == '.' || operations.count(c))) { return ErrorCode::BadCharacter; @@ -109,24 +100,25 @@ ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { } std::istringstream iss(input); + std::string tokenA, tokenB; double a = 0.0, b = 0.0; char op = 0; - // 2. Wczytanie pierwszej liczby - if (!(iss >> a)) + // pierwsza liczba + if (!(iss >> tokenA)) return ErrorCode::BadFormat; + a = std::stod(tokenA); - // 3. Wczytanie operatora (jeśli jest) + // operator if (!(iss >> op)) { - *out = a; // tylko jedna liczba → OK - return ErrorCode::OK; + *out = a; + return ErrorCode::OK; // tylko jedna liczba } - // 4. Sprawdzenie, czy operator jest obsługiwany if (operations.find(op) == operations.end()) return ErrorCode::BadCharacter; - // 5. Operator unarny ! + // factorial (!) if (op == '!') { std::string leftover; if (iss >> leftover) @@ -134,19 +126,16 @@ ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { return operations['!'](a, 0, out); } - // 6. Operator binarny – wczytanie drugiej liczby - if (!(iss >> b)) + // druga liczba + if (!(iss >> tokenB)) return ErrorCode::BadFormat; + b = std::stod(tokenB); - // 7. Sprawdzenie dodatkowych znaków po drugiej liczbie + // brak śmieci po liczbie std::string rest; - std::getline(iss, rest); - for (char c : rest) { - if (!std::isspace(c)) - return ErrorCode::BadFormat; - } + if (iss >> rest) + return ErrorCode::BadFormat; - // 8. Wywołanie operacji return operations[op](a, b, out); } From b760e2f9867f8252ba3c433f8e9ac9c01d97781d Mon Sep 17 00:00:00 2001 From: Jarek Jeda Date: Mon, 6 Oct 2025 14:41:32 +0200 Subject: [PATCH 089/104] error fixes --- .../advancedCalculator.cpp | 38 ++++++++----------- 1 file changed, 15 insertions(+), 23 deletions(-) diff --git a/homework/advanced-calculator/advancedCalculator.cpp b/homework/advanced-calculator/advancedCalculator.cpp index 609132403..0f9b7e653 100644 --- a/homework/advanced-calculator/advancedCalculator.cpp +++ b/homework/advanced-calculator/advancedCalculator.cpp @@ -92,50 +92,42 @@ ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { if (input.empty()) return ErrorCode::BadFormat; - // Sprawdzenie niedozwolonych znaków - for (char c : input) { - if (!(std::isdigit(c) || std::isspace(c) || c == '.' || operations.count(c))) { - return ErrorCode::BadCharacter; - } - } - std::istringstream iss(input); - std::string tokenA, tokenB; double a = 0.0, b = 0.0; char op = 0; - // pierwsza liczba - if (!(iss >> tokenA)) + // Wczytaj pierwszą liczbę + if (!(iss >> a)) return ErrorCode::BadFormat; - a = std::stod(tokenA); - // operator + // Wczytaj operator if (!(iss >> op)) { *out = a; return ErrorCode::OK; // tylko jedna liczba } - if (operations.find(op) == operations.end()) - return ErrorCode::BadCharacter; - - // factorial (!) + // Operator unarny if (op == '!') { - std::string leftover; - if (iss >> leftover) - return ErrorCode::BadFormat; + std::string extra; + if (iss >> extra) + return ErrorCode::BadFormat; // np. "5! 2" return operations['!'](a, 0, out); } - // druga liczba - if (!(iss >> tokenB)) + // Wczytaj drugą liczbę + if (!(iss >> b)) return ErrorCode::BadFormat; - b = std::stod(tokenB); - // brak śmieci po liczbie + // Sprawdź, czy operator jest poprawny + if (operations.find(op) == operations.end()) + return ErrorCode::BadCharacter; + + // Sprawdź, czy nie ma śmieci po drugiej liczbie std::string rest; if (iss >> rest) return ErrorCode::BadFormat; + // Wykonaj operację return operations[op](a, b, out); } From ec71edd1074b3a84ff1d6b3b036a2868bc82fc67 Mon Sep 17 00:00:00 2001 From: Jarek Jeda Date: Mon, 6 Oct 2025 14:49:38 +0200 Subject: [PATCH 090/104] error fixes --- .../advancedCalculator.cpp | 31 ++++++++++++------- 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/homework/advanced-calculator/advancedCalculator.cpp b/homework/advanced-calculator/advancedCalculator.cpp index 0f9b7e653..3afaece8c 100644 --- a/homework/advanced-calculator/advancedCalculator.cpp +++ b/homework/advanced-calculator/advancedCalculator.cpp @@ -92,21 +92,32 @@ ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { if (input.empty()) return ErrorCode::BadFormat; + // sprawdzenie niedozwolonych znaków + for (char c : input) { + if (!(std::isdigit(c) || std::isspace(c) || c == '.' || operations.count(c))) { + return ErrorCode::BadCharacter; + } + } + std::istringstream iss(input); double a = 0.0, b = 0.0; char op = 0; - // Wczytaj pierwszą liczbę + // pierwsza liczba if (!(iss >> a)) return ErrorCode::BadFormat; - // Wczytaj operator + // operator if (!(iss >> op)) { - *out = a; - return ErrorCode::OK; // tylko jedna liczba + *out = a; // tylko liczba + return ErrorCode::OK; } - // Operator unarny + // nieznany operator + if (operations.find(op) == operations.end()) + return ErrorCode::BadCharacter; + + // operator unarny if (op == '!') { std::string extra; if (iss >> extra) @@ -114,20 +125,16 @@ ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { return operations['!'](a, 0, out); } - // Wczytaj drugą liczbę + // druga liczba if (!(iss >> b)) return ErrorCode::BadFormat; - // Sprawdź, czy operator jest poprawny - if (operations.find(op) == operations.end()) - return ErrorCode::BadCharacter; - - // Sprawdź, czy nie ma śmieci po drugiej liczbie + // sprawdź, czy po drugiej liczbie coś jeszcze jest std::string rest; if (iss >> rest) return ErrorCode::BadFormat; - // Wykonaj operację + // oblicz wynik return operations[op](a, b, out); } From 6e902be36ea71431e94335f8284504e231ab8024 Mon Sep 17 00:00:00 2001 From: Jarek Jeda Date: Mon, 6 Oct 2025 15:28:44 +0200 Subject: [PATCH 091/104] error fixes --- homework/advanced-calculator/advancedCalculator.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/homework/advanced-calculator/advancedCalculator.cpp b/homework/advanced-calculator/advancedCalculator.cpp index 3afaece8c..e8df88f80 100644 --- a/homework/advanced-calculator/advancedCalculator.cpp +++ b/homework/advanced-calculator/advancedCalculator.cpp @@ -94,6 +94,8 @@ ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { // sprawdzenie niedozwolonych znaków for (char c : input) { + if (c == ',') + return ErrorCode::BadFormat; // przecinek to błąd formatu, nie znak if (!(std::isdigit(c) || std::isspace(c) || c == '.' || operations.count(c))) { return ErrorCode::BadCharacter; } From a4b7f7891b75bd3e850c4577242e384136264d4f Mon Sep 17 00:00:00 2001 From: Jarek Jeda Date: Mon, 6 Oct 2025 15:41:22 +0200 Subject: [PATCH 092/104] error fixes --- .../advancedCalculator.cpp | 21 ++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/homework/advanced-calculator/advancedCalculator.cpp b/homework/advanced-calculator/advancedCalculator.cpp index e8df88f80..235981e72 100644 --- a/homework/advanced-calculator/advancedCalculator.cpp +++ b/homework/advanced-calculator/advancedCalculator.cpp @@ -92,12 +92,23 @@ ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { if (input.empty()) return ErrorCode::BadFormat; - // sprawdzenie niedozwolonych znaków - for (char c : input) { - if (c == ',') - return ErrorCode::BadFormat; // przecinek to błąd formatu, nie znak + for (size_t i = 0; i < input.size(); ++i) { + char c = input[i]; + + if (c == ',') { + return ErrorCode::BadFormat; // przecinek = błąd formatu + } + if (!(std::isdigit(c) || std::isspace(c) || c == '.' || operations.count(c))) { - return ErrorCode::BadCharacter; + return ErrorCode::BadCharacter; // np. ';', '#' itp. + } + + // jeśli '+' występuje na początku liczby (np. "+8") + if (c == '+' && (i == 0 || std::isspace(input[i - 1]))) { + // jeśli po '+' jest cyfra, to to jest zły format + if (i + 1 < input.size() && std::isdigit(input[i + 1])) { + return ErrorCode::BadFormat; + } } } From 105c69dd93e7a0c5dc94e8305638f9ef4d918b00 Mon Sep 17 00:00:00 2001 From: Jarek Jeda Date: Mon, 6 Oct 2025 15:47:03 +0200 Subject: [PATCH 093/104] errors --- .../advancedCalculator.cpp | 22 ++++++++++++++----- 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/homework/advanced-calculator/advancedCalculator.cpp b/homework/advanced-calculator/advancedCalculator.cpp index 235981e72..93525e871 100644 --- a/homework/advanced-calculator/advancedCalculator.cpp +++ b/homework/advanced-calculator/advancedCalculator.cpp @@ -95,17 +95,27 @@ ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { for (size_t i = 0; i < input.size(); ++i) { char c = input[i]; - if (c == ',') { - return ErrorCode::BadFormat; // przecinek = błąd formatu + // Zakazane znaki → BadCharacter + if (c == ',' || c == ';' || (!std::isdigit(c) && !std::isspace(c) && c != '.' && !operations.count(c))) { + return ErrorCode::BadCharacter; } - if (!(std::isdigit(c) || std::isspace(c) || c == '.' || operations.count(c))) { - return ErrorCode::BadCharacter; // np. ';', '#' itp. + // Zły format: więcej niż jedna kropka w liczbie + if (c == '.') { + // sprawdzamy, czy w aktualnej liczbie jest już kropka + int dotCount = 0; + // szukamy wstecz aż do operatora lub początku + for (int j = i - 1; j >= 0 && (std::isdigit(input[j]) || input[j] == '.'); --j) { + if (input[j] == '.') + dotCount++; + } + if (dotCount >= 1) { + return ErrorCode::BadFormat; // druga kropka → zły format liczby + } } - // jeśli '+' występuje na początku liczby (np. "+8") + // '+' na początku liczby → BadFormat if (c == '+' && (i == 0 || std::isspace(input[i - 1]))) { - // jeśli po '+' jest cyfra, to to jest zły format if (i + 1 < input.size() && std::isdigit(input[i + 1])) { return ErrorCode::BadFormat; } From c0037e95bd85b18bbd00f5c8e6b9f703209ef46e Mon Sep 17 00:00:00 2001 From: Jarek Jeda Date: Mon, 6 Oct 2025 15:49:38 +0200 Subject: [PATCH 094/104] errors --- .../advancedCalculator.cpp | 33 +++++++------------ 1 file changed, 11 insertions(+), 22 deletions(-) diff --git a/homework/advanced-calculator/advancedCalculator.cpp b/homework/advanced-calculator/advancedCalculator.cpp index 93525e871..7fa4716a5 100644 --- a/homework/advanced-calculator/advancedCalculator.cpp +++ b/homework/advanced-calculator/advancedCalculator.cpp @@ -95,30 +95,19 @@ ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { for (size_t i = 0; i < input.size(); ++i) { char c = input[i]; - // Zakazane znaki → BadCharacter - if (c == ',' || c == ';' || (!std::isdigit(c) && !std::isspace(c) && c != '.' && !operations.count(c))) { - return ErrorCode::BadCharacter; + // --- Przecinek: BadFormat jeśli w liczbie, BadCharacter jeśli poza nią --- + if (c == ',') { + bool beforeIsDigit = (i > 0 && std::isdigit(input[i - 1])); + bool afterIsDigit = (i + 1 < input.size() && std::isdigit(input[i + 1])); + if (beforeIsDigit && afterIsDigit) + return ErrorCode::BadFormat; // np. "5,1!" + else + return ErrorCode::BadCharacter; // np. "123,4 ; 345" } - // Zły format: więcej niż jedna kropka w liczbie - if (c == '.') { - // sprawdzamy, czy w aktualnej liczbie jest już kropka - int dotCount = 0; - // szukamy wstecz aż do operatora lub początku - for (int j = i - 1; j >= 0 && (std::isdigit(input[j]) || input[j] == '.'); --j) { - if (input[j] == '.') - dotCount++; - } - if (dotCount >= 1) { - return ErrorCode::BadFormat; // druga kropka → zły format liczby - } - } - - // '+' na początku liczby → BadFormat - if (c == '+' && (i == 0 || std::isspace(input[i - 1]))) { - if (i + 1 < input.size() && std::isdigit(input[i + 1])) { - return ErrorCode::BadFormat; - } + // --- Zły znak --- + if (!(std::isdigit(c) || std::isspace(c) || c == '.' || operations.count(c))) { + return ErrorCode::BadCharacter; } } From 1882e1074ae6a95a95a07f2ffb4e647aa93533e7 Mon Sep 17 00:00:00 2001 From: Jarek Jeda Date: Mon, 6 Oct 2025 15:53:32 +0200 Subject: [PATCH 095/104] errors --- .../advanced-calculator/advancedCalculator.cpp | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/homework/advanced-calculator/advancedCalculator.cpp b/homework/advanced-calculator/advancedCalculator.cpp index 7fa4716a5..4067adbb7 100644 --- a/homework/advanced-calculator/advancedCalculator.cpp +++ b/homework/advanced-calculator/advancedCalculator.cpp @@ -89,13 +89,23 @@ AdvancedCalculator::AdvancedCalculator() { } ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { + std::string trimmed = input; + trimmed.erase(0, trimmed.find_first_not_of(" \t")); + if (!trimmed.empty() && operations.count(trimmed[0]) && trimmed[0] != '-' && trimmed[0] != '+') { + return ErrorCode::BadFormat; + } + // jeśli zaczyna się od '+' lub '-', musi być liczba po nim + if ((trimmed[0] == '+' || trimmed[0] == '-') && (trimmed.size() == 1 || !std::isdigit(trimmed[1]))) { + return ErrorCode::BadFormat; + } + if (input.empty()) return ErrorCode::BadFormat; for (size_t i = 0; i < input.size(); ++i) { char c = input[i]; - // --- Przecinek: BadFormat jeśli w liczbie, BadCharacter jeśli poza nią --- + // Przecinek — zależnie od kontekstu if (c == ',') { bool beforeIsDigit = (i > 0 && std::isdigit(input[i - 1])); bool afterIsDigit = (i + 1 < input.size() && std::isdigit(input[i + 1])); @@ -105,7 +115,7 @@ ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { return ErrorCode::BadCharacter; // np. "123,4 ; 345" } - // --- Zły znak --- + // Inne niepoprawne znaki if (!(std::isdigit(c) || std::isspace(c) || c == '.' || operations.count(c))) { return ErrorCode::BadCharacter; } From 1b351c05e84809573d21349ac7d46c5edda6e8cc Mon Sep 17 00:00:00 2001 From: Jarek Jeda Date: Mon, 6 Oct 2025 16:32:02 +0200 Subject: [PATCH 096/104] errors --- homework/advanced-calculator/advancedCalculator.cpp | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/homework/advanced-calculator/advancedCalculator.cpp b/homework/advanced-calculator/advancedCalculator.cpp index 4067adbb7..a980b59a3 100644 --- a/homework/advanced-calculator/advancedCalculator.cpp +++ b/homework/advanced-calculator/advancedCalculator.cpp @@ -91,11 +91,9 @@ AdvancedCalculator::AdvancedCalculator() { ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { std::string trimmed = input; trimmed.erase(0, trimmed.find_first_not_of(" \t")); - if (!trimmed.empty() && operations.count(trimmed[0]) && trimmed[0] != '-' && trimmed[0] != '+') { - return ErrorCode::BadFormat; - } - // jeśli zaczyna się od '+' lub '-', musi być liczba po nim - if ((trimmed[0] == '+' || trimmed[0] == '-') && (trimmed.size() == 1 || !std::isdigit(trimmed[1]))) { + + // Jeśli wyrażenie zaczyna się od operatora (np. "+8 - 32.1"), uznaj to za błąd formatu + if (!trimmed.empty() && operations.count(trimmed[0])) { return ErrorCode::BadFormat; } From f5ba8c68b2f783b3c581c106b45624068346fe5b Mon Sep 17 00:00:00 2001 From: Jarek Jeda Date: Mon, 6 Oct 2025 16:34:47 +0200 Subject: [PATCH 097/104] errors_FIXES --- .../advancedCalculator.cpp | 89 +++++++++++-------- 1 file changed, 54 insertions(+), 35 deletions(-) diff --git a/homework/advanced-calculator/advancedCalculator.cpp b/homework/advanced-calculator/advancedCalculator.cpp index a980b59a3..c979c0a91 100644 --- a/homework/advanced-calculator/advancedCalculator.cpp +++ b/homework/advanced-calculator/advancedCalculator.cpp @@ -89,32 +89,12 @@ AdvancedCalculator::AdvancedCalculator() { } ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { - std::string trimmed = input; - trimmed.erase(0, trimmed.find_first_not_of(" \t")); - - // Jeśli wyrażenie zaczyna się od operatora (np. "+8 - 32.1"), uznaj to za błąd formatu - if (!trimmed.empty() && operations.count(trimmed[0])) { - return ErrorCode::BadFormat; - } - if (input.empty()) return ErrorCode::BadFormat; - for (size_t i = 0; i < input.size(); ++i) { - char c = input[i]; - - // Przecinek — zależnie od kontekstu - if (c == ',') { - bool beforeIsDigit = (i > 0 && std::isdigit(input[i - 1])); - bool afterIsDigit = (i + 1 < input.size() && std::isdigit(input[i + 1])); - if (beforeIsDigit && afterIsDigit) - return ErrorCode::BadFormat; // np. "5,1!" - else - return ErrorCode::BadCharacter; // np. "123,4 ; 345" - } - - // Inne niepoprawne znaki - if (!(std::isdigit(c) || std::isspace(c) || c == '.' || operations.count(c))) { + // 1. Sprawdzenie niedozwolonych znaków (tylko znaki niebędące cyframi, kropką, spacją lub operatorem) + for (char c : input) { + if (!(std::isdigit(c) || std::isspace(c) || c == '.' || operations.count(c) || c == '-' || c == '+')) { return ErrorCode::BadCharacter; } } @@ -123,38 +103,77 @@ ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { double a = 0.0, b = 0.0; char op = 0; - // pierwsza liczba - if (!(iss >> a)) + // 2. Wczytanie pierwszej liczby + std::string tokenA; + if (!(iss >> tokenA)) + return ErrorCode::BadFormat; + + // Sprawdzenie liczby pod kątem wielu kropek + int dotCount = 0; + for (size_t i = 0; i < tokenA.size(); ++i) { + if (tokenA[i] == '.') + dotCount++; + if (dotCount > 1) + return ErrorCode::BadFormat; + if (!std::isdigit(tokenA[i]) && !(i == 0 && (tokenA[i] == '-' || tokenA[i] == '+')) && tokenA[i] != '.') { + return ErrorCode::BadFormat; + } + } + + // Zamiana string na double + try { + a = std::stod(tokenA); + } catch (...) { return ErrorCode::BadFormat; + } - // operator + // 3. Wczytanie operatora if (!(iss >> op)) { - *out = a; // tylko liczba + *out = a; // tylko jedna liczba → OK return ErrorCode::OK; } - // nieznany operator + // Nieznany operator if (operations.find(op) == operations.end()) return ErrorCode::BadCharacter; - // operator unarny + // Operator unarny ! if (op == '!') { - std::string extra; - if (iss >> extra) + std::string leftover; + if (iss >> leftover) return ErrorCode::BadFormat; // np. "5! 2" return operations['!'](a, 0, out); } - // druga liczba - if (!(iss >> b)) + // 4. Wczytanie drugiej liczby + std::string tokenB; + if (!(iss >> tokenB)) return ErrorCode::BadFormat; - // sprawdź, czy po drugiej liczbie coś jeszcze jest + // Sprawdzenie drugiej liczby pod kątem wielu kropek + dotCount = 0; + for (size_t i = 0; i < tokenB.size(); ++i) { + if (tokenB[i] == '.') + dotCount++; + if (dotCount > 1) + return ErrorCode::BadFormat; + if (!std::isdigit(tokenB[i]) && !(i == 0 && (tokenB[i] == '-' || tokenB[i] == '+')) && tokenB[i] != '.') { + return ErrorCode::BadFormat; + } + } + + try { + b = std::stod(tokenB); + } catch (...) { + return ErrorCode::BadFormat; + } + + // 5. Sprawdzenie dodatkowych znaków po drugiej liczbie std::string rest; if (iss >> rest) return ErrorCode::BadFormat; - // oblicz wynik + // 6. Obliczenie wyniku return operations[op](a, b, out); } From 37a7dc0e73cb31e3818e42ee43faddc0c66dcbe0 Mon Sep 17 00:00:00 2001 From: Jarek Jeda Date: Tue, 21 Oct 2025 20:28:49 +0200 Subject: [PATCH 098/104] error_fixes --- .../advancedCalculator.cpp | 67 ++++--------------- 1 file changed, 14 insertions(+), 53 deletions(-) diff --git a/homework/advanced-calculator/advancedCalculator.cpp b/homework/advanced-calculator/advancedCalculator.cpp index c979c0a91..3afaece8c 100644 --- a/homework/advanced-calculator/advancedCalculator.cpp +++ b/homework/advanced-calculator/advancedCalculator.cpp @@ -92,9 +92,9 @@ ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { if (input.empty()) return ErrorCode::BadFormat; - // 1. Sprawdzenie niedozwolonych znaków (tylko znaki niebędące cyframi, kropką, spacją lub operatorem) + // sprawdzenie niedozwolonych znaków for (char c : input) { - if (!(std::isdigit(c) || std::isspace(c) || c == '.' || operations.count(c) || c == '-' || c == '+')) { + if (!(std::isdigit(c) || std::isspace(c) || c == '.' || operations.count(c))) { return ErrorCode::BadCharacter; } } @@ -103,77 +103,38 @@ ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { double a = 0.0, b = 0.0; char op = 0; - // 2. Wczytanie pierwszej liczby - std::string tokenA; - if (!(iss >> tokenA)) + // pierwsza liczba + if (!(iss >> a)) return ErrorCode::BadFormat; - // Sprawdzenie liczby pod kątem wielu kropek - int dotCount = 0; - for (size_t i = 0; i < tokenA.size(); ++i) { - if (tokenA[i] == '.') - dotCount++; - if (dotCount > 1) - return ErrorCode::BadFormat; - if (!std::isdigit(tokenA[i]) && !(i == 0 && (tokenA[i] == '-' || tokenA[i] == '+')) && tokenA[i] != '.') { - return ErrorCode::BadFormat; - } - } - - // Zamiana string na double - try { - a = std::stod(tokenA); - } catch (...) { - return ErrorCode::BadFormat; - } - - // 3. Wczytanie operatora + // operator if (!(iss >> op)) { - *out = a; // tylko jedna liczba → OK + *out = a; // tylko liczba return ErrorCode::OK; } - // Nieznany operator + // nieznany operator if (operations.find(op) == operations.end()) return ErrorCode::BadCharacter; - // Operator unarny ! + // operator unarny if (op == '!') { - std::string leftover; - if (iss >> leftover) + std::string extra; + if (iss >> extra) return ErrorCode::BadFormat; // np. "5! 2" return operations['!'](a, 0, out); } - // 4. Wczytanie drugiej liczby - std::string tokenB; - if (!(iss >> tokenB)) + // druga liczba + if (!(iss >> b)) return ErrorCode::BadFormat; - // Sprawdzenie drugiej liczby pod kątem wielu kropek - dotCount = 0; - for (size_t i = 0; i < tokenB.size(); ++i) { - if (tokenB[i] == '.') - dotCount++; - if (dotCount > 1) - return ErrorCode::BadFormat; - if (!std::isdigit(tokenB[i]) && !(i == 0 && (tokenB[i] == '-' || tokenB[i] == '+')) && tokenB[i] != '.') { - return ErrorCode::BadFormat; - } - } - - try { - b = std::stod(tokenB); - } catch (...) { - return ErrorCode::BadFormat; - } - - // 5. Sprawdzenie dodatkowych znaków po drugiej liczbie + // sprawdź, czy po drugiej liczbie coś jeszcze jest std::string rest; if (iss >> rest) return ErrorCode::BadFormat; - // 6. Obliczenie wyniku + // oblicz wynik return operations[op](a, b, out); } From 705d6276035559957dbce84eedec8fcf4cf528de Mon Sep 17 00:00:00 2001 From: Jarek Jeda Date: Tue, 21 Oct 2025 20:37:50 +0200 Subject: [PATCH 099/104] error_fixes --- .../advancedCalculator.cpp | 92 +++++++++++++++++-- 1 file changed, 82 insertions(+), 10 deletions(-) diff --git a/homework/advanced-calculator/advancedCalculator.cpp b/homework/advanced-calculator/advancedCalculator.cpp index 3afaece8c..96875e029 100644 --- a/homework/advanced-calculator/advancedCalculator.cpp +++ b/homework/advanced-calculator/advancedCalculator.cpp @@ -88,12 +88,74 @@ AdvancedCalculator::AdvancedCalculator() { }; } +// ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { +// if (input.empty()) +// return ErrorCode::BadFormat; +// +// // sprawdzenie niedozwolonych znaków +// for (char c : input) { +// if (!(std::isdigit(c) || std::isspace(c) || c == '.' || operations.count(c))) { +// return ErrorCode::BadCharacter; +// } +// } +// +// std::istringstream iss(input); +// double a = 0.0, b = 0.0; +// char op = 0; +// +// // pierwsza liczba +// if (!(iss >> a)) +// return ErrorCode::BadFormat; +// +// // operator +// if (!(iss >> op)) { +// *out = a; // tylko liczba +// return ErrorCode::OK; +// } +// +// // nieznany operator +// if (operations.find(op) == operations.end()) +// return ErrorCode::BadCharacter; +// +// // operator unarny +// if (op == '!') { +// std::string extra; +// if (iss >> extra) +// return ErrorCode::BadFormat; // np. "5! 2" +// return operations['!'](a, 0, out); +// } +// +// // druga liczba +// if (!(iss >> b)) +// return ErrorCode::BadFormat; +// +// // sprawdź, czy po drugiej liczbie coś jeszcze jest +// std::string rest; +// if (iss >> rest) +// return ErrorCode::BadFormat; +// +// // oblicz wynik +// return operations[op](a, b, out); +// } + ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { if (input.empty()) return ErrorCode::BadFormat; - // sprawdzenie niedozwolonych znaków - for (char c : input) { + // sprawdzenie niedozwolonych znaków i złych formatów liczbowych + for (size_t i = 0; i < input.size(); ++i) { + char c = input[i]; + + if (c == ',') { + // przecinek między cyframi -> zły format + bool beforeDigit = (i > 0 && std::isdigit(input[i - 1])); + bool afterDigit = (i + 1 < input.size() && std::isdigit(input[i + 1])); + if (beforeDigit && afterDigit) + return ErrorCode::BadFormat; + else + return ErrorCode::BadCharacter; + } + if (!(std::isdigit(c) || std::isspace(c) || c == '.' || operations.count(c))) { return ErrorCode::BadCharacter; } @@ -103,13 +165,17 @@ ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { double a = 0.0, b = 0.0; char op = 0; - // pierwsza liczba if (!(iss >> a)) return ErrorCode::BadFormat; - // operator + // przypadek: liczba zawiera więcej niż jedną kropkę (np. "12.4.3") + std::string check = std::to_string(a); + size_t dotCount = std::count(input.begin(), input.end(), '.'); + if (dotCount > 1 && input.find_first_of("+-*/%^$!") != std::string::npos) + return ErrorCode::BadFormat; + if (!(iss >> op)) { - *out = a; // tylko liczba + *out = a; return ErrorCode::OK; } @@ -117,24 +183,30 @@ ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { if (operations.find(op) == operations.end()) return ErrorCode::BadCharacter; - // operator unarny + // operator unarny (!) if (op == '!') { std::string extra; if (iss >> extra) - return ErrorCode::BadFormat; // np. "5! 2" + return ErrorCode::BadFormat; // np. "123.4 ! 345" return operations['!'](a, 0, out); } - // druga liczba + // dwuznakowe operatory, np. ++ lub ^% + char nextChar = 0; + if (iss >> std::ws && iss.peek() != EOF) { + nextChar = iss.peek(); + if (operations.count(nextChar) && nextChar != '-') // drugi operator po operatorze + return ErrorCode::BadFormat; + } + if (!(iss >> b)) return ErrorCode::BadFormat; - // sprawdź, czy po drugiej liczbie coś jeszcze jest + // coś jeszcze po drugiej liczbie std::string rest; if (iss >> rest) return ErrorCode::BadFormat; - // oblicz wynik return operations[op](a, b, out); } From dc2c050a72285135b3e426fd9f1c7bebb0d381ee Mon Sep 17 00:00:00 2001 From: Jarek Jeda Date: Tue, 21 Oct 2025 20:41:02 +0200 Subject: [PATCH 100/104] error_fixes --- .../advancedCalculator.cpp | 141 ++++-------------- 1 file changed, 28 insertions(+), 113 deletions(-) diff --git a/homework/advanced-calculator/advancedCalculator.cpp b/homework/advanced-calculator/advancedCalculator.cpp index 96875e029..2bd59d4b2 100644 --- a/homework/advanced-calculator/advancedCalculator.cpp +++ b/homework/advanced-calculator/advancedCalculator.cpp @@ -1,5 +1,6 @@ #include "advancedCalculator.hpp" - +#include +#include #include #include @@ -23,31 +24,20 @@ std::string errorCodeToString(ErrorCode code) { } AdvancedCalculator::AdvancedCalculator() { - operations['+'] = [](double a, double b, double* out) { - *out = a + b; - return ErrorCode::OK; - }; - operations['-'] = [](double a, double b, double* out) { - *out = a - b; - return ErrorCode::OK; - }; - operations['*'] = [](double a, double b, double* out) { - *out = a * b; - return ErrorCode::OK; - }; + operations['+'] = [](double a, double b, double* out) { *out = a + b; return ErrorCode::OK; }; + operations['-'] = [](double a, double b, double* out) { *out = a - b; return ErrorCode::OK; }; + operations['*'] = [](double a, double b, double* out) { *out = a * b; return ErrorCode::OK; }; operations['/'] = [](double a, double b, double* out) { - if (b == 0.0) { + if (b == 0.0) return ErrorCode::DivideBy0; - } *out = a / b; return ErrorCode::OK; }; operations['%'] = [](double a, double b, double* out) { - if (std::floor(a) != a || std::floor(b) != b) { + if (std::floor(a) != a || std::floor(b) != b) return ErrorCode::ModuleOfNonIntegerValue; - } else if (b == 0.0) { + if (b == 0.0) return ErrorCode::DivideBy0; - } *out = static_cast(a) % static_cast(b); return ErrorCode::OK; }; @@ -56,106 +46,47 @@ AdvancedCalculator::AdvancedCalculator() { return ErrorCode::OK; }; operations['$'] = [](double a, double b, double* out) { - if (a < 0) { - return ErrorCode::SqrtOfNegativeNumber; - } - if (b == 0.0) { + if (b == 0.0) return ErrorCode::DivideBy0; - } else if (a < 0 && static_cast(b) % 2 == 0) { + if (a < 0 && std::fmod(b, 2) == 0) return ErrorCode::SqrtOfNegativeNumber; - } *out = std::pow(a, 1.0 / b); return ErrorCode::OK; }; operations['!'] = [](double a, double, double* out) { - if (std::isnan(a) || std::isinf(a)) { + if (a < 0.0) return ErrorCode::BadFormat; + if (a == 0.0 || a == 1.0) { + *out = 1; + return ErrorCode::OK; } - - long double result = 1.0; - double x = std::fabs(a); - - if (x == std::floor(x)) { - for (int i = 1; i <= static_cast(x); ++i) { - result *= i; - } - } else { - result = tgamma(x + 1); + if (a != std::floor(a)) { + *out = std::tgamma(a + 1); + return ErrorCode::OK; } - - *out = (a < 0 ? -result : result); + long double result = 1; + for (int i = 1; i <= static_cast(a); ++i) + result *= i; + *out = result; return ErrorCode::OK; }; } -// ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { -// if (input.empty()) -// return ErrorCode::BadFormat; -// -// // sprawdzenie niedozwolonych znaków -// for (char c : input) { -// if (!(std::isdigit(c) || std::isspace(c) || c == '.' || operations.count(c))) { -// return ErrorCode::BadCharacter; -// } -// } -// -// std::istringstream iss(input); -// double a = 0.0, b = 0.0; -// char op = 0; -// -// // pierwsza liczba -// if (!(iss >> a)) -// return ErrorCode::BadFormat; -// -// // operator -// if (!(iss >> op)) { -// *out = a; // tylko liczba -// return ErrorCode::OK; -// } -// -// // nieznany operator -// if (operations.find(op) == operations.end()) -// return ErrorCode::BadCharacter; -// -// // operator unarny -// if (op == '!') { -// std::string extra; -// if (iss >> extra) -// return ErrorCode::BadFormat; // np. "5! 2" -// return operations['!'](a, 0, out); -// } -// -// // druga liczba -// if (!(iss >> b)) -// return ErrorCode::BadFormat; -// -// // sprawdź, czy po drugiej liczbie coś jeszcze jest -// std::string rest; -// if (iss >> rest) -// return ErrorCode::BadFormat; -// -// // oblicz wynik -// return operations[op](a, b, out); -// } - ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { if (input.empty()) return ErrorCode::BadFormat; - // sprawdzenie niedozwolonych znaków i złych formatów liczbowych + // wykrycie nieprawidłowych znaków lub przecinków for (size_t i = 0; i < input.size(); ++i) { char c = input[i]; - if (c == ',') { - // przecinek między cyframi -> zły format bool beforeDigit = (i > 0 && std::isdigit(input[i - 1])); bool afterDigit = (i + 1 < input.size() && std::isdigit(input[i + 1])); if (beforeDigit && afterDigit) - return ErrorCode::BadFormat; + return ErrorCode::BadFormat; // np. "5,1" else - return ErrorCode::BadCharacter; + return ErrorCode::BadCharacter; // np. "5, + 2" } - if (!(std::isdigit(c) || std::isspace(c) || c == '.' || operations.count(c))) { return ErrorCode::BadCharacter; } @@ -168,41 +99,25 @@ ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { if (!(iss >> a)) return ErrorCode::BadFormat; - // przypadek: liczba zawiera więcej niż jedną kropkę (np. "12.4.3") - std::string check = std::to_string(a); - size_t dotCount = std::count(input.begin(), input.end(), '.'); - if (dotCount > 1 && input.find_first_of("+-*/%^$!") != std::string::npos) - return ErrorCode::BadFormat; - if (!(iss >> op)) { *out = a; return ErrorCode::OK; } - // nieznany operator if (operations.find(op) == operations.end()) return ErrorCode::BadCharacter; - // operator unarny (!) if (op == '!') { - std::string extra; - if (iss >> extra) - return ErrorCode::BadFormat; // np. "123.4 ! 345" - return operations['!'](a, 0, out); - } - - // dwuznakowe operatory, np. ++ lub ^% - char nextChar = 0; - if (iss >> std::ws && iss.peek() != EOF) { - nextChar = iss.peek(); - if (operations.count(nextChar) && nextChar != '-') // drugi operator po operatorze + std::string rest; + if (iss >> rest) return ErrorCode::BadFormat; + return operations['!'](a, 0, out); } if (!(iss >> b)) return ErrorCode::BadFormat; - // coś jeszcze po drugiej liczbie + // dodatkowe operatory po drugiej liczbie std::string rest; if (iss >> rest) return ErrorCode::BadFormat; From cf7d2c3ddf5ee3f5cff39fe2d51adac33df865ca Mon Sep 17 00:00:00 2001 From: Jarek Jeda Date: Tue, 21 Oct 2025 20:47:34 +0200 Subject: [PATCH 101/104] error_fixes --- .../Testing/Temporary/CTestCostData.txt | 1 + .../Testing/Temporary/LastTest.log | 3 +++ .../advancedCalculator.cpp | 27 ++++++++----------- 3 files changed, 15 insertions(+), 16 deletions(-) create mode 100644 homework/advanced-calculator/Testing/Temporary/CTestCostData.txt create mode 100644 homework/advanced-calculator/Testing/Temporary/LastTest.log diff --git a/homework/advanced-calculator/Testing/Temporary/CTestCostData.txt b/homework/advanced-calculator/Testing/Temporary/CTestCostData.txt new file mode 100644 index 000000000..ed97d539c --- /dev/null +++ b/homework/advanced-calculator/Testing/Temporary/CTestCostData.txt @@ -0,0 +1 @@ +--- diff --git a/homework/advanced-calculator/Testing/Temporary/LastTest.log b/homework/advanced-calculator/Testing/Temporary/LastTest.log new file mode 100644 index 000000000..7058ed947 --- /dev/null +++ b/homework/advanced-calculator/Testing/Temporary/LastTest.log @@ -0,0 +1,3 @@ +Start testing: Oct 21 20:43 W. Europe Summer Time +---------------------------------------------------------- +End testing: Oct 21 20:43 W. Europe Summer Time diff --git a/homework/advanced-calculator/advancedCalculator.cpp b/homework/advanced-calculator/advancedCalculator.cpp index 2bd59d4b2..d6f1fb9cf 100644 --- a/homework/advanced-calculator/advancedCalculator.cpp +++ b/homework/advanced-calculator/advancedCalculator.cpp @@ -1,5 +1,4 @@ #include "advancedCalculator.hpp" -#include #include #include #include @@ -48,7 +47,7 @@ AdvancedCalculator::AdvancedCalculator() { operations['$'] = [](double a, double b, double* out) { if (b == 0.0) return ErrorCode::DivideBy0; - if (a < 0 && std::fmod(b, 2) == 0) + if (a < 0 && std::floor(b) == b && static_cast(b) % 2 == 0) return ErrorCode::SqrtOfNegativeNumber; *out = std::pow(a, 1.0 / b); return ErrorCode::OK; @@ -56,14 +55,8 @@ AdvancedCalculator::AdvancedCalculator() { operations['!'] = [](double a, double, double* out) { if (a < 0.0) return ErrorCode::BadFormat; - if (a == 0.0 || a == 1.0) { - *out = 1; - return ErrorCode::OK; - } - if (a != std::floor(a)) { - *out = std::tgamma(a + 1); - return ErrorCode::OK; - } + if (a != std::floor(a)) + return ErrorCode::BadFormat; long double result = 1; for (int i = 1; i <= static_cast(a); ++i) result *= i; @@ -76,20 +69,23 @@ ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { if (input.empty()) return ErrorCode::BadFormat; - // wykrycie nieprawidłowych znaków lub przecinków + std::string trimmed = input; + trimmed.erase(0, trimmed.find_first_not_of(" \t")); + if (!trimmed.empty() && trimmed[0] == '+') + return ErrorCode::BadFormat; + for (size_t i = 0; i < input.size(); ++i) { char c = input[i]; if (c == ',') { bool beforeDigit = (i > 0 && std::isdigit(input[i - 1])); bool afterDigit = (i + 1 < input.size() && std::isdigit(input[i + 1])); if (beforeDigit && afterDigit) - return ErrorCode::BadFormat; // np. "5,1" + return ErrorCode::BadFormat; else - return ErrorCode::BadCharacter; // np. "5, + 2" + return ErrorCode::BadCharacter; } - if (!(std::isdigit(c) || std::isspace(c) || c == '.' || operations.count(c))) { + if (!(std::isdigit(c) || std::isspace(c) || c == '.' || operations.count(c))) return ErrorCode::BadCharacter; - } } std::istringstream iss(input); @@ -117,7 +113,6 @@ ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { if (!(iss >> b)) return ErrorCode::BadFormat; - // dodatkowe operatory po drugiej liczbie std::string rest; if (iss >> rest) return ErrorCode::BadFormat; From 183fe0496974c13eb205a7877e01ebda724e0775 Mon Sep 17 00:00:00 2001 From: Jarek Jeda Date: Tue, 21 Oct 2025 20:50:41 +0200 Subject: [PATCH 102/104] error_fixes --- .../Testing/Temporary/LastTest.log | 4 ++-- .../advancedCalculator.cpp | 21 +++++++++---------- 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/homework/advanced-calculator/Testing/Temporary/LastTest.log b/homework/advanced-calculator/Testing/Temporary/LastTest.log index 7058ed947..234fd0f08 100644 --- a/homework/advanced-calculator/Testing/Temporary/LastTest.log +++ b/homework/advanced-calculator/Testing/Temporary/LastTest.log @@ -1,3 +1,3 @@ -Start testing: Oct 21 20:43 W. Europe Summer Time +Start testing: Oct 21 20:47 W. Europe Summer Time ---------------------------------------------------------- -End testing: Oct 21 20:43 W. Europe Summer Time +End testing: Oct 21 20:47 W. Europe Summer Time diff --git a/homework/advanced-calculator/advancedCalculator.cpp b/homework/advanced-calculator/advancedCalculator.cpp index d6f1fb9cf..4d3d6441c 100644 --- a/homework/advanced-calculator/advancedCalculator.cpp +++ b/homework/advanced-calculator/advancedCalculator.cpp @@ -44,18 +44,8 @@ AdvancedCalculator::AdvancedCalculator() { *out = std::pow(a, b); return ErrorCode::OK; }; - operations['$'] = [](double a, double b, double* out) { - if (b == 0.0) - return ErrorCode::DivideBy0; - if (a < 0 && std::floor(b) == b && static_cast(b) % 2 == 0) - return ErrorCode::SqrtOfNegativeNumber; - *out = std::pow(a, 1.0 / b); - return ErrorCode::OK; - }; operations['!'] = [](double a, double, double* out) { - if (a < 0.0) - return ErrorCode::BadFormat; - if (a != std::floor(a)) + if (a < 0.0 || std::floor(a) != a) return ErrorCode::BadFormat; long double result = 1; for (int i = 1; i <= static_cast(a); ++i) @@ -63,6 +53,15 @@ AdvancedCalculator::AdvancedCalculator() { *out = result; return ErrorCode::OK; }; + + operations['$'] = [](double a, double b, double* out) { + if (b == 0.0) + return ErrorCode::DivideBy0; + if (a < 0 && std::floor(b) == b && static_cast(b) % 2 == 0) + return ErrorCode::SqrtOfNegativeNumber; + *out = std::pow(a, 1.0 / b); + return ErrorCode::OK; + }; } ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { From 14bd646bb78df5abe920ce27d3c61b3d1ac21b4e Mon Sep 17 00:00:00 2001 From: Jarek Jeda Date: Tue, 21 Oct 2025 20:52:04 +0200 Subject: [PATCH 103/104] error_fixes --- .../advancedCalculator.cpp | 37 +++++++++++-------- 1 file changed, 22 insertions(+), 15 deletions(-) diff --git a/homework/advanced-calculator/advancedCalculator.cpp b/homework/advanced-calculator/advancedCalculator.cpp index 4d3d6441c..0f3f873b5 100644 --- a/homework/advanced-calculator/advancedCalculator.cpp +++ b/homework/advanced-calculator/advancedCalculator.cpp @@ -1,5 +1,4 @@ #include "advancedCalculator.hpp" -#include #include #include @@ -23,9 +22,18 @@ std::string errorCodeToString(ErrorCode code) { } AdvancedCalculator::AdvancedCalculator() { - operations['+'] = [](double a, double b, double* out) { *out = a + b; return ErrorCode::OK; }; - operations['-'] = [](double a, double b, double* out) { *out = a - b; return ErrorCode::OK; }; - operations['*'] = [](double a, double b, double* out) { *out = a * b; return ErrorCode::OK; }; + operations['+'] = [](double a, double b, double* out) { + *out = a + b; + return ErrorCode::OK; + }; + operations['-'] = [](double a, double b, double* out) { + *out = a - b; + return ErrorCode::OK; + }; + operations['*'] = [](double a, double b, double* out) { + *out = a * b; + return ErrorCode::OK; + }; operations['/'] = [](double a, double b, double* out) { if (b == 0.0) return ErrorCode::DivideBy0; @@ -44,6 +52,14 @@ AdvancedCalculator::AdvancedCalculator() { *out = std::pow(a, b); return ErrorCode::OK; }; + operations['$'] = [](double a, double b, double* out) { + if (b == 0.0) + return ErrorCode::DivideBy0; + if (a < 0 && std::floor(b) == b && static_cast(b) % 2 == 0) + return ErrorCode::SqrtOfNegativeNumber; + *out = std::pow(a, 1.0 / b); + return ErrorCode::OK; + }; operations['!'] = [](double a, double, double* out) { if (a < 0.0 || std::floor(a) != a) return ErrorCode::BadFormat; @@ -53,15 +69,6 @@ AdvancedCalculator::AdvancedCalculator() { *out = result; return ErrorCode::OK; }; - - operations['$'] = [](double a, double b, double* out) { - if (b == 0.0) - return ErrorCode::DivideBy0; - if (a < 0 && std::floor(b) == b && static_cast(b) % 2 == 0) - return ErrorCode::SqrtOfNegativeNumber; - *out = std::pow(a, 1.0 / b); - return ErrorCode::OK; - }; } ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { @@ -103,8 +110,8 @@ ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { return ErrorCode::BadCharacter; if (op == '!') { - std::string rest; - if (iss >> rest) + std::string extra; + if (iss >> extra) return ErrorCode::BadFormat; return operations['!'](a, 0, out); } From f4ed6dbc668e32eefeef42c04572fd1888d571ba Mon Sep 17 00:00:00 2001 From: Jarek Jeda Date: Tue, 21 Oct 2025 20:54:38 +0200 Subject: [PATCH 104/104] error_fixes --- .../advancedCalculator.cpp | 43 +++++++++++++++---- 1 file changed, 34 insertions(+), 9 deletions(-) diff --git a/homework/advanced-calculator/advancedCalculator.cpp b/homework/advanced-calculator/advancedCalculator.cpp index 0f3f873b5..15cc270e2 100644 --- a/homework/advanced-calculator/advancedCalculator.cpp +++ b/homework/advanced-calculator/advancedCalculator.cpp @@ -75,54 +75,79 @@ ErrorCode AdvancedCalculator::process(const std::string& input, double* out) { if (input.empty()) return ErrorCode::BadFormat; - std::string trimmed = input; - trimmed.erase(0, trimmed.find_first_not_of(" \t")); - if (!trimmed.empty() && trimmed[0] == '+') - return ErrorCode::BadFormat; - + // --- sprawdzenie znaków --- + int dotCount = 0; for (size_t i = 0; i < input.size(); ++i) { char c = input[i]; + if (c == ',') { bool beforeDigit = (i > 0 && std::isdigit(input[i - 1])); bool afterDigit = (i + 1 < input.size() && std::isdigit(input[i + 1])); if (beforeDigit && afterDigit) - return ErrorCode::BadFormat; - else + return ErrorCode::BadFormat; // np. "5,1!" + return ErrorCode::BadCharacter; + } + + if (c == '.') { + dotCount++; + if (dotCount > 1) + return ErrorCode::BadFormat; // np. "12.4.3" + } else if (!std::isdigit(c) && !std::isspace(c) && !operations.count(c)) { + dotCount = 0; + if (c != '.') return ErrorCode::BadCharacter; + } else if (std::isspace(c)) { + dotCount = 0; } - if (!(std::isdigit(c) || std::isspace(c) || c == '.' || operations.count(c))) - return ErrorCode::BadCharacter; } + // --- parser --- std::istringstream iss(input); double a = 0.0, b = 0.0; char op = 0; + // pierwsza liczba if (!(iss >> a)) return ErrorCode::BadFormat; + // operator if (!(iss >> op)) { *out = a; return ErrorCode::OK; } + // nieznany operator if (operations.find(op) == operations.end()) return ErrorCode::BadCharacter; + // silnia if (op == '!') { + if (a < 0.0 || std::floor(a) != a) + return ErrorCode::BadFormat; std::string extra; if (iss >> extra) return ErrorCode::BadFormat; return operations['!'](a, 0, out); } + // druga liczba if (!(iss >> b)) return ErrorCode::BadFormat; + // po drugiej liczbie nie może być nic więcej std::string rest; if (iss >> rest) return ErrorCode::BadFormat; + // pierwiastkowanie ujemnych liczb + if (op == '$') { + if (a < 0 && std::floor(b) == b && static_cast(b) % 2 == 0) + return ErrorCode::SqrtOfNegativeNumber; + if (b == 0.0) + return ErrorCode::DivideBy0; + } + + // obliczenie wyniku return operations[op](a, b, out); }