Compare commits
8 Commits
688f490ec1
..
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 110a292529 | |||
| 000629b937 | |||
| f651fcdb4c | |||
| 93da1771f4 | |||
| 1ef2665418 | |||
| 9c090bc317 | |||
| 8dda608d07 | |||
| 78ada5fbd4 |
+15
-2
@@ -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:
|
||||
|
||||
+6
-1
@@ -71,7 +71,7 @@ Command resolveCommand()
|
||||
|
||||
bool shouldQuit = token.find("q") != token.npos;
|
||||
bool shouldPrint = token.find("p") != token.npos;
|
||||
char operation;
|
||||
char operation = 0;
|
||||
|
||||
for (const char c: token)
|
||||
{
|
||||
@@ -82,5 +82,10 @@ Command resolveCommand()
|
||||
}
|
||||
}
|
||||
|
||||
if (operation == 0)
|
||||
{
|
||||
operation = '+';
|
||||
}
|
||||
|
||||
return {shouldQuit, operation, validation_result.next_arg(), shouldPrint};
|
||||
}
|
||||
@@ -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())
|
||||
|
||||
Reference in New Issue
Block a user