Skip to content

Commit

Permalink
Calculator finished
Browse files Browse the repository at this point in the history
  • Loading branch information
fferegrino committed Dec 10, 2016
1 parent e10fbdf commit 1fcbe3c
Show file tree
Hide file tree
Showing 4 changed files with 446 additions and 188 deletions.
91 changes: 85 additions & 6 deletions Calculator/CalculatorBrain.cs
Original file line number Diff line number Diff line change
@@ -1,28 +1,63 @@

using System;
using System.Linq;
using System.Collections.Generic;

namespace Calculator
{
public class CalculatorBrain
{
double _accumulator;
bool _hasSetDot;
double DecimalPositions = 10;
const double IntegerPositions = 10;
const int MaxMemory = 10;
Queue<string> _operations = new Queue<string>(MaxMemory);

internal void SetOperand(double operand)

internal void AddOperand(double operand, bool userIsInTheMiddleOfTyping)
{
_accumulator = operand;
if (userIsInTheMiddleOfTyping)
{
if (!_hasSetDot)
{
_accumulator = _accumulator * IntegerPositions + operand;
//_integerPositions *= 10;
}
else
{
_accumulator = _accumulator + operand / DecimalPositions;
DecimalPositions *= 10;
}
}
else
{
DecimalPositions = 10;
if (!_hasSetDot)
{
_accumulator = operand;
//_integerPositions *= 10;
}
else
{
_accumulator = operand / DecimalPositions;
DecimalPositions *= 10;
}
}
}

enum Operation
{
Constant,
UnaryOperation,
BinaryOperation,
Equals
Equals,
Dot,
Clear
}

struct PendingOperationInfo
{
{
public PendingOperationInfo(Func<double, double, double> binaryOperation, double firstOperand)
{
BinaryOperation = binaryOperation;
Expand All @@ -49,13 +84,19 @@ void PerformPendingOperation()
{
{ "π", Operation.Constant },
{ "e", Operation.Constant },
{ "C", Operation.Clear },
{ "√", Operation.UnaryOperation },
{ "±", Operation.UnaryOperation },
{ "cos", Operation.UnaryOperation },
{ "sin", Operation.UnaryOperation },
{ "tan", Operation.UnaryOperation },
{ "x!", Operation.UnaryOperation },
{ "×", Operation.BinaryOperation },
{ "÷", Operation.BinaryOperation },
{ "−", Operation.BinaryOperation },
{ "+", Operation.BinaryOperation },
{ "=", Operation.Equals }
{ "=", Operation.Equals },
{ ".", Operation.Dot }
};

Dictionary<string, double> constants = new Dictionary<string, double>
Expand All @@ -68,9 +109,13 @@ void PerformPendingOperation()
{
{ "√", Math.Sqrt },
{ "±", (d ) => -1 * d },
{ "cos", Math.Cos },
{ "sin", Math.Sin},
{ "tan", Math.Tan },
{ "x!", (d) => d }
};

Dictionary<string, Func<double,double,double>> binaries = new Dictionary<string, Func<double, double, double>>
Dictionary<string, Func<double, double, double>> binaries = new Dictionary<string, Func<double, double, double>>
{
{ "×", (a,b) => a * b },
{ "÷", (a,b) => a / b },
Expand All @@ -83,6 +128,7 @@ internal void PerformOperation(string symbol)
Operation op;
if (operations.TryGetValue(symbol, out op))
{
AddRecentOp(symbol);
switch (op)
{
case Operation.Constant:
Expand All @@ -92,18 +138,51 @@ internal void PerformOperation(string symbol)
_accumulator = unaries[symbol](_accumulator);
break;
case Operation.BinaryOperation:

_hasSetDot = false;
PerformPendingOperation();
_pendingOperation = new PendingOperationInfo(binaries[symbol], _accumulator);

break;
case Operation.Equals:
PerformPendingOperation();
break;
case Operation.Dot:
if (!_hasSetDot)
{
_hasSetDot = true;
}
break;
case Operation.Clear:
Clear();
break;
default:
break;
}
}
}

void Clear()
{
_hasSetDot = false;
_accumulator = 0;
_pendingOperation = null;
DecimalPositions = 10;
}


internal String RecentOperations
{
get { return String.Join(" ", _operations); }
}

private void AddRecentOp(string op)
{
if(_operations.Count == MaxMemory)
_operations.Dequeue();
_operations.Enqueue(op);
}

internal Double Result
{
get { return _accumulator; }
Expand Down
Loading

0 comments on commit 1fcbe3c

Please sign in to comment.