Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
104 commits
Select commit Hold shift + click to select a range
a9d63d8
advancedCalculator.cpp
jarek1992 Aug 12, 2025
3fb0d9c
enums, classes
jarek1992 Aug 13, 2025
1e02cdc
operations_implementations
jarek1992 Aug 13, 2025
c4f018f
operations fixes
jarek1992 Aug 13, 2025
cd01bb1
main.cpp
jarek1992 Aug 13, 2025
f54df1e
last
jarek1992 Aug 13, 2025
92a96e4
Delete .clang-format
jarek1992 Aug 13, 2025
5689513
Delete .gitignore
jarek1992 Aug 13, 2025
b8e3ac0
lst2
jarek1992 Aug 13, 2025
223e534
last2
jarek1992 Aug 13, 2025
ea2a17c
last3
jarek1992 Aug 13, 2025
aa4ce61
process
jarek1992 Aug 14, 2025
5f50d2b
process2
jarek1992 Aug 14, 2025
8e2fca2
test_checks
jarek1992 Aug 16, 2025
4d9f67a
test cdd
jarek1992 Aug 16, 2025
b2a2d61
check
jarek1992 Aug 16, 2025
405b86f
check2
jarek1992 Sep 3, 2025
d4aa495
test_9_fixed
jarek1992 Sep 3, 2025
5d17084
errors_process/!_fix
jarek1992 Sep 3, 2025
1ec0991
test 8 9 10_fixes
jarek1992 Sep 3, 2025
3480bc9
test8_fix
jarek1992 Sep 3, 2025
e3d3507
test 9,10_fixes
jarek1992 Sep 3, 2025
40633b0
test9,10_fixes
jarek1992 Sep 3, 2025
1fc2cab
test10_fixes
jarek1992 Sep 3, 2025
546a891
test9,10_fixes
jarek1992 Sep 3, 2025
5abe4de
tests_9,10_fixes
jarek1992 Sep 3, 2025
3df7945
tests
jarek1992 Sep 3, 2025
c56fd79
tests
jarek1992 Sep 3, 2025
2b6d47d
error9/10
jarek1992 Sep 24, 2025
1019001
changes
jarek1992 Sep 24, 2025
d3754a4
test
jarek1992 Sep 24, 2025
cdd86c3
ds
jarek1992 Sep 24, 2025
ac9d511
gt
jarek1992 Sep 24, 2025
1f974cd
9,10 error
jarek1992 Sep 24, 2025
2f07088
9,10 error
jarek1992 Sep 24, 2025
953ebf1
fd
jarek1992 Sep 24, 2025
c09e4f6
fd
jarek1992 Sep 24, 2025
8de2388
new
jarek1992 Sep 24, 2025
42ad96c
test9_fix
jarek1992 Sep 27, 2025
d76bbeb
test9_fix2
jarek1992 Sep 27, 2025
f64337d
fix 9,10
jarek1992 Sep 27, 2025
a510475
fix
jarek1992 Sep 27, 2025
0c03c7c
test9_fix
jarek1992 Sep 27, 2025
b48ad53
fixes
jarek1992 Sep 27, 2025
2baece6
fixes
jarek1992 Sep 27, 2025
eb7f0e6
fixes
jarek1992 Sep 27, 2025
b2697e9
fixes
jarek1992 Sep 27, 2025
bf6b39b
fixes
jarek1992 Sep 27, 2025
6e438c7
fixes
jarek1992 Sep 27, 2025
718e2fe
fixes2
jarek1992 Sep 27, 2025
3853f67
fixes2
jarek1992 Sep 27, 2025
765363b
fixes2
jarek1992 Sep 27, 2025
df3eb74
fixes2
jarek1992 Sep 27, 2025
691bd6e
fixes2
jarek1992 Sep 27, 2025
d58ac45
fixes2
jarek1992 Sep 27, 2025
1cfd961
fixes2
jarek1992 Sep 27, 2025
89d61c2
fixes2
jarek1992 Sep 27, 2025
3fd3f23
fixes42
jarek1992 Sep 27, 2025
fb44824
fixes4
jarek1992 Sep 27, 2025
d26c7bc
fixes6
jarek1992 Sep 27, 2025
0315a53
fixes6
jarek1992 Sep 27, 2025
e87caa3
fixes6
jarek1992 Sep 27, 2025
3911d6c
fixes6
jarek1992 Sep 27, 2025
4aa12da
fixes6
jarek1992 Sep 27, 2025
7095ac2
fixes6
jarek1992 Sep 27, 2025
89f7a1b
fixes6
jarek1992 Sep 27, 2025
b966aef
fix9
jarek1992 Sep 29, 2025
1101d77
fix9
jarek1992 Sep 29, 2025
51dfa3d
fix_error_9/10
jarek1992 Sep 29, 2025
5c195a2
fix_error_9
jarek1992 Sep 29, 2025
78680ff
fix_error_9
jarek1992 Sep 29, 2025
40806ad
fix_error_9
jarek1992 Sep 29, 2025
fc49e09
fix_error_9
jarek1992 Sep 29, 2025
f47df5d
fix_error_9
jarek1992 Sep 29, 2025
1e43ea2
fix_error_9
jarek1992 Sep 29, 2025
e41f0cd
fix_error_9
jarek1992 Sep 29, 2025
ec53c54
fix_error_9
jarek1992 Sep 29, 2025
9d6362e
fix_error_9
jarek1992 Sep 29, 2025
2d17603
fix_error_9
jarek1992 Sep 29, 2025
c9230e0
fix_error_9
jarek1992 Sep 30, 2025
fdc781d
fix_error_9
jarek1992 Sep 30, 2025
88c39bf
fix_error_9
jarek1992 Sep 30, 2025
dc5df73
fix_error_9
jarek1992 Sep 30, 2025
0eebfd4
fix_error_9
jarek1992 Sep 30, 2025
e660915
fix_error_9
jarek1992 Sep 30, 2025
f640825
fix_error_9
jarek1992 Sep 30, 2025
17cd954
fix_error_9
jarek1992 Oct 2, 2025
a40e735
error fixes
jarek1992 Oct 6, 2025
b760e2f
error fixes
jarek1992 Oct 6, 2025
ec71edd
error fixes
jarek1992 Oct 6, 2025
6e902be
error fixes
jarek1992 Oct 6, 2025
a4b7f78
error fixes
jarek1992 Oct 6, 2025
105c69d
errors
jarek1992 Oct 6, 2025
c0037e9
errors
jarek1992 Oct 6, 2025
1882e10
errors
jarek1992 Oct 6, 2025
1b351c0
errors
jarek1992 Oct 6, 2025
f5ba8c6
errors_FIXES
jarek1992 Oct 6, 2025
37a7dc0
error_fixes
jarek1992 Oct 21, 2025
705d627
error_fixes
jarek1992 Oct 21, 2025
dc2c050
error_fixes
jarek1992 Oct 21, 2025
cf7d2c3
error_fixes
jarek1992 Oct 21, 2025
183fe04
error_fixes
jarek1992 Oct 21, 2025
14bd646
error_fixes
jarek1992 Oct 21, 2025
f4ed6db
error_fixes
jarek1992 Oct 21, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.vs/
.idea/
*.iml
*.iws
Expand Down
4 changes: 2 additions & 2 deletions homework/advanced-calculator/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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ć

___
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
---
3 changes: 3 additions & 0 deletions homework/advanced-calculator/Testing/Temporary/LastTest.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Start testing: Oct 21 20:47 W. Europe Summer Time
----------------------------------------------------------
End testing: Oct 21 20:47 W. Europe Summer Time
157 changes: 157 additions & 0 deletions homework/advanced-calculator/advancedCalculator.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
#include "advancedCalculator.hpp"
#include <cmath>
#include <sstream>

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::DivideBy0:
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, 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;
if (b == 0.0)
return ErrorCode::DivideBy0;
*out = static_cast<long long>(a) % static_cast<long long>(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<int>(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;
long double result = 1;
for (int i = 1; i <= static_cast<int>(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 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; // 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;
}
}

// --- 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<int>(b) % 2 == 0)
return ErrorCode::SqrtOfNegativeNumber;
if (b == 0.0)
return ErrorCode::DivideBy0;
}

// obliczenie wyniku
return operations[op](a, b, out);
}

ErrorCode process(const std::string& input, double* out) {
AdvancedCalculator calc;
return calc.process(input, out);
}
28 changes: 28 additions & 0 deletions homework/advanced-calculator/advancedCalculator.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#pragma once

#include <functional>
#include <map>
#include <string>
#include <variant>

enum class ErrorCode {
OK,
BadCharacter,
BadFormat,
DivideBy0,
SqrtOfNegativeNumber,
ModuleOfNonIntegerValue
};

std::string errorCodeToString(ErrorCode code);

class AdvancedCalculator {
public:
AdvancedCalculator();
ErrorCode process(const std::string& input, double* out);

private:
std::map<char, std::function<ErrorCode(double, double, double*)>> operations;
};

ErrorCode process(const std::string& input, double* out);
26 changes: 26 additions & 0 deletions homework/advanced-calculator/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include <iostream>

#include "advancedCalculator.hpp"

int main() {
AdvancedCalculator calc;
std::string input;
double result = 0;

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;
}
4 changes: 2 additions & 2 deletions homework/advanced-calculator/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
Loading