Compare commits

...

13 Commits

10 changed files with 52 additions and 13 deletions
+3 -3
View File
@@ -2,11 +2,11 @@ all:
cc command.cpp input_resolver.cpp main.cpp -lstdc++ -std=c++17 -O3 -L./ -lCalc -o calculator cc command.cpp input_resolver.cpp main.cpp -lstdc++ -std=c++17 -O3 -L./ -lCalc -o calculator
libs: libs:
cc -c -fPIC basecalc.cpp Operation.cpp cc -c -fPIC basecalc.cpp Operation.cpp special_calculator.cpp
cc -shared basecalc.o Operation.o -o libCalc.so cc -shared basecalc.o Operation.o special_calculator.o -o libCalc.so
compile: compile:
cc *.cpp -O3 -c cc *.cpp -O3 -c
debug: debug:
cc *.cpp -g -std=c++17 -o calculator -lstdc++ cc *.cpp -g -std=c++17 -o calculator -lstdc++ -lm
+2
View File
@@ -1,3 +1,5 @@
#pragma once
enum class OpType enum class OpType
{ {
Addition, Subtraction, Multiplication, Division, Modulo, Special Addition, Subtraction, Multiplication, Division, Modulo, Special
+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:
+2
View File
@@ -1,3 +1,5 @@
#pragma once
#include "Operation.h" #include "Operation.h"
class BaseCalculator class BaseCalculator
{ {
+2
View File
@@ -1,3 +1,5 @@
#pragma once
class Command class Command
{ {
public: public:
+13 -5
View File
@@ -1,6 +1,7 @@
#include "input_resolver.h" #include "input_resolver.h"
#include <string> #include <string>
#include <iostream> #include <iostream>
#include <sstream>
#define delimiter " " #define delimiter " "
@@ -47,10 +48,10 @@ ValidationResult validate_token(const std::string &token)
if (digitpos == -1) if (digitpos == -1)
return {true, ' ', 0}; return {true, token[0], 0};
int next = stoi(token.substr(digitpos)); int next = stoi(token.substr(digitpos));
return {true, ' ', next}; return {true, token[0], next};
} }
Command resolveCommand() Command resolveCommand()
@@ -63,21 +64,28 @@ Command resolveCommand()
auto validation_result = validate_token(token); auto validation_result = validate_token(token);
if (!validation_result.ok()) 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 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)
{ {
static const std::string operators {"+-*/%"}; static const std::string operators {"+-*/%$"};
if(operators.find(c) != operators.npos) if(operators.find(c) != operators.npos)
{ {
operation = c; operation = c;
} }
} }
if (operation == 0)
{
operation = '+';
}
return {shouldQuit, operation, validation_result.next_arg(), shouldPrint}; return {shouldQuit, operation, validation_result.next_arg(), shouldPrint};
} }
+2
View File
@@ -1,2 +1,4 @@
#pragma once
#include "command.h" #include "command.h"
Command resolveCommand(); Command resolveCommand();
+4 -3
View File
@@ -1,17 +1,18 @@
#include <iostream> #include <iostream>
#include "basecalc.h" #include "basecalc.h"
#include "special_calculator.h"
#include <memory> #include <memory>
#include "input_resolver.h" #include "input_resolver.h"
int main() 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) 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())
+7
View File
@@ -0,0 +1,7 @@
#include "special_calculator.h"
#include <cmath>
int SpecialCalculator::special(int a)
{
return sqrt(a);
}
+2
View File
@@ -1,3 +1,5 @@
#pragma once
#include "basecalc.h" #include "basecalc.h"
class SpecialCalculator: public BaseCalculator class SpecialCalculator: public BaseCalculator