Compare commits

..

7 Commits

3 changed files with 23 additions and 5 deletions
+14 -1
View File
@@ -1,4 +1,5 @@
#include "basecalc.h" #include "basecalc.h"
#include <iostream>
BaseCalculator::~BaseCalculator() BaseCalculator::~BaseCalculator()
{} {}
@@ -20,11 +21,23 @@ int BaseCalculator::calculate(const Operation& op)
break; break;
case OpType::Division: case OpType::Division:
if (op.b == 0)
{
std::cerr << "Invalid operation requested! Resetting the accumulator to 0\n";
result = 0;
}
else
result = op.a / op.b; result = op.a / op.b;
break; break;
case OpType::Modulo: case OpType::Modulo:
result += op.a % op.b; if (op.b == 0)
{
std::cerr << "Invalid operation requested! Resetting the accumulator to 0\n";
result = 0;
}
else
result = op.a % op.b;
break; break;
case OpType::Special: case OpType::Special:
+6 -1
View File
@@ -71,7 +71,7 @@ Command resolveCommand()
bool shouldQuit = token.find("q") != token.npos; bool shouldQuit = token.find("q") != token.npos;
bool shouldPrint = token.find("p") != token.npos; bool shouldPrint = token.find("p") != token.npos;
char operation; char operation = 0;
for (const char c: token) for (const char c: token)
{ {
@@ -82,5 +82,10 @@ Command resolveCommand()
} }
} }
if (operation == 0)
{
operation = '+';
}
return {shouldQuit, operation, validation_result.next_arg(), shouldPrint}; return {shouldQuit, operation, validation_result.next_arg(), shouldPrint};
} }
+2 -2
View File
@@ -8,11 +8,11 @@ int main()
{ {
std::unique_ptr<BaseCalculator> calc = std::make_unique<SpecialCalculator>(); std::unique_ptr<BaseCalculator> calc = std::make_unique<SpecialCalculator>();
std::cout << "Initializing calculator"; std::cout << "Initializing calculator\n";
while (true) while (true)
{ {
std::cout << "What's your next input? (+,-,*,/,%,$,q,p)"; std::cout << "What's your next input? (+,-,*,/,%,$,q,p): ";
auto command = resolveCommand(); auto command = resolveCommand();
if (command.shouldTerminate()) if (command.shouldTerminate())