Compare commits

..

10 Commits

8 changed files with 38 additions and 7 deletions
+2
View File
@@ -1,3 +1,5 @@
#pragma once
enum class OpType
{
Addition, Subtraction, Multiplication, Division, Modulo, Special
+15 -2
View File
@@ -1,4 +1,5 @@
#include "basecalc.h"
#include <iostream>
BaseCalculator::~BaseCalculator()
{}
@@ -20,11 +21,23 @@ int BaseCalculator::calculate(const Operation& op)
break;
case OpType::Division:
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;
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;
case OpType::Special:
+2
View File
@@ -1,3 +1,5 @@
#pragma once
#include "Operation.h"
class BaseCalculator
{
+2
View File
@@ -1,3 +1,5 @@
#pragma once
class Command
{
public:
+10 -2
View File
@@ -1,6 +1,7 @@
#include "input_resolver.h"
#include <string>
#include <iostream>
#include <sstream>
#define delimiter " "
@@ -63,12 +64,14 @@ Command resolveCommand()
auto validation_result = validate_token(token);
if (!validation_result.ok())
{
throw std::runtime_error{"invalid token " + validation_result.incorrect_token()};
std::ostringstream os;
os << "invalid token "<< validation_result.incorrect_token();
throw std::runtime_error{os.str()};
}
bool shouldQuit = token.find("q") != token.npos;
bool shouldPrint = token.find("p") != token.npos;
char operation;
char operation = 0;
for (const char c: token)
{
@@ -79,5 +82,10 @@ Command resolveCommand()
}
}
if (operation == 0)
{
operation = '+';
}
return {shouldQuit, operation, validation_result.next_arg(), shouldPrint};
}
+2
View File
@@ -1,2 +1,4 @@
#pragma once
#include "command.h"
Command resolveCommand();
+3 -3
View File
@@ -6,13 +6,13 @@
int main()
{
auto calc = std::make_unique<BaseCalculator>();
std::unique_ptr<BaseCalculator> calc = std::make_unique<SpecialCalculator>();
std::cout << "Initializing calculator";
std::cout << "Initializing calculator\n";
while (true)
{
std::cout << "What's your next input? (+,-,*,/,%,$,q,p)";
std::cout << "What's your next input? (+,-,*,/,%,$,q,p): ";
auto command = resolveCommand();
if (command.shouldTerminate())
+2
View File
@@ -1,3 +1,5 @@
#pragma once
#include "basecalc.h"
class SpecialCalculator: public BaseCalculator