StringMathCalculator/Calculator.cs
///<summary>
///<para>The main method for calculations.</para>
///<para>Reads <paramref name="calculations"/> from right to left and parses the characters to doubles and operations.</para>
///</summary>
///<returns>
///The calculations ready to be calculated in order.
///</returns>
///<exception cref="System.FormatException">
///Thrown when a string provided has illegal characters, too many parentheses or too many decimals in a number.
///</exception>
///<param name="calculations">A string with math calculations.</param>
///
The main method for calculations.Reads  from right to left and parses the characters to doubles and operations.
The main method for calculations . Reads from right to left and parses the characters to doubles and operations .
public CalculatorCalculation Calculation(string calculations)
        {
            calculations = removeSpaces.Replace(calculations, string.Empty);
            foreach(var wordReplacement in wordReplacements)
            {
                calculations = calculations.Replace(wordReplacement.Value, wordReplacement.Key.ToString());
            }
            if(Regex.IsMatch(calculations, legalCharacters.ToString() + "]"))
            {
                throw new FormatException("Illegal characters in calculation.");
            }
            var calculationHelper = new CalculationHelper();
            for (int i = calculations.Length - 1; i > -1; i--)
            {
                bool isNum = int.TryParse(calculations[i].ToString(), out _);
                if (isNum)
                {
                    calculationHelper.OperationNext = true;
                    calculationHelper.NumberChar.Push(calculations[i]);
                    if (i == 0)
                    {
                        string numberString = new string(calculationHelper.NumberChar.ToArray());
                        calculationHelper.OperationOrder.Enqueue(new OperationNum(double.Parse(numberString)));
                    }
                }
                else if (calculations[i].Equals(')'))
                {
                    RightParentheses(calculationHelper);
                }
                else if (calculations[i].Equals('('))
                {
                    i = LeftParentheses(calculations, calculationHelper, i);
                }
                else if (calculations[i].Equals('.'))
                {
                    Decimal(calculations, calculationHelper, i);
                }
                else if(!calculationHelper.OperationNext)
                {
                    throw new FormatException("Operation found next to another operation.");
                }
                else
                {
                    if(calculations[i].Equals('-'))
                    {
                        if(i == 0)
                        {
                            calculationHelper.NumberChar.Push(calculations[i]);
                            calculationHelper.OperationOrder.Enqueue(
                                new OperationNum(
                                    double.Parse(new string(calculationHelper.NumberChar.ToArray()))));
                            continue;
                        }
                        else
                        {
                            char leftCharacter = calculations[i - 1];
                            bool isLeftNum = int.TryParse(leftCharacter.ToString(), out _);
                            if(!isLeftNum  
                                && !leftCharacter.Equals(')'))
                            {
                                calculationHelper.NegativeNum = true;
                                calculationHelper.NumberChar.Push(calculations[i]);
                                continue;
                            }
                        }
                    }
                    if(i == 0)
                    {
                        throw new FormatException("Operation found at the beginning of the string");
                    }
                    double number = double.Parse(new string(calculationHelper.NumberChar.ToArray()));
                    calculationHelper.Reset();
                    var calculatorOperation = singleCharOperations[calculations[i]];
                    calculationHelper.OperationOrder.Enqueue(
                        new OperationPair(number,
                                      calculatorOperation.Weight,
                                      calculatorOperation.CalculationPair));
                }
            }
            return OrderCalculations(calculationHelper.OperationOrder);
        }
public CalculatorCalculation Calculation ( string calculations ) { calculations = removeSpaces . Replace ( calculations , string . Empty ) ; foreach ( var wordReplacement in wordReplacements ) { calculations = calculations . Replace ( wordReplacement . Value , wordReplacement . Key . ToString ( ) ) ; } if ( Regex . IsMatch ( calculations , legalCharacters . ToString ( ) + " " ) ) { throw new FormatException ( " " ) ; } var calculationHelper = new CalculationHelper ( ) ; for ( int i = calculations . Length - 1 ; i > - 1 ; i -- ) { bool isNum = int . TryParse ( calculations [ i ] . ToString ( ) , out _ ) ; if ( isNum ) { calculationHelper . OperationNext = true ; calculationHelper . NumberChar . Push ( calculations [ i ] ) ; if ( i == 0 ) { string numberString = new string ( calculationHelper . NumberChar . ToArray ( ) ) ; calculationHelper . OperationOrder . Enqueue ( new OperationNum ( double . Parse ( numberString ) ) ) ; } } else if ( calculations [ i ] . Equals ( ' ' ) ) { RightParentheses ( calculationHelper ) ; } else if ( calculations [ i ] . Equals ( ' ' ) ) { i = LeftParentheses ( calculations , calculationHelper , i ) ; } else if ( calculations [ i ] . Equals ( ' ' ) ) { Decimal ( calculations , calculationHelper , i ) ; } else if ( ! calculationHelper . OperationNext ) { throw new FormatException ( " " ) ; } else { if ( calculations [ i ] . Equals ( ' ' ) ) { if ( i == 0 ) { calculationHelper . NumberChar . Push ( calculations [ i ] ) ; calculationHelper . OperationOrder . Enqueue ( new OperationNum ( double . Parse ( new string ( calculationHelper . NumberChar . ToArray ( ) ) ) ) ) ; continue ; } else { char leftCharacter = calculations [ i - 1 ] ; bool isLeftNum = int . TryParse ( leftCharacter . ToString ( ) , out _ ) ; if ( ! isLeftNum && ! leftCharacter . Equals ( ' ' ) ) { calculationHelper . NegativeNum = true ; calculationHelper . NumberChar . Push ( calculations [ i ] ) ; continue ; } } } if ( i == 0 ) { throw new FormatException ( " " ) ; } double number = double . Parse ( new string ( calculationHelper . NumberChar . ToArray ( ) ) ) ; calculationHelper . Reset ( ) ; var calculatorOperation = singleCharOperations [ calculations [ i ] ] ; calculationHelper . OperationOrder . Enqueue ( new OperationPair ( number , calculatorOperation . Weight , calculatorOperation . CalculationPair ) ) ; } } return OrderCalculations ( calculationHelper . OperationOrder ) ; }
The main method for calculations.Reads  from right to left and parses the characters to doubles and operations.
The main method for calculations . Reads from right to left and parses the characters to doubles and operations .
//StringBuilder builder = new StringBuilder(calculations.ToLower()); //builder.Replace(removeSpaces, string.Empty); //If the character is a number add it to the numberChar queue //Check if we are on the end of the calculations string //If we are, add a SingleNum to the operation order with the number //Check if the number is negative //var (calculationPair, weight) = GetCalculationPairAndWeight(calculations[i]);
The calculations ready to be calculated in order.
The calculations ready to be calculated in order .
Thrown when a string provided has illegal characters, too many parentheses or too many decimals in a number.
Thrown when a string provided has illegal characters too many parentheses or too many decimals in a number .
calculations
string
A string with math calculations.
A string with math calculations .